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

Unified Diff: runtime/lib/growable_array.dart

Issue 1701213002: Shrink growable arrays (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 10 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 | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/lib/growable_array.dart
diff --git a/runtime/lib/growable_array.dart b/runtime/lib/growable_array.dart
index 39b4522c72711d9e3cb1cffd56f875dd5eec54f8..75a17643bf89b78c64732fb7f0d6ccaa5807733d 100644
--- a/runtime/lib/growable_array.dart
+++ b/runtime/lib/growable_array.dart
@@ -113,13 +113,24 @@ class _GrowableList<T> extends ListBase<T> {
factory _GrowableList.withData(_List data)
native "GrowableList_allocate";
- int get length native "GrowableList_getLength";
-
int get _capacity native "GrowableList_getCapacity";
+ int get length native "GrowableList_getLength";
+
void set length(int new_length) {
if (new_length > _capacity) {
Ivan Posva 2016/02/16 23:54:54 Judging from Srdjan's comment we should not interm
Cutch 2016/02/17 21:12:54 Done.
_grow(new_length);
+ _setLength(new_length);
+ return;
+ }
+ // We are shrinking. Pick the method which has fewer writes.
+ // In the shrink-to-fit path, we write 2 * |new_length| words
+ // (null fill + copy).
Ivan Posva 2016/02/16 23:54:54 How about "(null init + copy)" and "(null overwrit
Cutch 2016/02/17 21:12:54 Done.
+ // In the non-shrink-to-fit path, we write |length - new_length| words
+ // (null fill).
+ final bool shouldShrinkToFit = (2 * new_length) < (length - new_length);
Ivan Posva 2016/02/16 23:54:54 Which would mean here (new_capacity + new_length)
Cutch 2016/02/17 21:12:54 Done.
+ if (shouldShrinkToFit) {
+ _shrink(new_length);
} else {
for (int i = new_length; i < length; i++) {
this[i] = null;
@@ -225,6 +236,14 @@ class _GrowableList<T> extends ListBase<T> {
_setData(new_data);
}
+ void _shrink(int new_length) {
srdjan 2016/02/16 23:45:29 if (new_length == _capacity) return
Ivan Posva 2016/02/16 23:54:54 I don't think this would work here as the code abo
Cutch 2016/02/17 21:12:54 Acknowledged.
Cutch 2016/02/17 21:12:54 Acknowledged.
+ var new_data = new _List(new_length == 0 ? _kDefaultCapacity : new_length);
+ for (int i = 0; i < new_length; i++) {
+ new_data[i] = this[i];
+ }
+ _setData(new_data);
+ }
+
// Iterable interface.
void forEach(f(T element)) {
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698