Skip to content

Commit

Permalink
Replace LINQ's ArrayBuilder, LargeArrayBuilder, and SparseArrayBuilder (
Browse files Browse the repository at this point in the history
#96570)

There's a lot of code involved in these, some of which we special-case to only build into some of the targets, and it's unnecessarily complicated.  We can get most of the benefit with a simpler solution, which this attempts to provide. This commit removes those three types and replaces them with a SegmentedArrayBuilder.

The changes to ToArray also obviate the need for the old Buffer type. It existed to avoid one array allocation at the end of loading an enumerable, where we'd doubled and doubled and doubled in size, and then eventually have an array with all the data but some extra space. Now that we don't have such an array, we don't need Buffer, and can just the normal Enumerable.ToArray.

The one thing the new scheme doesn't handle as well is when there are multiple sources being added (specifically in Concat / SelectMany). Previously, the code used a complicated scheme to reserve space in the output for partial sources known to be ICollections, so it could use ICollection.CopyTo for each consistuent source. But CopyTo doesn't support partial copies, which means we can only use it if there's enough room available in an individual segment. The implementation thus tries to use it, and falls back to normal enumeration if it can't. For larger enumerations where the cost would end up being more apparent, the expectation is we'd quickly grow to a segment size where the subsequent appends are able to fit into the current segment. Hopefully.
  • Loading branch information
stephentoub authored Jan 8, 2024
1 parent e2f58fe commit f44d62c
Show file tree
Hide file tree
Showing 31 changed files with 714 additions and 1,312 deletions.

This file was deleted.

This file was deleted.

Loading

0 comments on commit f44d62c

Please sign in to comment.