-
Notifications
You must be signed in to change notification settings - Fork 98
fix: zero copy to create a new Buffer #916
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
Conversation
Summary of ChangesHello @fengmk2, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a performance optimization by implementing zero-copy Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. WalkthroughChanged Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Code Review
This pull request correctly implements a zero-copy buffer creation in readDistBytesToBuffer, which is a good performance optimization. The change avoids an unnecessary memory copy when converting a Uint8Array to a Buffer. The implementation is sound and handles the case where the input might already be a Buffer. A similar optimization could be applied to the readDistBytesToString method, which is still using Buffer.from(bytes) and thus performing a copy. This could be considered for a follow-up change to further improve performance in other parts of the repository, like readDistBytesToJSON.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #916 +/- ##
========================================
Coverage 95.25% 95.25%
========================================
Files 197 197
Lines 22102 22104 +2
Branches 2593 2049 -544
========================================
+ Hits 21054 21056 +2
Misses 1048 1048 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Pull request overview
This PR optimizes the readDistBytesToBuffer method in DistRepository by implementing zero-copy Buffer creation from Uint8Array. When converting a Uint8Array to a Buffer, the change uses Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength) instead of Buffer.from(bytes), which creates a view over the existing ArrayBuffer rather than copying the data. This improves performance for package manifest operations where large binary data is read from the NFS storage layer.
Key Changes:
- Updated
readDistBytesToBufferto use zero-copy Buffer creation via ArrayBuffer reference - Added documentation comment linking to Node.js v24 Buffer API documentation
Co-authored-by: Copilot <[email protected]> Signed-off-by: MK (fengmk2) <[email protected]>
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.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
app/repository/DistRepository.ts(1 hunks)
🧰 Additional context used
📓 Path-based instructions (4)
**/*.{js,ts,tsx,jsx}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.{js,ts,tsx,jsx}: Use single quotes in JavaScript/TypeScript code (from Prettier configuration)
Use 2-space indentation (from Prettier configuration)
Maintain 120 character line width maximum (from Prettier configuration)
Use trailing commas in ES5 syntax (from Prettier configuration)
Avoid parentheses in arrow functions when possible (from Prettier configuration)
Maximum of 6 function parameters (from Oxlint configuration)
Warn on console usage (from Oxlint configuration)
Disallow anonymous default exports (from Oxlint configuration)
Use ES modules (import/export) syntax throughout
Files:
app/repository/DistRepository.ts
**/*.{ts,tsx}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.{ts,tsx}: Use strict TypeScript with comprehensive type definitions - avoidanytypes, use proper typing orunknown
Export types and interfaces for reusability in TypeScript
Files:
app/repository/DistRepository.ts
app/repository/**/*.ts
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
app/repository/**/*.ts: Repository methods must follow naming convention:findSomething,saveSomething,removeSomething,listSomethings
Repositories must contain CRUD operations and query building without business logic
Repositories must inject dependencies using@Inject()decoratorUse repository method naming convention:
findX(single),saveX(create/update),removeX(delete),listXs(multiple, plural)
Files:
app/repository/DistRepository.ts
**/*.ts
📄 CodeRabbit inference engine (CLAUDE.md)
**/*.ts: Use strict TypeScript with proper typing - avoidanytypes, use proper typing orunknowninstead
Use ES modules withimport/exportsyntax throughout the codebase
Use single quotes (') for strings
Use 2-space indentation
Enforce 120 character line width
Use ES5 trailing commas
Limit functions to a maximum of 6 parameters
Do not use console statements - use logger instead
Files:
app/repository/DistRepository.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (18)
- GitHub Check: test on mysql (node@24, shard@0/3, enableJSONBuilder@true)
- GitHub Check: test on mysql (node@22, shard@2/3, enableJSONBuilder@true)
- GitHub Check: test on mysql (node@22, shard@2/3, enableJSONBuilder@false)
- GitHub Check: test on mysql (node@22, shard@0/3, enableJSONBuilder@true)
- GitHub Check: test on mysql (node@22, shard@1/3, enableJSONBuilder@false)
- GitHub Check: test on mysql (node@22, shard@1/3, enableJSONBuilder@true)
- GitHub Check: test on mysql (node@24, shard@1/3, enableJSONBuilder@false)
- GitHub Check: test on mysql (node@22, shard@0/3, enableJSONBuilder@false)
- GitHub Check: test on mysql (node@24, shard@2/3, enableJSONBuilder@true)
- GitHub Check: test on mysql (node@24, shard@1/3, enableJSONBuilder@true)
- GitHub Check: test on mysql (node@24, shard@0/3, enableJSONBuilder@false)
- GitHub Check: test on postgresql (node@22, shard@2/3)
- GitHub Check: test on postgresql (node@22, shard@0/3)
- GitHub Check: test on postgresql (node@22, shard@1/3)
- GitHub Check: test on postgresql (node@24, shard@2/3)
- GitHub Check: test on postgresql (node@24, shard@1/3)
- GitHub Check: test on postgresql (node@24, shard@0/3)
- GitHub Check: build-and-push-image
| async readDistBytesToBuffer(dist: Dist): Promise<Buffer | undefined> { | ||
| const bytes = await this.readDistBytes(dist); | ||
| if (!bytes) return undefined; | ||
| return Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes); | ||
| // if bytes is Uint8Array, should use zero copy to create a new Buffer | ||
| // https://nodejs.org/docs/latest-v24.x/api/buffer.html#static-method-bufferfromarraybuffer-byteoffset-length | ||
| return Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength); | ||
| } |
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.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Check Node.js version configuration
echo "=== Checking Node.js version configuration ==="
if [ -f "package.json" ]; then
echo "--- package.json engines ---"
cat package.json | jq -r '.engines.node // "not specified"'
fi
if [ -f ".nvmrc" ]; then
echo "--- .nvmrc ---"
cat .nvmrc
fi
if [ -f ".node-version" ]; then
echo "--- .node-version ---"
cat .node-version
fiRepository: cnpm/cnpmcore
Length of output: 170
🏁 Script executed:
#!/bin/bash
# Examine the DistRepository.ts file structure
echo "=== Examining DistRepository.ts ==="
wc -l app/repository/DistRepository.ts
echo -e "\n=== All method names in DistRepository ==="
ast-grep --pattern 'class $_ {
$$$
$_($$$) {
$$$
}
$$$
}' app/repository/DistRepository.ts || grep -n "^\s*\(async\|public\|private\)\s\+\w\+" app/repository/DistRepository.ts
echo -e "\n=== Full DistRepository file (first 150 lines) ==="
head -150 app/repository/DistRepository.tsRepository: cnpm/cnpmcore
Length of output: 170
Rename method to follow repository naming convention.
The method readDistBytesToBuffer violates the repository naming convention. Repository methods must use findX, saveX, removeX, or listXs patterns. Rename this to findDistBytesAsBuffer or appropriate equivalent.
The zero-copy Buffer implementation using Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength) is correct, but cannot be approved until the naming convention is addressed.
🤖 Prompt for AI Agents
In app/repository/DistRepository.ts around lines 90 to 96, rename the method
readDistBytesToBuffer to follow repository naming conventions (use
findX/saveX/removeX/listXs); update the method name to findDistBytesAsBuffer (or
another find-prefixed variant), keep the same async signature
Promise<Buffer|undefined> and the existing zero-copy Buffer.from(...)
implementation, and then update all internal and external
references/imports/tests to use the new name so TypeScript types and exports
remain consistent.
[skip ci] ## <small>4.13.3 (2025-12-06)</small> * fix: zero copy to create a new Buffer (#916) ([683ee1d](683ee1d)), closes [#916](#916) [hi#level](https://github.com/hi/issues/level) * chore(deps): update mime-types to v3 (#899) ([0a5c90c](0a5c90c)), closes [#899](#899) [#865](#865) * chore(deps): upgrade base-x to v5 (#897) ([d3e4fae](d3e4fae)), closes [#897](#897) [#864](#864)
|
🎉 This PR is included in version 4.13.3 🎉 The release is available on: Your semantic-release bot 📦🚀 |
https://nodejs.org/docs/latest-v24.x/api/buffer.html#static-method-bufferfromarraybuffer-byteoffset-length
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.