Use .pop() to flush rerenderQueue instead of .shift() #4630
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.
This pull request slightly modifies how
rerenderQueue
is flushed.The idea is to use
.pop()
to flush the rerender queue instead of using.shift()
. This also requires inverting the depth sorting comparison function to keep the queue in reverse order.The rerender queue is an array that was previously emptied by calling the array's
.shift()
method repeatedly. In v8 the.shift()
operation appears to O(n) time cost, based on looking at the relevant parts of v8's source code and a relevant open issue. Therefore, when.shift()
is called repeatedly for a large array the aggregate time spent seems to grow faster than linearly.In contrast the
.pop()
method of arrays seems to behave in O(1) way, at least when amortized over many operations. Therefor calling it repeatedly tends to take a linear-ish time in aggregate.The overall performance effect of these changes is likely very small in the common case, as these changes address only the overhead from the rerender queue management. Usually everything else, like the queued rerenders themselves, take much more time. Still, these changes aim to avoid the queue management becoming a bottleneck in extreme cases by avoiding O(n^2) behavior when hundreds or thousands rerenders are getting queued simultaneously.
The performance effect seems to be a wash when the queue stays relatively small, with the exception when the queue length is 1. There is now an optimization that skips sorting queues of length < 2.