Skip to content

Streamline rent/return on ArrayPool - #55710

Merged
stephentoub merged 2 commits into
dotnet:mainfrom
stephentoub:arraypoolslim
Jul 16, 2021
Merged

Streamline rent/return on ArrayPool#55710
stephentoub merged 2 commits into
dotnet:mainfrom
stephentoub:arraypoolslim

Conversation

@stephentoub

Copy link
Copy Markdown
Member

A variety of tweaks to reduce overheads:

  • Stop storing and using a _bucketArraySizes. It's cheaper to recompute the shift on each use than it is to index into the array (with a bounds check). Plus less memory.
  • The 99% case is renting a positive length for pooled array sizes (especially now that we've bumped the limit up to a gig). Move the checks for lengths <= 0 to after the check for whether the length is poolable.
  • Move arrays/counts into locals to enable the JIT to eliminate some bounds checks.
  • Use ThrowHelpers where we already have them
  • Move non-generic helpers out of generic class into Utilities
  • Consolidate buffer allocation in Rent to a single line
  • Reorganize TLS checks to be as early as possible
  • Use FastMod instead of % in per-core stacks
MethodToolchainMeanErrorStdDevRatioRatioSDCode Size
PopPush\main\corerun.exe17.58 ns0.357 ns0.334 ns1.000.002,221 B
PopPush\pr\corerun.exe16.64 ns0.021 ns0.019 ns0.950.021,980 B
PopPushMulti\main\corerun.exe1,127.07 ns7.303 ns6.098 ns1.000.002,167 B
PopPushMulti\pr\corerun.exe1,082.29 ns3.208 ns2.679 ns0.960.011,875 B

@ghost

Copy link
Copy Markdown

I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label.

@ghost

Copy link
Copy Markdown

Tagging subscribers to this area: @GrabYourPitchforks, @dotnet/area-system-buffers
See info in area-owners.md if you want to be subscribed.

Issue Details

A variety of tweaks to reduce overheads:

  • Stop storing and using a _bucketArraySizes. It's cheaper to recompute the shift on each use than it is to index into the array (with a bounds check). Plus less memory.
  • The 99% case is renting a positive length for pooled array sizes (especially now that we've bumped the limit up to a gig). Move the checks for lengths <= 0 to after the check for whether the length is poolable.
  • Move arrays/counts into locals to enable the JIT to eliminate some bounds checks.
  • Use ThrowHelpers where we already have them
  • Move non-generic helpers out of generic class into Utilities
  • Consolidate buffer allocation in Rent to a single line
  • Reorganize TLS checks to be as early as possible
  • Use FastMod instead of % in per-core stacks
MethodToolchainMeanErrorStdDevRatioRatioSDCode Size
PopPush\main\corerun.exe17.58 ns0.357 ns0.334 ns1.000.002,221 B
PopPush\pr\corerun.exe16.64 ns0.021 ns0.019 ns0.950.021,980 B
PopPushMulti\main\corerun.exe1,127.07 ns7.303 ns6.098 ns1.000.002,167 B
PopPushMulti\pr\corerun.exe1,082.29 ns3.208 ns2.679 ns0.960.011,875 B
Author:stephentoub
Assignees:-
Labels:

area-System.Buffers

Milestone:6.0.0

Comment threadsrc/libraries/System.Private.CoreLib/src/System/Buffers/Utilities.cs Outdated
- Stop storing and using a _bucketArraySizes. It's cheaper to recompute the shift on each use than it is to index into the array (with a bounds check). Plus less memory.
- The 99% case is renting a positive length for pooled array sizes (especially now that we've bumped the limit up to a gig). Move the checks for lengths <= 0 to after the check for whether the length is poolable.
- Move arrays into locals to enable the JIT to eliminate some bounds checks.
- Use ThrowHelpers where we already have them
- Move non-generic helpers out of generic class into Utilities
- Consolidate buffer allocation in Rent to a single line
- Reorganize TLS checks to be as early as possible
- Use FastMod instead of % in per-core stacks
@stephentoub
stephentoub merged commit d4dcde1 into dotnet:mainJul 16, 2021
@stephentoub
stephentoub deleted the arraypoolslim branch July 16, 2021 14:49
@ghostghost locked as resolved and limited conversation to collaborators Aug 15, 2021
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@stephentoub@jkotas