Revision: 12991
Author: [email protected]
Date: Fri Nov 16 05:30:43 2012
Log: Drastically reduce the number of created strings in d8.
This also reduces the number of TLS accesses, so it is a double-win. We have
230k less TLS accesses while running Octane now, so we are down to 23% now
compared to the start of these TLS-related CLs.
Introduced a tiny helper function for throwing on the way.
Review URL: https://codereview.chromium.org/11417029
http://code.google.com/p/v8/source/detail?r=12991
Modified:
/branches/bleeding_edge/src/d8.cc
=======================================
--- /branches/bleeding_edge/src/d8.cc Fri Sep 14 04:43:46 2012
+++ /branches/bleeding_edge/src/d8.cc Fri Nov 16 05:30:43 2012
@@ -67,6 +67,49 @@
namespace v8 {
+
+static Handle<Value> Throw(const char* message) {
+ return ThrowException(String::New(message));
+}
+
+
+// TODO(rossberg): should replace these by proper uses of HasInstance,
+// once we figure out a good way to make the templates global.
+const char kArrayBufferMarkerPropName[] = "d8::_is_array_buffer_";
+const char kArrayMarkerPropName[] = "d8::_is_typed_array_";
+
+
+namespace Symbols {
+#define FOR_EACH_SYMBOL(V) \
+ V(ArrayBuffer, "ArrayBuffer") \
+ V(ArrayBufferMarkerPropName, kArrayBufferMarkerPropName) \
+ V(ArrayMarkerPropName, kArrayMarkerPropName) \
+ V(buffer, "buffer") \
+ V(byteLength, "byteLength") \
+ V(byteOffset, "byteOffset") \
+ V(BYTES_PER_ELEMENT, "BYTES_PER_ELEMENT") \
+ V(length, "length")
+
+#define DEFINE_SYMBOL(name, value) Persistent<String> name;
+FOR_EACH_SYMBOL(DEFINE_SYMBOL)
+#undef DEFINE_SYMBOL
+
+void Initialize() {
+ HandleScope scope;
+#define INIT_SYMBOL(name, value) \
+ name = Persistent<String>::New(String::NewSymbol(value));
+FOR_EACH_SYMBOL(INIT_SYMBOL)
+#undef INIT_SYMBOL
+}
+
+void TearDown() {
+#define DISPOSE_SYMBOL(name, value) name.Dispose();
+ FOR_EACH_SYMBOL(DISPOSE_SYMBOL)
+#undef DISPOSE_SYMBOL
+}
+}
+
+
LineEditor *LineEditor::first_ = NULL;
@@ -232,11 +275,11 @@
Handle<Value> Shell::Read(const Arguments& args) {
String::Utf8Value file(args[0]);
if (*file == NULL) {
- return ThrowException(String::New("Error loading file"));
+ return Throw("Error loading file");
}
Handle<String> source = ReadFile(*file);
if (source.IsEmpty()) {
- return ThrowException(String::New("Error loading file"));
+ return Throw("Error loading file");
}
return source;
}
@@ -277,14 +320,14 @@
HandleScope handle_scope;
String::Utf8Value file(args[i]);
if (*file == NULL) {
- return ThrowException(String::New("Error loading file"));
+ return Throw("Error loading file");
}
Handle<String> source = ReadFile(*file);
if (source.IsEmpty()) {
- return ThrowException(String::New("Error loading file"));
+ return Throw("Error loading file");
}
if (!ExecuteString(source, String::New(*file), false, true)) {
- return ThrowException(String::New("Error executing file"));
+ return Throw("Error executing file");
}
}
return Undefined();
@@ -314,7 +357,7 @@
if (try_catch->HasCaught()) return 0;
if (raw_value < 0) {
- ThrowException(String::New("Array length must not be negative."));
+ Throw("Array length must not be negative.");
return 0;
}
@@ -323,17 +366,10 @@
ASSERT(kMaxLength == i::ExternalArray::kMaxLength);
#endif // V8_SHARED
if (raw_value > static_cast<int32_t>(kMaxLength)) {
- ThrowException(
- String::New("Array length exceeds maximum length."));
+ Throw("Array length exceeds maximum length.");
}
return raw_value;
}
-
-
-// TODO(rossberg): should replace these by proper uses of HasInstance,
-// once we figure out a good way to make the templates global.
-const char kArrayBufferMarkerPropName[] = "d8::_is_array_buffer_";
-const char kArrayMarkerPropName[] = "d8::_is_typed_array_";
Handle<Value> Shell::CreateExternalArrayBuffer(Handle<Object> buffer,
@@ -341,15 +377,15 @@
static const int32_t kMaxSize = 0x7fffffff;
// Make sure the total size fits into a (signed) int.
if (length < 0 || length > kMaxSize) {
- return ThrowException(String::New("ArrayBuffer exceeds maximum size
(2G)"));
+ return Throw("ArrayBuffer exceeds maximum size (2G)");
}
uint8_t* data = new uint8_t[length];
if (data == NULL) {
- return ThrowException(String::New("Memory allocation failed"));
+ return Throw("Memory allocation failed");
}
memset(data, 0, length);
- buffer->SetHiddenValue(String::New(kArrayBufferMarkerPropName), True());
+ buffer->SetHiddenValue(Symbols::ArrayBufferMarkerPropName, True());
Persistent<Object> persistent_array = Persistent<Object>::New(buffer);
persistent_array.MakeWeak(data, ExternalArrayWeakCallback);
persistent_array.MarkIndependent();
@@ -357,7 +393,7 @@
buffer->SetIndexedPropertiesToExternalArrayData(
data, v8::kExternalByteArray, length);
- buffer->Set(String::New("byteLength"), Int32::New(length), ReadOnly);
+ buffer->Set(Symbols::byteLength, Int32::New(length), ReadOnly);
return buffer;
}
@@ -373,8 +409,7 @@
}
if (args.Length() == 0) {
- return ThrowException(
- String::New("ArrayBuffer constructor must have one argument"));
+ return Throw("ArrayBuffer constructor must have one argument");
}
TryCatch try_catch;
int32_t length = convertToUint(args[0], &try_catch);
@@ -400,12 +435,12 @@
array->SetIndexedPropertiesToExternalArrayData(
static_cast<uint8_t*>(data) + byteOffset, type, length);
- array->SetHiddenValue(String::New(kArrayMarkerPropName),
Int32::New(type));
- array->Set(String::New("byteLength"), Int32::New(byteLength), ReadOnly);
- array->Set(String::New("byteOffset"), Int32::New(byteOffset), ReadOnly);
- array->Set(String::New("length"), Int32::New(length), ReadOnly);
- array->Set(String::New("BYTES_PER_ELEMENT"), Int32::New(element_size));
- array->Set(String::New("buffer"), buffer, ReadOnly);
+ array->SetHiddenValue(Symbols::ArrayMarkerPropName, Int32::New(type));
+ array->Set(Symbols::byteLength, Int32::New(byteLength), ReadOnly);
+ array->Set(Symbols::byteOffset, Int32::New(byteOffset), ReadOnly);
+ array->Set(Symbols::length, Int32::New(length), ReadOnly);
+ array->Set(Symbols::BYTES_PER_ELEMENT, Int32::New(element_size));
+ array->Set(Symbols::buffer, buffer, ReadOnly);
return array;
}
@@ -439,16 +474,15 @@
int32_t byteOffset;
bool init_from_array = false;
if (args.Length() == 0) {
- return ThrowException(
- String::New("Array constructor must have at least one argument"));
+ return Throw("Array constructor must have at least one argument");
}
if (args[0]->IsObject() &&
!args[0]->ToObject()->GetHiddenValue(
- String::New(kArrayBufferMarkerPropName)).IsEmpty()) {
+ Symbols::ArrayBufferMarkerPropName).IsEmpty()) {
// Construct from ArrayBuffer.
buffer = args[0]->ToObject();
int32_t bufferLength =
- convertToUint(buffer->Get(String::New("byteLength")), &try_catch);
+ convertToUint(buffer->Get(Symbols::byteLength), &try_catch);
if (try_catch.HasCaught()) return try_catch.ReThrow();
if (args.Length() < 2 || args[1]->IsUndefined()) {
@@ -457,11 +491,10 @@
byteOffset = convertToUint(args[1], &try_catch);
if (try_catch.HasCaught()) return try_catch.ReThrow();
if (byteOffset > bufferLength) {
- return ThrowException(String::New("byteOffset out of bounds"));
+ return Throw("byteOffset out of bounds");
}
if (byteOffset % element_size != 0) {
- return ThrowException(
- String::New("byteOffset must be multiple of element size"));
+ return Throw("byteOffset must be multiple of element size");
}
}
@@ -469,23 +502,22 @@
byteLength = bufferLength - byteOffset;
length = byteLength / element_size;
if (byteLength % element_size != 0) {
- return ThrowException(
- String::New("buffer size must be multiple of element size"));
+ return Throw("buffer size must be multiple of element size");
}
} else {
length = convertToUint(args[2], &try_catch);
if (try_catch.HasCaught()) return try_catch.ReThrow();
byteLength = length * element_size;
if (byteOffset + byteLength > bufferLength) {
- return ThrowException(String::New("length out of bounds"));
+ return Throw("length out of bounds");
}
}
} else {
if (args[0]->IsObject() &&
- args[0]->ToObject()->Has(String::New("length"))) {
+ args[0]->ToObject()->Has(Symbols::length)) {
// Construct from array.
length = convertToUint(
- args[0]->ToObject()->Get(String::New("length")), &try_catch);
+ args[0]->ToObject()->Get(Symbols::length), &try_catch);
if (try_catch.HasCaught()) return try_catch.ReThrow();
init_from_array = true;
} else {
@@ -497,7 +529,7 @@
byteOffset = 0;
Handle<Object> global = Context::GetCurrent()->Global();
- Handle<Value> array_buffer = global->Get(String::New("ArrayBuffer"));
+ Handle<Value> array_buffer = global->Get(Symbols::ArrayBuffer);
ASSERT(!try_catch.HasCaught() && array_buffer->IsFunction());
Handle<Value> buffer_args[] = { Uint32::New(byteLength) };
Handle<Value> result =
Handle<Function>::Cast(array_buffer)->NewInstance(
@@ -522,25 +554,22 @@
TryCatch try_catch;
if (!args.This()->IsObject()) {
- return ThrowException(
- String::New("'slice' invoked on non-object receiver"));
+ return Throw("'slice' invoked on non-object receiver");
}
Local<Object> self = args.This();
Local<Value> marker =
- self->GetHiddenValue(String::New(kArrayBufferMarkerPropName));
+ self->GetHiddenValue(Symbols::ArrayBufferMarkerPropName);
if (marker.IsEmpty()) {
- return ThrowException(
- String::New("'slice' invoked on wrong receiver type"));
+ return Throw("'slice' invoked on wrong receiver type");
}
int32_t length =
- convertToUint(self->Get(String::New("byteLength")), &try_catch);
+ convertToUint(self->Get(Symbols::byteLength), &try_catch);
if (try_catch.HasCaught()) return try_catch.ReThrow();
if (args.Length() == 0) {
- return ThrowException(
- String::New("'slice' must have at least one argument"));
+ return Throw("'slice' must have at least one argument");
}
int32_t begin = convertToInt(args[0], &try_catch);
if (try_catch.HasCaught()) return try_catch.ReThrow();
@@ -579,32 +608,29 @@
TryCatch try_catch;
if (!args.This()->IsObject()) {
- return ThrowException(
- String::New("'subarray' invoked on non-object receiver"));
+ return Throw("'subarray' invoked on non-object receiver");
}
Local<Object> self = args.This();
- Local<Value> marker =
self->GetHiddenValue(String::New(kArrayMarkerPropName));
+ Local<Value> marker = self->GetHiddenValue(Symbols::ArrayMarkerPropName);
if (marker.IsEmpty()) {
- return ThrowException(
- String::New("'subarray' invoked on wrong receiver type"));
+ return Throw("'subarray' invoked on wrong receiver type");
}
- Handle<Object> buffer = self->Get(String::New("buffer"))->ToObject();
+ Handle<Object> buffer = self->Get(Symbols::buffer)->ToObject();
if (try_catch.HasCaught()) return try_catch.ReThrow();
int32_t length =
- convertToUint(self->Get(String::New("length")), &try_catch);
+ convertToUint(self->Get(Symbols::length), &try_catch);
if (try_catch.HasCaught()) return try_catch.ReThrow();
int32_t byteOffset =
- convertToUint(self->Get(String::New("byteOffset")), &try_catch);
+ convertToUint(self->Get(Symbols::byteOffset), &try_catch);
if (try_catch.HasCaught()) return try_catch.ReThrow();
int32_t element_size =
- convertToUint(self->Get(String::New("BYTES_PER_ELEMENT")),
&try_catch);
+ convertToUint(self->Get(Symbols::BYTES_PER_ELEMENT), &try_catch);
if (try_catch.HasCaught()) return try_catch.ReThrow();
if (args.Length() == 0) {
- return ThrowException(
- String::New("'subarray' must have at least one argument"));
+ return Throw("'subarray' must have at least one argument");
}
int32_t begin = convertToInt(args[0], &try_catch);
if (try_catch.HasCaught()) return try_catch.ReThrow();
@@ -639,35 +665,31 @@
TryCatch try_catch;
if (!args.This()->IsObject()) {
- return ThrowException(
- String::New("'set' invoked on non-object receiver"));
+ return Throw("'set' invoked on non-object receiver");
}
Local<Object> self = args.This();
- Local<Value> marker =
self->GetHiddenValue(String::New(kArrayMarkerPropName));
+ Local<Value> marker = self->GetHiddenValue(Symbols::ArrayMarkerPropName);
if (marker.IsEmpty()) {
- return ThrowException(
- String::New("'set' invoked on wrong receiver type"));
+ return Throw("'set' invoked on wrong receiver type");
}
int32_t length =
- convertToUint(self->Get(String::New("length")), &try_catch);
+ convertToUint(self->Get(Symbols::length), &try_catch);
if (try_catch.HasCaught()) return try_catch.ReThrow();
int32_t element_size =
- convertToUint(self->Get(String::New("BYTES_PER_ELEMENT")),
&try_catch);
+ convertToUint(self->Get(Symbols::BYTES_PER_ELEMENT), &try_catch);
if (try_catch.HasCaught()) return try_catch.ReThrow();
if (args.Length() == 0) {
- return ThrowException(
- String::New("'set' must have at least one argument"));
+ return Throw("'set' must have at least one argument");
}
if (!args[0]->IsObject() ||
- !args[0]->ToObject()->Has(String::New("length"))) {
- return ThrowException(
- String::New("'set' invoked with non-array argument"));
+ !args[0]->ToObject()->Has(Symbols::length)) {
+ return Throw("'set' invoked with non-array argument");
}
Handle<Object> source = args[0]->ToObject();
int32_t source_length =
- convertToUint(source->Get(String::New("length")), &try_catch);
+ convertToUint(source->Get(Symbols::length), &try_catch);
if (try_catch.HasCaught()) return try_catch.ReThrow();
int32_t offset;
@@ -678,31 +700,31 @@
if (try_catch.HasCaught()) return try_catch.ReThrow();
}
if (offset + source_length > length) {
- return ThrowException(String::New("offset or source length out of
bounds"));
+ return Throw("offset or source length out of bounds");
}
int32_t source_element_size;
- if (source->GetHiddenValue(String::New(kArrayMarkerPropName)).IsEmpty())
{
+ if (source->GetHiddenValue(Symbols::ArrayMarkerPropName).IsEmpty()) {
source_element_size = 0;
} else {
source_element_size =
- convertToUint(source->Get(String::New("BYTES_PER_ELEMENT")),
&try_catch);
+ convertToUint(source->Get(Symbols::BYTES_PER_ELEMENT), &try_catch);
if (try_catch.HasCaught()) return try_catch.ReThrow();
}
if (element_size == source_element_size &&
self->GetConstructor()->StrictEquals(source->GetConstructor())) {
// Use memmove on the array buffers.
- Handle<Object> buffer = self->Get(String::New("buffer"))->ToObject();
+ Handle<Object> buffer = self->Get(Symbols::buffer)->ToObject();
if (try_catch.HasCaught()) return try_catch.ReThrow();
Handle<Object> source_buffer =
- source->Get(String::New("buffer"))->ToObject();
+ source->Get(Symbols::buffer)->ToObject();
if (try_catch.HasCaught()) return try_catch.ReThrow();
int32_t byteOffset =
- convertToUint(self->Get(String::New("byteOffset")), &try_catch);
+ convertToUint(self->Get(Symbols::byteOffset), &try_catch);
if (try_catch.HasCaught()) return try_catch.ReThrow();
int32_t source_byteOffset =
- convertToUint(source->Get(String::New("byteOffset")), &try_catch);
+ convertToUint(source->Get(Symbols::byteOffset), &try_catch);
if (try_catch.HasCaught()) return try_catch.ReThrow();
uint8_t* dest = byteOffset + offset * element_size +
static_cast<uint8_t*>(
@@ -718,10 +740,10 @@
}
} else {
// Need to copy element-wise to make the right conversions.
- Handle<Object> buffer = self->Get(String::New("buffer"))->ToObject();
+ Handle<Object> buffer = self->Get(Symbols::buffer)->ToObject();
if (try_catch.HasCaught()) return try_catch.ReThrow();
Handle<Object> source_buffer =
- source->Get(String::New("buffer"))->ToObject();
+ source->Get(Symbols::buffer)->ToObject();
if (try_catch.HasCaught()) return try_catch.ReThrow();
if (buffer->StrictEquals(source_buffer)) {
@@ -729,10 +751,10 @@
// This gets a bit tricky in the case of different element sizes
// (which, of course, is extremely unlikely to ever occur in
practice).
int32_t byteOffset =
- convertToUint(self->Get(String::New("byteOffset")), &try_catch);
+ convertToUint(self->Get(Symbols::byteOffset), &try_catch);
if (try_catch.HasCaught()) return try_catch.ReThrow();
int32_t source_byteOffset =
- convertToUint(source->Get(String::New("byteOffset")),
&try_catch);
+ convertToUint(source->Get(Symbols::byteOffset), &try_catch);
if (try_catch.HasCaught()) return try_catch.ReThrow();
// Copy as much as we can from left to right.
@@ -779,7 +801,7 @@
void Shell::ExternalArrayWeakCallback(Persistent<Value> object, void*
data) {
HandleScope scope;
int32_t length =
- object->ToObject()->Get(String::New("byteLength"))->Uint32Value();
+ object->ToObject()->Get(Symbols::byteLength)->Uint32Value();
V8::AdjustAmountOfExternalAllocatedMemory(-length);
delete[] static_cast<uint8_t*>(data);
object.Dispose();
@@ -1165,7 +1187,7 @@
// Bind the handlers for external arrays.
PropertyAttribute attr =
static_cast<PropertyAttribute>(ReadOnly | DontDelete);
- global_template->Set(String::New("ArrayBuffer"),
+ global_template->Set(Symbols::ArrayBuffer,
CreateArrayBufferTemplate(ArrayBuffer), attr);
global_template->Set(String::New("Int8Array"),
CreateArrayTemplate(Int8Array), attr);
@@ -1223,6 +1245,8 @@
V8::SetAddHistogramSampleFunction(AddHistogramSample);
}
#endif // V8_SHARED
+
+ Symbols::Initialize();
if (options.test_shell) return;
#ifndef V8_SHARED
@@ -1381,15 +1405,15 @@
String::Utf8Value filename(args[0]);
int length;
if (*filename == NULL) {
- return ThrowException(String::New("Error loading file"));
+ return Throw("Error loading file");
}
uint8_t* data = reinterpret_cast<uint8_t*>(ReadChars(*filename,
&length));
if (data == NULL) {
- return ThrowException(String::New("Error reading file"));
+ return Throw("Error reading file");
}
Handle<Object> buffer = Object::New();
- buffer->SetHiddenValue(String::New(kArrayBufferMarkerPropName), True());
+ buffer->SetHiddenValue(Symbols::ArrayBufferMarkerPropName, True());
Persistent<Object> persistent_buffer = Persistent<Object>::New(buffer);
persistent_buffer.MakeWeak(data, ExternalArrayWeakCallback);
persistent_buffer.MarkIndependent();
@@ -1397,7 +1421,7 @@
buffer->SetIndexedPropertiesToExternalArrayData(
data, kExternalUnsignedByteArray, length);
- buffer->Set(String::New("byteLength"),
+ buffer->Set(Symbols::byteLength,
Int32::New(static_cast<int32_t>(length)), ReadOnly);
return buffer;
}
@@ -1895,6 +1919,7 @@
RunShell();
}
+ Symbols::TearDown();
V8::Dispose();
#ifndef V8_SHARED
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev