Optimize some memory allocations from ActorPath. #4351
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I was seeing a lot of allocations happening in a system with tens of thousands of actors, which was causing the system to slow down over hours or days due to the increase of time spent in GC. This PR optimizes away some allocations from commonly used ActorPath methods, in order of importance.
The GetHashCode() code path is invoked a lot in the code due to the various caches using Dictionary<IActorRef, xxx>. Many of the Dictionary methods invoke GetHashCode() on the key.
I needed to add the newly added GetHashCode() to CoreAPISpec.ApproveCore.approved.txt, though it's not really a public API change.
This mostly triggers when comparing identical ActorPaths which are different objects, as the Uid comparison early-exits non-matching comparisons.
Changed ActorPath.Validate() to take a string instead of char[]. This avoids an allocation due to not needing to call string.ToCharArray(). Some micro-benchmarks show ToCharArray() a tiny bit faster, but less GC pressure is arguably more desirable behavior. There might be faster still variants if fixed(char* p) -style tricks are used.
The ActorPath.SystemElements and UserElements weren't used anywhere, so I removed them.