Skip to content

Commit c9ee654

Browse files
committedMar 5, 2015
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: #1042 Reviewed-By: Trevor Norris <[email protected]>
1 parent 364cc7e commit c9ee654

File tree

2 files changed

+9
-15
lines changed

2 files changed

+9
-15
lines changed
 

‎src/util.cc

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#include "util.h"
2-
32
#include "string_bytes.h"
43

54
namespace node {
65

76
Utf8Value::Utf8Value(v8::Isolate* isolate, v8::Handle<v8::Value> value)
8-
: length_(0), str_(nullptr) {
7+
: length_(0), str_(str_st_) {
98
if (value.IsEmpty())
109
return;
1110

@@ -15,19 +14,15 @@ Utf8Value::Utf8Value(v8::Isolate* isolate, v8::Handle<v8::Value> value)
1514

1615
// Allocate enough space to include the null terminator
1716
size_t len = StringBytes::StorageSize(val_, UTF8) + 1;
18-
19-
char* str;
20-
if (len > kStorageSize)
21-
str = static_cast<char*>(malloc(len));
22-
else
23-
str = str_st_;
24-
CHECK_NE(str, NULL);
17+
if (len > sizeof(str_st_)) {
18+
str_ = static_cast<char*>(malloc(len));
19+
CHECK_NE(str_, nullptr);
20+
}
2521

2622
const int flags =
2723
v8::String::NO_NULL_TERMINATION | v8::String::REPLACE_INVALID_UTF8;
28-
length_ = val_->WriteUtf8(str, len, 0, flags);
29-
str[length_] = '\0';
30-
31-
str_ = reinterpret_cast<char*>(str);
24+
length_ = val_->WriteUtf8(str_, len, 0, flags);
25+
str_[length_] = '\0';
3226
}
27+
3328
} // namespace node

‎src/util.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,9 @@ class Utf8Value {
190190
};
191191

192192
private:
193-
static const int kStorageSize = 1024;
194193
size_t length_;
195-
char str_st_[kStorageSize];
196194
char* str_;
195+
char str_st_[1024];
197196
};
198197

199198
} // namespace node

0 commit comments

Comments
 (0)
Please sign in to comment.