diff options
author | Ben Noordhuis <info@bnoordhuis.nl> | 2018-06-19 21:14:31 +0200 |
---|---|---|
committer | Ben Noordhuis <info@bnoordhuis.nl> | 2018-06-22 12:04:20 +0200 |
commit | 7ec6951034658b63f908977caeb83112d77cdf3b (patch) | |
tree | 0ed2589ebbd5d9a4c9a8f2f4943b7757eb199651 /src/node_internals.h | |
parent | cbe307dc812a9088aa909837b9edd7e728e14953 (diff) | |
download | android-node-v8-7ec6951034658b63f908977caeb83112d77cdf3b.tar.gz android-node-v8-7ec6951034658b63f908977caeb83112d77cdf3b.tar.bz2 android-node-v8-7ec6951034658b63f908977caeb83112d77cdf3b.zip |
src: avoid common case heap allocation
Optimize three functions that pass on (part of) their JS arguments to
the JS function they call by stack-allocating the storage in the common
case.
PR-URL: https://github.com/nodejs/node/pull/21409
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Eugene Ostroukhov <eostroukhov@google.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Diffstat (limited to 'src/node_internals.h')
-rw-r--r-- | src/node_internals.h | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/node_internals.h b/src/node_internals.h index 025fa10100..cd791f8c05 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -40,6 +40,7 @@ #include <stdlib.h> #include <string> +#include <vector> // Custom constants used by both node_constants.cc and node_zlib.cc #define Z_MIN_WINDOWBITS 8 @@ -315,6 +316,39 @@ class FatalTryCatch : public v8::TryCatch { Environment* env_; }; +class SlicedArguments { + public: + inline explicit SlicedArguments( + const v8::FunctionCallbackInfo<v8::Value>& args, + size_t start = 0); + inline size_t size() const { return size_; } + inline v8::Local<v8::Value>* data() { return data_; } + + private: + size_t size_; + v8::Local<v8::Value>* data_; + v8::Local<v8::Value> fixed_[64]; + std::vector<v8::Local<v8::Value>> dynamic_; +}; + +SlicedArguments::SlicedArguments( + const v8::FunctionCallbackInfo<v8::Value>& args, + size_t start) : size_(0), data_(fixed_) { + const size_t length = static_cast<size_t>(args.Length()); + if (start >= length) return; + const size_t size = length - start; + + if (size > arraysize(fixed_)) { + dynamic_.resize(size); + data_ = dynamic_.data(); + } + + for (size_t i = 0; i < size; ++i) + data_[i] = args[i + start]; + + size_ = size; +} + void ReportException(Environment* env, v8::Local<v8::Value> er, v8::Local<v8::Message> message); |