Skip to content

BlobBuilder.LinkSuffix/LinkPrefix need to Free zero length chunks #99266

Closed

Description

Description

Both LinkSuffix and LinkPrefix have an optimization around zero length chunk to avoid unnecessary chaining:

// avoid chaining empty chunks:
if (prefix.Count == 0)
{
    return;
}

The issue is that LinkPrefix/Suffix effectively pass ownership of the BlobBuilder to the receiver. In the non-zero case it's put into the chain of chunks and freeing the head will free the time. In the zero case though the suffix / prefix is never freed. That makes managing pools difficult:

BlobBuilder b1 = GetBuilder1();
BlobBuilder b2 = GetBuilder2();
b1.LinkSuffix(b2);
b1.Free(); // Did that free b2 or not? Depends on length of b2

Reproduction Steps

var b1 = PooledBlobBuilder.GetInstance();
var b2 = PooledBlobBuilder.GetInstance();
var b3 = PooledBlobBuilder.GetInstance();
b1.WriteBytes(1, 1);
b2.WriteBytes(1, 1);
b1.LinkSuffix(b2);
b1.LinkSuffix(b3);
b1.Free();

Expected behavior

All of the instances are freed

Actual behavior

The b3 instance is not freed cause it was discarded in LinkSuffix for having a zero length.

Regression?

No.

Known Workarounds

Add LinkSuffixSmart / LinkPrefixSmart extension methods that do the zero check.

Configuration

No response

Other information

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions