diff options
author | Brian White <mscdex@mscdex.net> | 2017-05-30 12:56:09 -0400 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2017-06-13 11:48:59 -0700 |
commit | add4b0ab8cc0ec663cd4623e9032c14830873760 (patch) | |
tree | 97134dcaa2e4aa7406fb4d25a6293cb992049136 /src/node_zlib.cc | |
parent | e5dc934ef6f66edade76720dc7592e9e348db49f (diff) | |
download | android-node-v8-add4b0ab8cc0ec663cd4623e9032c14830873760.tar.gz android-node-v8-add4b0ab8cc0ec663cd4623e9032c14830873760.tar.bz2 android-node-v8-add4b0ab8cc0ec663cd4623e9032c14830873760.zip |
zlib: improve performance
PR-URL: https://github.com/nodejs/node/pull/13322
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Diffstat (limited to 'src/node_zlib.cc')
-rw-r--r-- | src/node_zlib.cc | 106 |
1 files changed, 53 insertions, 53 deletions
diff --git a/src/node_zlib.cc b/src/node_zlib.cc index 4495eb2bca..07cd526b1e 100644 --- a/src/node_zlib.cc +++ b/src/node_zlib.cc @@ -40,14 +40,17 @@ namespace node { using v8::Array; +using v8::ArrayBuffer; using v8::Context; +using v8::Function; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::HandleScope; -using v8::Integer; using v8::Local; using v8::Number; using v8::Object; +using v8::Persistent; +using v8::Uint32Array; using v8::Value; namespace { @@ -86,7 +89,8 @@ class ZCtx : public AsyncWrap { write_in_progress_(false), pending_close_(false), refs_(0), - gzip_id_bytes_read_(0) { + gzip_id_bytes_read_(0), + write_result_(nullptr) { MakeWeak<ZCtx>(this); Wrap(wrap, this); } @@ -200,38 +204,19 @@ class ZCtx : public AsyncWrap { if (!async) { // sync version - ctx->env()->PrintSyncTrace(); + env->PrintSyncTrace(); Process(work_req); - if (CheckError(ctx)) - AfterSync(ctx, args); + if (CheckError(ctx)) { + ctx->write_result_[0] = ctx->strm_.avail_out; + ctx->write_result_[1] = ctx->strm_.avail_in; + ctx->write_in_progress_ = false; + ctx->Unref(); + } return; } // async version - uv_queue_work(ctx->env()->event_loop(), - work_req, - ZCtx::Process, - ZCtx::After); - - args.GetReturnValue().Set(ctx->object()); - } - - - static void AfterSync(ZCtx* ctx, const FunctionCallbackInfo<Value>& args) { - Environment* env = ctx->env(); - Local<Integer> avail_out = Integer::New(env->isolate(), - ctx->strm_.avail_out); - Local<Integer> avail_in = Integer::New(env->isolate(), - ctx->strm_.avail_in); - - ctx->write_in_progress_ = false; - - Local<Array> result = Array::New(env->isolate(), 2); - result->Set(0, avail_in); - result->Set(1, avail_out); - args.GetReturnValue().Set(result); - - ctx->Unref(); + uv_queue_work(env->event_loop(), work_req, ZCtx::Process, ZCtx::After); } @@ -389,16 +374,14 @@ class ZCtx : public AsyncWrap { if (!CheckError(ctx)) return; - Local<Integer> avail_out = Integer::New(env->isolate(), - ctx->strm_.avail_out); - Local<Integer> avail_in = Integer::New(env->isolate(), - ctx->strm_.avail_in); - + ctx->write_result_[0] = ctx->strm_.avail_out; + ctx->write_result_[1] = ctx->strm_.avail_in; ctx->write_in_progress_ = false; // call the write() cb - Local<Value> args[2] = { avail_in, avail_out }; - ctx->MakeCallback(env->callback_string(), arraysize(args), args); + Local<Function> cb = PersistentToLocal(env->isolate(), + ctx->write_js_callback_); + ctx->MakeCallback(cb, 0, nullptr); ctx->Unref(); if (ctx->pending_close_) @@ -447,41 +430,51 @@ class ZCtx : public AsyncWrap { // just pull the ints out of the args and call the other Init static void Init(const FunctionCallbackInfo<Value>& args) { - CHECK((args.Length() == 4 || args.Length() == 5) && - "init(windowBits, level, memLevel, strategy, [dictionary])"); + CHECK(args.Length() == 7 && + "init(windowBits, level, memLevel, strategy, writeResult, writeCallback," + " dictionary)"); ZCtx* ctx; ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder()); int windowBits = args[0]->Uint32Value(); - CHECK((windowBits >= 8 && windowBits <= 15) && "invalid windowBits"); + CHECK((windowBits >= Z_MIN_WINDOWBITS && windowBits <= Z_MAX_WINDOWBITS) && + "invalid windowBits"); int level = args[1]->Int32Value(); - CHECK((level >= -1 && level <= 9) && "invalid compression level"); + CHECK((level >= Z_MIN_LEVEL && level <= Z_MAX_LEVEL) && + "invalid compression level"); int memLevel = args[2]->Uint32Value(); - CHECK((memLevel >= 1 && memLevel <= 9) && "invalid memlevel"); + CHECK((memLevel >= Z_MIN_MEMLEVEL && memLevel <= Z_MAX_MEMLEVEL) && + "invalid memlevel"); int strategy = args[3]->Uint32Value(); CHECK((strategy == Z_FILTERED || - strategy == Z_HUFFMAN_ONLY || - strategy == Z_RLE || - strategy == Z_FIXED || - strategy == Z_DEFAULT_STRATEGY) && "invalid strategy"); + strategy == Z_HUFFMAN_ONLY || + strategy == Z_RLE || + strategy == Z_FIXED || + strategy == Z_DEFAULT_STRATEGY) && "invalid strategy"); + + CHECK(args[4]->IsUint32Array()); + Local<Uint32Array> array = args[4].As<Uint32Array>(); + Local<ArrayBuffer> ab = array->Buffer(); + uint32_t* write_result = static_cast<uint32_t*>(ab->GetContents().Data()); + + Local<Function> write_js_callback = args[5].As<Function>(); char* dictionary = nullptr; size_t dictionary_len = 0; - if (args.Length() >= 5 && Buffer::HasInstance(args[4])) { - Local<Object> dictionary_ = args[4]->ToObject(args.GetIsolate()); + if (Buffer::HasInstance(args[6])) { + const char* dictionary_ = Buffer::Data(args[6]); + dictionary_len = Buffer::Length(args[6]); - dictionary_len = Buffer::Length(dictionary_); dictionary = new char[dictionary_len]; - - memcpy(dictionary, Buffer::Data(dictionary_), dictionary_len); + memcpy(dictionary, dictionary_, dictionary_len); } - Init(ctx, level, windowBits, memLevel, strategy, - dictionary, dictionary_len); + Init(ctx, level, windowBits, memLevel, strategy, write_result, + write_js_callback, dictionary, dictionary_len); SetDictionary(ctx); } @@ -500,7 +493,9 @@ class ZCtx : public AsyncWrap { } static void Init(ZCtx *ctx, int level, int windowBits, int memLevel, - int strategy, char* dictionary, size_t dictionary_len) { + int strategy, uint32_t* write_result, + Local<Function> write_js_callback, char* dictionary, + size_t dictionary_len) { ctx->level_ = level; ctx->windowBits_ = windowBits; ctx->memLevel_ = memLevel; @@ -564,6 +559,9 @@ class ZCtx : public AsyncWrap { } ctx->env()->ThrowError("Init error"); } + + ctx->write_result_ = write_result; + ctx->write_js_callback_.Reset(ctx->env()->isolate(), write_js_callback); } static void SetDictionary(ZCtx* ctx) { @@ -670,6 +668,8 @@ class ZCtx : public AsyncWrap { bool pending_close_; unsigned int refs_; unsigned int gzip_id_bytes_read_; + uint32_t* write_result_; + Persistent<Function> write_js_callback_; }; |