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

Reduce allocations in TextDocumentStates.AddRange #75640

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,18 @@ public ImmutableArray<TValue> SelectAsArray<TValue, TArg>(Func<TState, TArg, TVa
}

public TextDocumentStates<TState> AddRange(ImmutableArray<TState> states)
=> new(_ids.AddRange(states.Select(state => state.Id)),
States.AddRange(states.Select(state => KeyValuePairUtil.Create(state.Id, state))),
filePathToDocumentIds: null);
{
using var pooledIds = SharedPools.Default<List<DocumentId>>().GetPooledObject();
var ids = pooledIds.Object;
Copy link
Member

Choose a reason for hiding this comment

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

Worth setting the capacity of this list?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I'd rather not unless it shows up. The reasons:

  1. Looks like it would need to ensure the desired size exceeds the existing capacity before setting the capacity, otherwise it may throw
  2. Setting the capacity explicitly avoids the double resizing code in EnsureCapacity. So, without doing that ourselves here, we'd potentially be allocating quite a bit more often.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Going to go ahead and merge, let me know if you would like followup on this


foreach (var state in states)
ids.Add(state.Id);

return new(
_ids.AddRange(ids),
States.AddRange(states.Select(state => KeyValuePairUtil.Create(state.Id, state))),
filePathToDocumentIds: null);
}

public TextDocumentStates<TState> RemoveRange(ImmutableArray<DocumentId> ids)
{
Expand Down
Loading