perf(createSelector): gather input selector results without extra allocations - #772
Open
veksa wants to merge 1 commit into
Open
perf(createSelector): gather input selector results without extra allocations#772veksa wants to merge 1 commit into
veksa wants to merge 1 commit into
Conversation
✅ Deploy Preview for reselect-docs canceled.
|
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
dependenciesCheckercallscollectInputSelectorResults(dependencies, arguments)on every dependency recomputation. Inlining that loop is worth about a fifth of the call, for two separate reasons:argumentsstops escaping. Passing it to another function forces V8 to materialize it on the heap every call. Used only as the second operand of.apply, it can stay in the frame. The dev-mode block below still hands it around, but that block is compiled out of production builds, which is the only build this matters in.[]bypush. This is the larger half. I assumed it would be the smaller one.I also tried arity-specialized array literals on top of this —
[d0(...)],[d0(...), d1(...)], and so on. They measured no better than a singlenew Array(length)loop at 1 and 3 inputs, and at 6 inputs the ladder was slightly worse than the fallback, so there's one path for every dependency count instead of a cutoff I couldn't justify.dependenciesand itslengthare still read per call rather than hoisted, so a selector whosedependenciesarray is mutated behaves as before.Measured with the harness from #770 (paired against
masterin one process, min of 15 interleaved rounds, ns/call):(state, props)(state, props)(state, props)argsMemoize: lruMemoize(state)The last two rows are selectors called with the state alone, so they hit the argument cache and never reach this code — nothing to gain there. The win shrinks as the input count grows, which makes sense: the allocation saved is fixed, the input selector calls aren't.
Recomputation counts are identical everywhere, so it's the same work throughout.
A profile is what pointed here, and it also killed my first two guesses — arity dispatch and the array allocation looked like the obvious targets, and
dependenciesCheckerwas 1.3% of self time. The array turned out to matter anyway, but through allocation and GC rather than through the code being slow.