From e17e6fb2faa04193eddf8062fbd49f1612b4dbff Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Fri, 30 Jan 2015 21:43:31 +0300 Subject: [PATCH] util: use on-stack buffer for Utf8Value Improves `crypto.createHash().update().digest()` performance by 10%. PR-URL: https://github.com/iojs/io.js/pull/670 Reviewed-By: Ben Noordhuis --- src/util.cc | 9 +++++++-- src/util.h | 5 ++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/util.cc b/src/util.cc index 01ea76a8f89..1c57a976e11 100644 --- a/src/util.cc +++ b/src/util.cc @@ -16,15 +16,20 @@ Utf8Value::Utf8Value(v8::Isolate* isolate, v8::Handle value) // Allocate enough space to include the null terminator size_t len = StringBytes::StorageSize(val_, UTF8) + 1; - char* str = static_cast(calloc(1, len)); + char* str; + if (len > kStorageSize) + str = static_cast(malloc(len)); + else + str = str_st_; + CHECK_NE(str, NULL); int flags = WRITE_UTF8_FLAGS; - flags |= ~v8::String::NO_NULL_TERMINATION; length_ = val_->WriteUtf8(str, len, 0, flags); + str[length_] = '\0'; str_ = reinterpret_cast(str); } diff --git a/src/util.h b/src/util.h index 80eb2b1ad13..e0fa0fd0096 100644 --- a/src/util.h +++ b/src/util.h @@ -110,7 +110,8 @@ class Utf8Value { explicit Utf8Value(v8::Isolate* isolate, v8::Handle value); ~Utf8Value() { - free(str_); + if (str_ != str_st_) + free(str_); } char* operator*() { @@ -126,7 +127,9 @@ class Utf8Value { }; private: + static const int kStorageSize = 1024; size_t length_; + char str_st_[kStorageSize]; char* str_; };