-
Notifications
You must be signed in to change notification settings - Fork 504
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
perf: improving performance #528
Closed
Closed
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 |
---|---|---|
@@ -1,11 +1,17 @@ | ||
// parse out just the options we care about so we always get a consistent | ||
// obj with keys in a consistent order. | ||
const opts = ['includePrerelease', 'loose', 'rtl'] | ||
const parseOptions = options => | ||
!options ? {} | ||
: typeof options !== 'object' ? { loose: true } | ||
: opts.filter(k => options[k]).reduce((o, k) => { | ||
o[k] = true | ||
return o | ||
}, {}) | ||
module.exports = parseOptions | ||
const parseOptions = options => { | ||
if (!options) return {}; | ||
|
||
if (typeof options !== 'object') return { loose: true }; | ||
|
||
const parsedOptions = {}; | ||
|
||
// parse out just the options we care about so we always get a consistent | ||
// obj with keys in a consistent order. | ||
|
||
if (options.includePrerelease) parsedOptions.includePrerelease = true; | ||
if (options.loose) parsedOptions.loose = true; | ||
if (options.rtl) parsedOptions.rtl = true; | ||
|
||
return parsedOptions; | ||
}; | ||
module.exports = parseOptions; |
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.
You may also want to try a handwritten key such as:
Which is what we do in the TypeScript compiler. Not sure if it's faster than here but could be interesting to test.
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.
It doesn't hurt the performance:
After 1B ops/s, it almost has no effect choosing one or another.
The only drawback is increasing the lru-cache key by 6 characters instead of having it as 1.
If NPM team prefer readability over a tiny cache key, I can change the implementation.
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.
This is actually a good use case for a bit flag. It's a bunch of booleans, and bitwise operations are super fast. And while bit ops are often frowned upon as being dense or obscure, if organized properly, they're much more maintainable than a series of
if/else
statements and hoping that you captured each case.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.
I don't think the suggestion is to change Options, but to use a different way of constructing a key.
That being said, I think it should be possible to internally store flags for these and then recustruct the options bag when asked on whichever methods that request it. Then, internally, things can use the fast check and pass
number
around. Spitballing, though.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.
Yes, I realized a few seconds after writing that, and yes, using the option internally and then exposing makes sense, I'll try using the getter to compute the options and internally using just the bit flag.
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.
Thinking further, the user already has to pass the object in, so you probably can just keep that one and not reconstruct it.
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.
@jakebailey I tried, but it would break a lot of tests if I did.
I put the version of parseOptions with bit flags here: h4ad-forks@6c7a68a
@isaacs Take a look and see if this PR is worth pushing, I made a lot of changes.
Also, I introduce 1 small breaking change, now the numbers passed to
parseOptions
are considered valid options, so instead of re-parsing it by passing the object, I use many of the options as bit flags to construct new objects that are inside the functions.About performance, no performance penalties and I assume it got a bit faster because I now parse the options as an object once and then just parse the options again as a number which saves some comparisons.
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.
The only downside I notice when using bitmask is the memory usage (ref: #528 (comment)) is the same as allocating the objects at runtime (8.7mb).
I don't know why this behavior is happening, but I just want to point out that it's not as memory efficient as Object.freeze.