- Optimized copying of FixedArray.
authorbak@chromium.org <bak@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Mon, 20 Oct 2008 06:35:28 +0000 (06:35 +0000)
committerbak@chromium.org <bak@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Mon, 20 Oct 2008 06:35:28 +0000 (06:35 +0000)
Review URL: http://codereview.chromium.org/7516

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@524 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

src/heap.cc
src/heap.h
src/objects-inl.h
src/objects.cc
src/objects.h

index 06a981f..8ad7e2c 100644 (file)
@@ -2034,6 +2034,43 @@ Object* Heap::AllocateEmptyFixedArray() {
 }
 
 
+Object* Heap::AllocateRawFixedArray(int length) {
+  // Allocate the raw data for a fixed array.
+  int size = FixedArray::SizeFor(length);
+  return (size > MaxHeapObjectSize())
+      ? lo_space_->AllocateRawFixedArray(size)
+      : new_space_.AllocateRaw(size);
+}
+
+
+Object* Heap::CopyFixedArray(FixedArray* src) {
+  int len = src->length();
+  Object* obj = Heap::AllocateRawFixedArray(len);
+  if (obj->IsFailure()) return obj;
+  HeapObject::cast(obj)->set_map(src->map());
+  FixedArray* result = FixedArray::cast(obj);
+  result->set_length(len);
+  FixedArray::WriteBarrierMode mode = result->GetWriteBarrierMode();
+  // Copy the content
+  for (int i = 0; i < len; i++) result->set(i, src->get(i), mode);
+  return result;
+}
+
+
+Object* Heap::AllocateFixedArray(int length) {
+  Object* result = AllocateRawFixedArray(length);
+  if (!result->IsFailure()) {
+    // Initialize header.
+    reinterpret_cast<Array*>(result)->set_map(fixed_array_map());
+    FixedArray* array = FixedArray::cast(result);
+    array->set_length(length);
+    // Initialize body.
+    for (int index = 0; index < length; index++) array->set_undefined(index);
+  }
+  return result;
+}
+
+
 Object* Heap::AllocateFixedArray(int length, PretenureFlag pretenure) {
   ASSERT(empty_fixed_array()->IsFixedArray());
   if (length == 0) return empty_fixed_array();
@@ -2060,18 +2097,16 @@ Object* Heap::AllocateFixedArray(int length, PretenureFlag pretenure) {
 
 Object* Heap::AllocateFixedArrayWithHoles(int length) {
   if (length == 0) return empty_fixed_array();
-  int size = FixedArray::SizeFor(length);
-  Object* result = size > MaxHeapObjectSize()
-      ? lo_space_->AllocateRawFixedArray(size)
-      : AllocateRaw(size, NEW_SPACE);
-  if (result->IsFailure()) return result;
-
-  // Initialize the object.
-  reinterpret_cast<Array*>(result)->set_map(fixed_array_map());
-  FixedArray* array = FixedArray::cast(result);
-  array->set_length(length);
-  for (int index = 0; index < length; index++) array->set_the_hole(index);
-  return array;
+  Object* result = AllocateRawFixedArray(length);
+  if (!result->IsFailure()) {
+    // Initialize header.
+    reinterpret_cast<Array*>(result)->set_map(fixed_array_map());
+    FixedArray* array = FixedArray::cast(result);
+    array->set_length(length);
+    // Initialize body.
+    for (int index = 0; index < length; index++) array->set_the_hole(index);
+  }
+  return result;
 }
 
 
index 0338707..7648868 100644 (file)
@@ -379,9 +379,13 @@ class Heap : public AllStatic {
   // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
   // failed.
   // Please note this does not perform a garbage collection.
-  static Object* AllocateFixedArray(int length,
-                                    PretenureFlag pretenure = NOT_TENURED);
+  static Object* AllocateFixedArray(int length, PretenureFlag pretenure);
+  // Allocate uninitialized, non-tenured fixed array with length elements.
+  static Object* AllocateFixedArray(int length);
 
+  // Make a copy of src and return it. Returns
+  // Failure::RetryAfterGC(requested_bytes, space) if the allocation failed.
+  static Object* CopyFixedArray(FixedArray* src);
 
   // Allocates a fixed array initialized with the hole values.
   // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
@@ -814,6 +818,8 @@ class Heap : public AllStatic {
   // (since both AllocateRaw and AllocateRawMap are inlined).
   static inline Object* AllocateRawMap(int size_in_bytes);
 
+  // Allocate unitialized fixed array (pretenure == NON_TENURE).
+  static Object* AllocateRawFixedArray(int length);
 
   // Initializes a JSObject based on its map.
   static void InitializeJSObjectFromMap(JSObject* obj,
index ce2da12..c7d93e3 100644 (file)
@@ -2295,6 +2295,12 @@ void JSArray::SetContent(FixedArray* storage) {
 }
 
 
+Object* FixedArray::Copy() {
+  if (length() == 0) return this;
+  return Heap::CopyFixedArray(this);
+}
+
+
 #undef CAST_ACCESSOR
 #undef INT_ACCESSORS
 #undef SMI_ACCESSORS
index 8cf5840..bfcedb2 100644 (file)
@@ -889,13 +889,12 @@ void HeapNumber::HeapNumberPrint(StringStream* accumulator) {
 
 String* JSObject::class_name() {
   if (IsJSFunction()) return Heap::function_class_symbol();
-  // If the constructor is not present "Object" is returned.
-  String* result = Heap::Object_symbol();
   if (map()->constructor()->IsJSFunction()) {
     JSFunction* constructor = JSFunction::cast(map()->constructor());
     return String::cast(constructor->shared()->instance_class_name());
   }
-  return result;
+  // If the constructor is not present, return "Object".
+  return Heap::Object_symbol();
 }
 
 
@@ -2538,21 +2537,6 @@ Object* FixedArray::UnionOfKeys(FixedArray* other) {
 }
 
 
-Object* FixedArray::Copy() {
-  int len = length();
-  if (len == 0) return this;
-  Object* obj = Heap::AllocateFixedArray(len);
-  if (obj->IsFailure()) return obj;
-  FixedArray* result = FixedArray::cast(obj);
-  WriteBarrierMode mode = result->GetWriteBarrierMode();
-  // Copy the content
-  for (int i = 0; i < len; i++) {
-    result->set(i, get(i), mode);
-  }
-  result->set_map(map());
-  return result;
-}
-
 Object* FixedArray::CopySize(int new_length) {
   if (new_length == 0) return Heap::empty_fixed_array();
   Object* obj = Heap::AllocateFixedArray(new_length);
index 3f0150e..9437844 100644 (file)
@@ -1489,7 +1489,7 @@ class FixedArray: public Array {
   inline WriteBarrierMode GetWriteBarrierMode();
 
   // Copy operations.
-  Object* Copy();
+  inline Object* Copy();
   Object* CopySize(int new_length);
 
   // Add the elements of a JSArray to this FixedArray.