More object field resolution / reduction optimizations #2029
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.
There are 2 main optimizations in this PR:
reduceObjectStep
mutable.HashMap
in ObjectFieldResolverNow, I'm somewhat conflicted about (2). For some reason, Scala's immutable HashMapBuilder is much slower than using a mutable Map. I realised this inefficiency after looking at some profiling graphs, where a lot of time was spend on the
+=
method of the HashMapBuilder. It seems that even if the HashMapBuilder is mutable, the implementation copies the underlying arrays on each new entry (no idea why).Since this is in our very hot path and we probably can't go changing the Scala std library, one way to optimize for it is to use a mutable HashMap, and change the Map type on ObjectStep from Map (i.e.,
immutable.Map
) tocollection.Map
which is the base type for both mutable and immutable collections. This change is binary incompatible but largely source-compatible so most users should not need to do any changes to their code.As I said though, I'm fairly conflicted about this change. It does offer a fairly sizeable performance improvement over the current implementation, but it could potentially leak mutability if misused; even if
collection.Map
doesn't expose any mutable APIs a user could potentially pattern match and get the mutable instance. Having said that, I don't know how much of an issue this actually isseries/2.x:
PR (~10% improvement):