diff options
author | Anna Henningsen <anna@addaleax.net> | 2019-02-25 04:12:19 +0100 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2019-03-01 21:57:54 +0100 |
commit | 31975bbc88d353cf2eecc93c9d2cde62dba67b2c (patch) | |
tree | 7a91545cfd50182e50addd1aa3a6468d74f65474 /src/node_http2.cc | |
parent | b42dcb0eeb2d2c302b0ecabbc1092605a54213d6 (diff) | |
download | android-node-v8-31975bbc88d353cf2eecc93c9d2cde62dba67b2c.tar.gz android-node-v8-31975bbc88d353cf2eecc93c9d2cde62dba67b2c.tar.bz2 android-node-v8-31975bbc88d353cf2eecc93c9d2cde62dba67b2c.zip |
src: allow not materializing ArrayBuffers from C++
Where appropriate, use a helper that wraps around
`ArrayBufferView::Buffer()` or `ArrayBufferView::CopyContents()`
rather than `Buffer::Data()`, as that may help to avoid materializing
the underlying `ArrayBuffer` when reading small typed arrays from C++.
This allows keeping the performance benefits of the faster creation of
heap-allocated small typed arrays in many cases.
PR-URL: https://github.com/nodejs/node/pull/26301
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src/node_http2.cc')
-rw-r--r-- | src/node_http2.cc | 26 |
1 files changed, 12 insertions, 14 deletions
diff --git a/src/node_http2.cc b/src/node_http2.cc index 7fc21c9cae..8946c71fc3 100644 --- a/src/node_http2.cc +++ b/src/node_http2.cc @@ -12,6 +12,7 @@ namespace node { using v8::ArrayBuffer; +using v8::ArrayBufferView; using v8::Boolean; using v8::Context; using v8::Float64Array; @@ -2483,7 +2484,7 @@ void Http2Session::Request(const FunctionCallbackInfo<Value>& args) { // state of the Http2Session, it's simply a notification. void Http2Session::Goaway(uint32_t code, int32_t lastStreamID, - uint8_t* data, + const uint8_t* data, size_t len) { if (IsDestroyed()) return; @@ -2508,16 +2509,13 @@ void Http2Session::Goaway(const FunctionCallbackInfo<Value>& args) { uint32_t code = args[0]->Uint32Value(context).ToChecked(); int32_t lastStreamID = args[1]->Int32Value(context).ToChecked(); - Local<Value> opaqueData = args[2]; - uint8_t* data = nullptr; - size_t length = 0; + ArrayBufferViewContents<uint8_t> opaque_data; - if (Buffer::HasInstance(opaqueData)) { - data = reinterpret_cast<uint8_t*>(Buffer::Data(opaqueData)); - length = Buffer::Length(opaqueData); + if (args[2]->IsArrayBufferView()) { + opaque_data.Read(args[2].As<ArrayBufferView>()); } - session->Goaway(code, lastStreamID, data, length); + session->Goaway(code, lastStreamID, opaque_data.data(), opaque_data.length()); } // Update accounting of data chunks. This is used primarily to manage timeout @@ -2771,10 +2769,10 @@ void Http2Session::Ping(const FunctionCallbackInfo<Value>& args) { // A PING frame may have exactly 8 bytes of payload data. If not provided, // then the current hrtime will be used as the payload. - uint8_t* payload = nullptr; - if (Buffer::HasInstance(args[0])) { - payload = reinterpret_cast<uint8_t*>(Buffer::Data(args[0])); - CHECK_EQ(Buffer::Length(args[0]), 8); + ArrayBufferViewContents<uint8_t, 8> payload; + if (args[0]->IsArrayBufferView()) { + payload.Read(args[0].As<ArrayBufferView>()); + CHECK_EQ(payload.length(), 8); } Local<Object> obj; @@ -2799,7 +2797,7 @@ void Http2Session::Ping(const FunctionCallbackInfo<Value>& args) { // the callback will be invoked and a notification sent out to JS land. The // notification will include the duration of the ping, allowing the round // trip to be measured. - ping->Send(payload); + ping->Send(payload.data()); args.GetReturnValue().Set(true); } @@ -2871,7 +2869,7 @@ Http2Session::Http2Ping::Http2Ping(Http2Session* session, Local<Object> obj) session_(session), startTime_(uv_hrtime()) {} -void Http2Session::Http2Ping::Send(uint8_t* payload) { +void Http2Session::Http2Ping::Send(const uint8_t* payload) { uint8_t data[8]; if (payload == nullptr) { memcpy(&data, &startTime_, arraysize(data)); |