uNetworking / uWebSockets.js

μWebSockets for Node.js back-ends :metal:
Apache License 2.0
8.08k stars 574 forks source link

send with array of buffers #837

Closed ronag closed 1 year ago

ronag commented 1 year ago

Currently, I'm doing this:

ws.cork(() => {
  for (const data of buffer.splice(0)) (
    ws.send(data)
  }
})

Which does a lot of js->cpp calls. Would it make sense to be able to do the following?

ws.cork(() => {
  ws.send(buffer.splice(0))
})
uNetworkingAB commented 1 year ago

528 #679 are similar posts.

I'm not interested in micro optimizing some specific use case - JavaScript is a script and if you plan your data flow you can get decent IO perf with uWS. But the shortcomings of this script should not result in this library adding tons of hacks to avoid JavaScript behavior. After all you are scripting.

If these micro overheads are a problem then use C++, but these are not really low hanging fruits there are way way heavier problems in Node.js itself (we lose 70% perf in node.js 14 because of mistakes in nodejs)

uNetworkingAB commented 1 year ago

All functions like writeStatus, writeHeader, write, send, should ideally be V8 fast calls but until Node.js camp exposes that API I cannot do anything about it. But even now, the overhead of calling into C++ is negligible compared to the overhead of calling into JS with node::MakeCallback. It's like day and night of a difference. You lose a drip going into C++ but you lose an entire ocean going into JS.

ronag commented 1 year ago

@uNetworkingAB Trying to raise the MakeCallback issue again. https://github.com/nodejs/performance/issues/24

Do you have any benchmark script that can be used for reference in the discussion?