Commit d27a026
committed
refactor(duplicatedcontent): length-prefix rootDir instead of hashing
Pass 3 of code review on PR #732 found that hashing rootDir with
sha256 (added in 07e0f10 to fix a real key-collision risk) was more
than the problem needs: corpusIndexKey is called once per file on
corpusIndex's hot path, and this is a cache-key uniqueness need, not
an adversarial one, so paying a cryptographic hash for it is wasted
CPU and an allocation on every call.
Write rootDir with a netstring-style length prefix (its byte length,
':', then the literal bytes) instead: the prefix makes rootDir's
boundary unambiguous regardless of its content — two different rootDir
values can only produce the same "N:" + N-byte segment by having the
same length and the same bytes — without needing a fixed-length
digest. Cheaper than sha256+hex, same collision-freedom guarantee.
TestCorpusIndexKey_RootDirControlBytesCannotBleedIntoOtherFields is
updated to pin the new invariant (key starts with the exact length
prefix) instead of the old fixed-length-digest one; confirmed red
against the prior bare-concatenation scheme before this commit's
predecessor fixed it, and again here against a bare-rootDir variant
with no prefix at all.1 parent 07e0f10 commit d27a026
2 files changed
Lines changed: 29 additions & 17 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
396 | 396 | | |
397 | 397 | | |
398 | 398 | | |
399 | | - | |
400 | | - | |
401 | | - | |
402 | | - | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
403 | 409 | | |
404 | 410 | | |
405 | 411 | | |
406 | 412 | | |
407 | | - | |
408 | | - | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
409 | 416 | | |
410 | 417 | | |
411 | 418 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
1039 | 1040 | | |
1040 | 1041 | | |
1041 | 1042 | | |
1042 | | - | |
1043 | | - | |
1044 | | - | |
1045 | | - | |
1046 | | - | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
1047 | 1050 | | |
1048 | 1051 | | |
1049 | 1052 | | |
| |||
1054 | 1057 | | |
1055 | 1058 | | |
1056 | 1059 | | |
1057 | | - | |
1058 | | - | |
1059 | | - | |
1060 | | - | |
1061 | | - | |
1062 | | - | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
1063 | 1068 | | |
1064 | 1069 | | |
1065 | 1070 | | |
| |||
0 commit comments