-
-
Notifications
You must be signed in to change notification settings - Fork 608
Feat/hash serialization #1382
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
Feat/hash serialization #1382
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -359,21 +359,28 @@ function defaultGetLocalIdent( | |
); | ||
} | ||
|
||
// eslint-disable-next-line no-underscore-dangle | ||
const hash = loaderContext._compiler.webpack.util.createHash(hashFunction); | ||
const { hashSalt } = options; | ||
let localIdentHash = ""; | ||
for (let tier = 0; localIdentHash.length < hashDigestLength; tier++) { | ||
// eslint-disable-next-line no-underscore-dangle | ||
const hash = loaderContext._compiler.webpack.util.createHash(hashFunction); | ||
const { hashSalt } = options; | ||
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. Let's move this under loop, because we don't change it 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. Good point! Will fix 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. Fixed in 725e4a5 |
||
|
||
if (hashSalt) { | ||
hash.update(hashSalt); | ||
} | ||
if (hashSalt) { | ||
hash.update(hashSalt); | ||
} | ||
|
||
const tierSalt = Buffer.allocUnsafe(4); | ||
tierSalt.writeUInt32LE(tier); | ||
hash.update(tierSalt); | ||
|
||
hash.update(options.content); | ||
hash.update(options.content); | ||
|
||
const localIdentHash = hash | ||
.digest(hashDigest) | ||
.slice(0, hashDigestLength) | ||
.replace(/[/+]/g, "_") | ||
.replace(/^\d/g, "_"); | ||
localIdentHash = (localIdentHash + hash.digest(hashDigest)) | ||
.replace(/^\d+/, "") | ||
.replace(/\//g, "_") | ||
.replace(/\W+/g, "") | ||
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. And I am afraid about these changes, can you explain 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. Drop all leading digits I could change the last one to /[^a-zA-Z0-9_]+/, for better readablility 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. Changed in 725e4a5 |
||
.slice(0, hashDigestLength); | ||
} | ||
|
||
// TODO need improve on webpack side, we should allow to pass hash/contentHash without chunk property, also `data` for `getPath` should be looks good without chunk property | ||
const ext = path.extname(resourcePath); | ||
|
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 am afraid only for perf here, create multiple times hash is not good idea, can you explain why we need create it again and again and do not reuse existing hash?
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 2nd iteration is only required if the digest length is not enough to generate a pseudorandom string long enough.
If we won't change anything between the iterations then each iteration would generate the same output over and over and the string would just cycle:
If
hash(str) = "ABCDE"
, then it would be"ABCDEABCDEABCDEA"
. Even if this str is 16 chars long its "uniqueness" is only 5 chars long.I've ran
npm test:only
logging the number of times this loop iterated per function call.1375 calls only ran this loop once.122 twice.2 calls took three iterationsThere 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.
Upd: My bad,
jest
spawns 4 processes and they were concurrently writing the same file 😓1672 - 1 time
125 - 2 times
2 - 3 times
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've investigated those two suspicious calls where the loop is iterated 3 times.
It's my own
xxhash64
test from this very PR. I'm asking a hex digest of length 20 (= 80bits) from a digest that can only produce 64 bits and have a really high chance to produce a long run of leading digits.Iteration 0:
digest
is7268299549203d4d
Leading digits are dropped, the resulting string so far is:
7268299549203
d4d
Iteration 1:
digest
is26a899717294ba28
=>7268299549203
d4d
26a899717294ba28
Iteration 2:
digest
is45400c2599876c30
. The string is trimmed at length 20:7268299549203
d4d
26a899717294ba28
4
5400c2599876c30