Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allocate exactly the space needed when creating a sourcetext #71886

Merged
merged 15 commits into from
Feb 1, 2024

Conversation

CyrusNajmabadi
Copy link
Member

No description provided.

@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner January 31, 2024 04:41
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Jan 31, 2024
@@ -17,6 +17,10 @@ namespace Microsoft.CodeAnalysis.Shared.Extensions;

internal static partial class SourceTextExtensions
{
// char pooled memory : 8K * 256 = 2MB
private const int CharBufferSize = 4 * 1024;
private static readonly ObjectPool<char[]> CharArrayPool = new(() => new char[CharBufferSize], 256);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved these here as this is the only place it is used. and it is easier to validate proper usage.


// number of chunks
var numberOfChunks = 1 + (length / buffer.Length);
writer.WriteInt32(numberOfChunks);

// write whole chunks
try
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

view with whitespace off.

var elementType = typeof(byte);
Debug.Assert(s_typeMap[elementType] == TypeCode.UInt8);
var elementType = typeof(T);
Contract.ThrowIfFalse(s_typeMap.ContainsKey(elementType));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s_typeMap

TryGetValue to avoid duplicated lookup?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't mind this too much. so we get a nice contract failure if we defy expectations here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big deal, but not sure why this is less clear:

Contract.ThrowIfFalse(s_typeMap.TryGetValue(elementType, out var actualType));
Contract.ThrowIfTrue(expectedType != actualType);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, misunderstood. yes, that's nicer. will do in followup.

}

/// <summary>
/// Write an array of bytes. The array data is provided as a <see
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bytes

nit: chars

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can fix in followup :)

Action<BinaryWriter, T[], int> write)
{
var spanLength = span.Length;
var buffer = System.Buffers.ArrayPool<T>.Shared.Rent(Math.Min(spanLength, rentLength));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

System.Buffers.ArrayPool

I'm curious why you chose to use System.Buffer.ArrayPools instead of ArrayBuilder

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

offline: for straight IO buffers, this feels more in line with what the runtime does.

/// </summary>
/// <param name="span">The array data.</param>
public void WriteValue(ReadOnlySpan<byte> span)
public void WriteSpan(ReadOnlySpan<byte> span)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public void WriteSpan(ReadOnlySpan span)

Just going to mention it in the PR for posterity, but would be nice if we wouldn't need to write the preamble for every chunk we want persisted.

Copy link
Contributor

@ToddGrun ToddGrun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@CyrusNajmabadi CyrusNajmabadi merged commit fd51297 into dotnet:main Feb 1, 2024
26 of 28 checks passed
@ghost ghost added this to the Next milestone Feb 1, 2024
@CyrusNajmabadi CyrusNajmabadi deleted the sourceTextAllocate branch February 1, 2024 19:15
@jjonescz jjonescz modified the milestones: Next, 17.10 P2 Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants