Restructured code to reduce the number of allocations during dispatch. #1
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 use Tree Style Tab with a fairly large number of tabs. I love the extension, but it causes my battery to drain too quickly. I'm sure at a lower number of tabs, this isn't much of a problem. But, I'm profiling it now and trying to fix some stuff.
This set of changes is fairly small, but cuts the retained memory for 18% -> 3% and the allocated memory from 8% -> 2% when I profiled with 25 tabs. I tried to profile under similar conditions and I only focused on "Web Extensions" in the profiler, but there are system variances and workload variances that likely impact it. I ran the profiles multiple times and got similar results each time. But, I'd still take the absolute numbers with a grain of salt and focus more on the relative difference between them. While I didn't focus explicitly on it, it appears from the profiles that GC frequency dropped as well.
The summary of changes are:
results
array with the correct size to avoid any array resizesmap
over the array, iterate over the set directlymap
callWhile I realize this changes from a functional style to a more procedural style, the performance impact is fairly large. I'm open to other approaches as well. I profiled keeping the
map
but hoisting the function passed to it so we can just pass a function reference rather an allocate a function object. That helps as well and maybe makes the code a bit cleaner, but the benefit was only about half of what this PR proposes.