New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reduce memory allocation in rules and utils #2976
Draft
willheslam
wants to merge
13
commits into
jsx-eslint:master
Choose a base branch
from
willheslam:optimisations
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+708
−514
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b4cd869
Curry read only props reporting function to avoid creating closures
willheslam 152a276
Replace Object.keys with values/entries
willheslam 35a88cb
Avoid allocating empty objects or arrays when possible
willheslam 69bcb8d
Speed up string manipulation and testing
willheslam da9231a
Speed up Components list by avoiding some array allocations and remov…
willheslam 8eff94d
Speed up Components utils
willheslam 671341e
Cache lower cased pragma
willheslam 0f7183e
Replace isFirstLetterCapitalised with regex check
willheslam 473d7c1
Replace array traversals with object property lookup
willheslam ccdd17c
Speed up no-deprecated
willheslam a2b5327
Preallocate arrays used as logical or equality checks
willheslam d16fcb3
Avoid recomputing expensive React version check
willheslam 232d6a1
Various speed ups by avoiding work and memory allocations
willheslam File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
|
||
'use strict'; | ||
|
||
const values = require('object.values'); | ||
const Components = require('../util/Components'); | ||
const docsUrl = require('../util/docsUrl'); | ||
|
||
|
@@ -93,11 +94,13 @@ module.exports = { | |
const list = components.list(); | ||
|
||
// If no defaultProps could be found, we don't report anything. | ||
Object.keys(list).filter((component) => list[component].defaultProps).forEach((component) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same with this .filter change to an if check inside forEach? |
||
reportInvalidDefaultProps( | ||
list[component].declaredPropTypes, | ||
list[component].defaultProps || {} | ||
); | ||
values(list).forEach((component) => { | ||
if (component.defaultProps) { | ||
reportInvalidDefaultProps( | ||
component.declaredPropTypes, | ||
component.defaultProps || {} | ||
); | ||
} | ||
}); | ||
} | ||
}; | ||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
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 was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how meaningful is this specific change? the engine should be able to optimize this down to the equivalent for loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question.
I'm not convinced v8 can optimise a filter and forEach into an equivalent for loop - some (admittedly slapdash and micro) benchmarking with node v14.15.3 iterating many times over an array shows a singular forEach with a built-in conditional as reliably faster than a filter, forEach chain:
The singular forEach is consistently faster - depending on the arrangement of the code seems to be around 20 -> 40% faster.
Going with more iterations but a smaller array shows similar results too, just in case:
Here's the gist:
https://gist.github.com/willheslam/d45a1d183d5d778d82c5113ffb8a4ab8
(using a naked for loop is faster than all three, but that's probably going too far)
Whether this specific change is meaningful, I don't honestly have concrete answer - it depends on the frequency the function is called, the average size of
args
(and how many of them fulfil the condition)and also how much the surrounding environment is generating memory pressure.
Benchmarking each individual change is difficult, given the noise and iteration time and how some effects of memory allocation are only obvious in concert, so as a blanket rule this PR finds any quick, localised opportunities to reduce memory allocation, where the change didn't massively impact the flow of the code.