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 <trev.norris@gmail.com>
This commit is contained in:
parent
364cc7e08a
commit
c9ee654290
21
src/util.cc
21
src/util.cc
@ -1,11 +1,10 @@
|
|||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
#include "string_bytes.h"
|
#include "string_bytes.h"
|
||||||
|
|
||||||
namespace node {
|
namespace node {
|
||||||
|
|
||||||
Utf8Value::Utf8Value(v8::Isolate* isolate, v8::Handle<v8::Value> value)
|
Utf8Value::Utf8Value(v8::Isolate* isolate, v8::Handle<v8::Value> value)
|
||||||
: length_(0), str_(nullptr) {
|
: length_(0), str_(str_st_) {
|
||||||
if (value.IsEmpty())
|
if (value.IsEmpty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -15,19 +14,15 @@ Utf8Value::Utf8Value(v8::Isolate* isolate, v8::Handle<v8::Value> value)
|
|||||||
|
|
||||||
// Allocate enough space to include the null terminator
|
// Allocate enough space to include the null terminator
|
||||||
size_t len = StringBytes::StorageSize(val_, UTF8) + 1;
|
size_t len = StringBytes::StorageSize(val_, UTF8) + 1;
|
||||||
|
if (len > sizeof(str_st_)) {
|
||||||
char* str;
|
str_ = static_cast<char*>(malloc(len));
|
||||||
if (len > kStorageSize)
|
CHECK_NE(str_, nullptr);
|
||||||
str = static_cast<char*>(malloc(len));
|
}
|
||||||
else
|
|
||||||
str = str_st_;
|
|
||||||
CHECK_NE(str, NULL);
|
|
||||||
|
|
||||||
const int flags =
|
const int flags =
|
||||||
v8::String::NO_NULL_TERMINATION | v8::String::REPLACE_INVALID_UTF8;
|
v8::String::NO_NULL_TERMINATION | v8::String::REPLACE_INVALID_UTF8;
|
||||||
length_ = val_->WriteUtf8(str, len, 0, flags);
|
length_ = val_->WriteUtf8(str_, len, 0, flags);
|
||||||
str[length_] = '\0';
|
str_[length_] = '\0';
|
||||||
|
|
||||||
str_ = reinterpret_cast<char*>(str);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace node
|
} // namespace node
|
||||||
|
@ -190,10 +190,9 @@ class Utf8Value {
|
|||||||
};
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static const int kStorageSize = 1024;
|
|
||||||
size_t length_;
|
size_t length_;
|
||||||
char str_st_[kStorageSize];
|
|
||||||
char* str_;
|
char* str_;
|
||||||
|
char str_st_[1024];
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace node
|
} // namespace node
|
||||||
|
Loading…
x
Reference in New Issue
Block a user