Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1146)

Unified Diff: src/objects.cc

Issue 7237004: Trim fast elements tail on significant length decreases. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | src/runtime.cc » ('j') | src/runtime.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/objects.cc
diff --git a/src/objects.cc b/src/objects.cc
index 6c9f52b2df89839306e30b8f925f87412fc111f4..cff87bdb471395cf498e84246334fa7dda44c0cb 100644
--- a/src/objects.cc
+++ b/src/objects.cc
@@ -7507,12 +7507,24 @@ MaybeObject* JSObject::SetElementsLength(Object* len) {
{ MaybeObject* maybe_obj = EnsureWritableFastElements();
if (!maybe_obj->ToObject(&obj)) return maybe_obj;
}
- int old_length = FastD2I(JSArray::cast(this)->length()->Number());
- // NOTE: We may be able to optimize this by removing the
- // last part of the elements backing storage array and
- // setting the capacity to the new size.
- for (int i = value; i < old_length; i++) {
- FixedArray::cast(elements())->set_the_hole(i);
+ FixedArray* fast_elements = FixedArray::cast(elements());
+ if (2 * value <= old_capacity) {
+ // If more than half the elements won't be used, trim the array.
+ if (value == 0) {
+ initialize_elements();
+ } else {
+ fast_elements->set_length(value);
+ Address filler_start = fast_elements->address() +
+ FixedArray::OffsetOfElementAt(value);
+ int filler_size = (old_capacity - value) * kPointerSize;
+ GetHeap()->CreateFillerObjectAt(filler_start, filler_size);
+ }
+ } else {
+ // Otherwise, fill the unused tail with holes.
+ int old_length = FastD2I(JSArray::cast(this)->length()->Number());
+ for (int i = value; i < old_length; i++) {
+ fast_elements->set_the_hole(i);
+ }
}
JSArray::cast(this)->set_length(Smi::cast(smi_length));
}
« no previous file with comments | « no previous file | src/runtime.cc » ('j') | src/runtime.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698