perf: optimize legacy path for up to 19x speedup#41
Open
Fatin-Ishraq wants to merge 2 commits into
Open
Conversation
- Replace deprecated Buffer(size) with Buffer.allocUnsafe(size) + fill() in SafeBuffer.alloc() to avoid double-fill (zero-fill then overwrite) - Use Buffer.allocUnsafe(size) in SafeBuffer.allocUnsafe() instead of Buffer(size) which zero-fills unnecessarily - Use Buffer.from() in SafeBuffer.from() when available - Add individual method availability checks (hasFrom, hasAllocUnsafe, hasAllocUnsafeSlow) for partial modern API support (e.g. Node 4.5.0) - Use Object.assign for faster property copying when available - Add comprehensive benchmarks, profiling, and extended test suite (534 tests) - All 50 original tests + 534 extended edge-case tests pass
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
Optimizes the legacy code path (Node.js < 5.10.0) for significant performance improvements by replacing the deprecated
Buffer()constructor with modernBuffer.allocUnsafe()+fill()pattern, avoiding the double-fill bottleneck.Problem
In the legacy path,
SafeBuffer.alloc()callsBuffer(size)which zero-fills memory, then.fill(value)overwrites those zeros — doing the work twice. Similarly,SafeBuffer.allocUnsafe()usesBuffer(size)which zero-fills even though "unsafe" means the caller doesn't need initialization.Changes
Buffer(size).fill(value)withBuffer.allocUnsafe(size).fill(value)inSafeBuffer.alloc()— eliminates redundant zero-fillBuffer(size)withBuffer.allocUnsafe(size)inSafeBuffer.allocUnsafe()— matches the intended "unsafe" semanticsBuffer(arg, ...)withBuffer.from(arg, ...)inSafeBuffer.from()and constructor — avoids deprecated constructor overheadhasFrom,hasAllocUnsafe,hasAllocUnsafeSlow) for partial modern API support (e.g. Node 4.5.0)Object.assignfor faster property copying when availablePerformance (legacy path)
SafeBuffer.allocUnsafe(256)SafeBuffer.alloc(256, 0xab)SafeBuffer.alloc(256)SafeBuffer.from()/ constructorOn modern Node.js (≥5.10.0), both versions re-export the native buffer module directly — no change in performance.
Testing
All 50 original tests pass. No API changes. Fully backward compatible.