Skip to content
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

Convert writingBuf to Buffer to compute the original string length #188

Merged
merged 3 commits into from
Jan 7, 2024

Conversation

unzan401
Copy link
Contributor

@unzan401 unzan401 commented Dec 27, 2023

Fixes #139

Although #139 (comment) suggested converting this._writingBuf to a buffer, I found that it might reduce performance if we convert every this._writingBuf which just simply writes string.
Therefore, I only handle cases when multi-byte characters exist.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Seems a good fix. I'll need to bench it.

index.js Outdated
Comment on lines 241 to 242
}
function releaseWritingBuf (writingBuf, len, n) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
function releaseWritingBuf (writingBuf, len, n) {
}
/**
* Please add a jsdoc block here.
*/
function releaseWritingBuf (writingBuf, len, n) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

index.js Outdated
Comment on lines 244 to 251
if (typeof writingBuf === 'string' && writingBuf.length !== n) {
// Since the fs.write callback parameter `n` means how many bytes the passed of string
// We calculate the original string length for avoiding the multi-byte character issue
n = Buffer.from(writingBuf).subarray(0, n).toString().length
}
len = Math.max(len - n, 0)
writingBuf = writingBuf.slice(n)
return { writingBuf, len }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could most of this be solved by comparing theBuffer.byteLength instead of theBuffer.length?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. Using .byteLength is more sensible to compare byteLength.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

benchmarks are ok.

@mcollina mcollina merged commit 7a4c445 into pinojs:master Jan 7, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Problem about message missing when write CJK string
3 participants