GeezerVison is showing github.com — buffer: add asString static method by anonrig · Pull Request #45408 · nodejs/node
Conversation
anonrig
commented
Nov 10, 2022
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
My microbenchmarks show 33% improvement for the following microbenchmark. (Fixes nodejs/performance#3)
cpu: Apple M1 Max
runtime: node v20.0.0-pre (arm64-darwin)
benchmark time (avg) (min … max) p75 p99 p995
--------------------------------------------------------------- -----------------------------
new Buffer().toString() 1.65 µs/iter (1.41 µs … 1.95 µs) 1.78 µs 1.95 µs 1.95 µs
Buffer.asString() 1.07 µs/iter (1 µs … 1.21 µs) 1.09 µs 1.21 µs 1.21 µs
TextDecoder.decode() 8.67 µs/iter (8.55 µs … 8.78 µs) 8.7 µs 8.78 µs 8.78 µs
Benchmark:
import { bench, run } from "mitata"
import { Buffer } from "node:buffer"
const encoder = new TextEncoder()
const decoded = 'hello world'.repeat(1000)
const encoded = encoder.encode(decoded)
bench("new Buffer().toString()", () => Buffer.from(encoded).toString())
bench("Buffer.asString()", () => Buffer.asString(encoded, 'utf8'))
bench("TextDecoder.decode()", () => (new TextDecoder()).decode(encoded))
await run()
CC @nodejs/buffer
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Is there a reason why new TextDecoder().decode() couldn’t operate at a similar performance?
I would advise against adding new APIs to Buffer, given that its functionality should at this point be fully covered by standarized Web APIs like Uint8Array and TextEncoder/TextDecoder.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
tniessen, bricss, jimmywarting, and Mrtenz reacted with thumbs up emoji
anonrig
commented
Nov 10, 2022
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
@addaleax TextDecoder creates string_decoder, and in terms of JS/C++ it's a lot costly. To be exact it's 8 times slower than Buffer.asString. I've updated my benchmark.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@anonrig Exactly! I’m saying it shouldn’t be doing that (if it’s about UTF-8 with default options). I’m suggesting to optimize the standardized API, rather than introducing a new, non-standard one (to what is effectively a legacy API).
|
jimmywarting reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
@addaleax TextDecoder creates string_decoder, and in terms of JS/C++ it's a lot costly. To be exact it's 8 times slower than Buffer.asString. I've updated my benchmark.
In your benchmark you don't reuse the TextDecoder instance, which is not how it's usually used. Most projects create a TextDecoder with the desired conversion options once, and then reuse throughout the app.
|
legendecas, BridgeAR, addaleax, and bricss reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@RReverser This is not the case for undici. This is why I had to open this pull request.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
RReverser
commented
Nov 10, 2022
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
@RReverser This is not the case for undici. This is why I had to open this pull request.
Right, but it sounds like a problem that undici could solve easily within the project instead of adding a new API to Node.js itself just to fix it.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
@RReverser This is not the case for undici. This is why I had to open this pull request.
Right, but it sounds like a problem that undici could solve easily within the project instead of adding a new API to Node.js itself just to fix it.
I was feeling the same way before @mcollina's review: nodejs/undici#1768 (comment)
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
TextDecoder is not necessarily comparable to the proposed API here because the newly proposed API does not allow decoding text in a streaming fashion. The statefulness of TextDecoder is what seems to prevent reuse in undici.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
The statefulness of TextDecoder is what seems to prevent reuse in undici.
It's only stateful if you use the stream option, but from the snippet above it doesn't seem they do that? (since they create new TextDecoder instance for each .decode(string) anyway).
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
RReverser
commented
Nov 10, 2022
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
anonrig, bricss, and addaleax reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
This won't help undici unfortunately, we need streaming parsing.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
I’m suggesting to optimize the standardized API, rather than introducing a new, non-standard one (to what is effectively a legacy API).
I would advise against adding new APIs to Buffer, given that its functionality should at this point be fully covered by standarized Web APIs like Uint8Array and TextEncoder/TextDecoder.
👍 Agree, Should improve standard apis (TextEncoder/TextDecoder) instead.
There is also this issue talking about using uint8array instead of buffer #41588 in NodeJS
speaking for my self i discourage any use of Buffer as it's not cross env friendly. It increase the bundle and slows browser down. ( for instance in browsers buffer.toString() is waaay slower than using TextDecoder: source: #39301 )
If new features are being added then ppl will continue to use the legacy Buffer. And more packages is just keep being bloated with the hole browserified buffer module while only using 1% of the Buffer class.
The browser package of the Buffer module haven't been updated much lately. not for 2y and keeps lagging behind in new features.
I'm starting to see new lint rules that starts to forbid the use of Buffer and suggest to use Uint8Array/Text(de)coder/DataView instead.
As such i'm 👎 for this PR
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
anonrig
commented
Nov 10, 2022
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
Thank you for all of the reviews, and responses. It's really great to hear some amazing feedback and experiences about encoding. I opened a new pull request to improve and add a fast path for UTF-8 with @addaleax. Feel free to review the changes in there, since fast path makes this pull request obsolete.
PR: #45412
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Labels
buffer
Issues and PRs related to the buffer subsystem.
c++
Issues and PRs that require attention from people who are familiar with C++.
needs-ci
PRs that need a full CI run.
performance
Issues and PRs related to the performance of Node.js.
semver-minor
PRs that contain new features and should be released in the next minor version.
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uint8Array to UTF8 conversion
Uh oh!
There was an error while loading. Please reload this page.
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.
|