diff options
author | Anna Henningsen <anna@addaleax.net> | 2019-02-25 06:12:19 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2019-03-01 23:57:54 +0300 |
commit | 31975bbc88d353cf2eecc93c9d2cde62dba67b2c (patch) | |
tree | 7a91545cfd50182e50addd1aa3a6468d74f65474 /src/util.h | |
parent | b42dcb0eeb2d2c302b0ecabbc1092605a54213d6 (diff) |
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/util.h')
-rw-r--r-- | src/util.h | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/util.h b/src/util.h index 4cd04dc6af9..4c930e637c5 100644 --- a/src/util.h +++ b/src/util.h @@ -417,6 +417,27 @@ class MaybeStackBuffer { T buf_st_[kStackStorageSize]; }; +// Provides access to an ArrayBufferView's storage, either the original, +// or for small data, a copy of it. This object's lifetime is bound to the +// original ArrayBufferView's lifetime. +template <typename T, size_t kStackStorageSize = 64> +class ArrayBufferViewContents { + public: + ArrayBufferViewContents() {} + + explicit inline ArrayBufferViewContents(v8::Local<v8::Value> value); + explicit inline ArrayBufferViewContents(v8::Local<v8::ArrayBufferView> abv); + inline void Read(v8::Local<v8::ArrayBufferView> abv); + + inline const T* data() const { return data_; } + inline size_t length() const { return length_; } + + private: + T stack_storage_[kStackStorageSize]; + T* data_ = nullptr; + size_t length_ = 0; +}; + class Utf8Value : public MaybeStackBuffer<char> { public: explicit Utf8Value(v8::Isolate* isolate, v8::Local<v8::Value> value); |