perf(fs): optimize bytesToHex encoding - #3
Merged
yifanxuaaa merged 1 commit intoAug 13, 2026
Conversation
yifanxuaaa
added a commit
that referenced
this pull request
Aug 13, 2026
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Reduce the JavaScript CPU cost of the internal
bytesToHexhelper by replacing per-bytetoString(16).padStart(2, "0")formatting with a module-private 256-entry lookup table and using an index loop.This preserves the existing host-neutral
Uint8Arrayimplementation and keeps the call tointrinsicByteRange, including its defensive behavior for NodeBuffervalues and adversarialUint8Arraysubclasses. The table isreadonlyin TypeScript but intentionally not frozen at runtime.Tests
Added direct algorithms-suite coverage for:
Bufferinput in the existing Node test environment;Uint8Arraysubclass that overridesbyteLength,byteOffset,buffer,subarray, and iteration;Local validation on the exact base below:
pnpm build- passbytesToHextest - passpnpm test:m1- pass (41/41)pnpm typecheck- passgit diff --check- passpnpm validate:accepted- base-red, not claimed as a passvalidate:acceptedstops incheck:styleon these two pre-existing files:tests/performance/artifacts-m3-final/A5-one-byte-edit.jsontests/performance/artifacts/A5-one-byte-edit.jsonThe untouched exact-base control fails at the same step with the same two files. After replacing only the isolated worktree path, the candidate and base logs are byte-identical. Neither failing file is modified here.
Microbenchmark
Scope: JavaScript bytes-to-lowercase-hex encoding only, not end-to-end filesystem throughput.
Exact base:
4bc1f8117a7fc210577a347d87b2acfa67ec81e727977c20e82e6efcd7ba1d68191090ad1965673bLocal environment: Node
v26.5.0, V814.6.202.34-node.24, Apple M1 Ultra, arm64 macOS 14.4.1. No distinct Node 22 or 24 runtime was locally available, and no alternate runtime was installed.Protocol: 32-byte deterministic input; 200,000 warm-up iterations per variant; 2,000,000 measured iterations per variant in each of 7 deterministically shuffled repeats; 1,003 correctness vectors per variant; medians below.
for...of+ per-byte formattingfor...ofOn this machine, the combined candidate is 2.44x faster than the existing implementation. The decomposition shows that the lookup table supplies the main gain, while the index loop is an additional change and is not presented as the sole cause. Runtime-freezing the table was 45.2% slower than the mutable module-private table here, so this patch does not use
Object.freeze.These numbers are environment-specific microbenchmark evidence. The claim is limited to reduced JS hex-encoding CPU cost; no filesystem-throughput claim is made.
Telegram: @wangrunyuansecbot | Nickname: 一心
Powered by LingTai AI: https://github.com/Lingtai-AI/lingtai