From c9ee654290d4b5f99855e06ff64a1cb8ed3e50b4 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Tue, 3 Mar 2015 15:16:07 +0100 Subject: [PATCH] src: simplify node::Utf8Value() * Remove kStorageSize constant. * Remove superfluous local variable and reinterpret_cast. * Reorder data members so the length field and data pointer (if not the data itself) fit in a single cache line. PR-URL: https://github.com/iojs/io.js/pull/1042 Reviewed-By: Trevor Norris --- src/util.cc | 21 ++++++++------------- src/util.h | 3 +-- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/util.cc b/src/util.cc index 1e8d801dbab9e5..a0bd077a93aaf9 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1,11 +1,10 @@ #include "util.h" - #include "string_bytes.h" namespace node { Utf8Value::Utf8Value(v8::Isolate* isolate, v8::Handle value) - : length_(0), str_(nullptr) { + : length_(0), str_(str_st_) { if (value.IsEmpty()) return; @@ -15,19 +14,15 @@ 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; - if (len > kStorageSize) - str = static_cast(malloc(len)); - else - str = str_st_; - CHECK_NE(str, NULL); + if (len > sizeof(str_st_)) { + str_ = static_cast(malloc(len)); + CHECK_NE(str_, nullptr); + } const int flags = v8::String::NO_NULL_TERMINATION | v8::String::REPLACE_INVALID_UTF8; - length_ = val_->WriteUtf8(str, len, 0, flags); - str[length_] = '\0'; - - str_ = reinterpret_cast(str); + length_ = val_->WriteUtf8(str_, len, 0, flags); + str_[length_] = '\0'; } + } // namespace node diff --git a/src/util.h b/src/util.h index 5742252688111b..ea17a155745993 100644 --- a/src/util.h +++ b/src/util.h @@ -190,10 +190,9 @@ class Utf8Value { }; private: - static const int kStorageSize = 1024; size_t length_; - char str_st_[kStorageSize]; char* str_; + char str_st_[1024]; }; } // namespace node