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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 class _GrowableList<T> extends ListBase<T> { 5 class _GrowableList<T> extends ListBase<T> {
6 6
7 void insert(int index, T element) { 7 void insert(int index, T element) {
8 if ((index < 0) || (index > length)) { 8 if ((index < 0) || (index > length)) {
9 throw new RangeError.range(index, 0, length); 9 throw new RangeError.range(index, 0, length);
10 } 10 }
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 106
107 factory _GrowableList.from(Iterable<T> other) { 107 factory _GrowableList.from(Iterable<T> other) {
108 List<T> result = new _GrowableList<T>(); 108 List<T> result = new _GrowableList<T>();
109 result.addAll(other); 109 result.addAll(other);
110 return result; 110 return result;
111 } 111 }
112 112
113 factory _GrowableList.withData(_List data) 113 factory _GrowableList.withData(_List data)
114 native "GrowableList_allocate"; 114 native "GrowableList_allocate";
115 115
116 int get _capacity native "GrowableList_getCapacity";
117
116 int get length native "GrowableList_getLength"; 118 int get length native "GrowableList_getLength";
117 119
118 int get _capacity native "GrowableList_getCapacity";
119
120 void set length(int new_length) { 120 void set length(int new_length) {
121 if (new_length > _capacity) { 121 int new_capacity = (new_length == 0) ? _kDefaultCapacity : new_length;
122 _grow(new_length); 122 if (new_capacity > _capacity) {
123 _grow(new_capacity);
124 _setLength(new_length);
125 return;
126 }
127 // We are shrinking. Pick the method which has fewer writes.
128 // In the shrink-to-fit path, we write |new_capacity + new_length| words
129 // (null init + copy).
130 // In the non-shrink-to-fit path, we write |length - new_length| words
131 // (null overwrite).
132 final bool shouldShrinkToFit =
133 (new_capacity + new_length) < (length - new_length);
134 if (shouldShrinkToFit) {
135 _shrink(new_capacity, new_length);
123 } else { 136 } else {
124 for (int i = new_length; i < length; i++) { 137 for (int i = new_length; i < length; i++) {
125 this[i] = null; 138 this[i] = null;
126 } 139 }
127 } 140 }
128 _setLength(new_length); 141 _setLength(new_length);
129 } 142 }
130 143
131 void _setLength(int new_length) native "GrowableList_setLength"; 144 void _setLength(int new_length) native "GrowableList_setLength";
132 145
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 if (length > 0) return this[length - 1]; 223 if (length > 0) return this[length - 1];
211 throw IterableElementError.noElement(); 224 throw IterableElementError.noElement();
212 } 225 }
213 226
214 T get single { 227 T get single {
215 if (length == 1) return this[0]; 228 if (length == 1) return this[0];
216 if (length == 0) throw IterableElementError.noElement(); 229 if (length == 0) throw IterableElementError.noElement();
217 throw IterableElementError.tooMany();; 230 throw IterableElementError.tooMany();;
218 } 231 }
219 232
220 void _grow(int new_length) { 233 void _grow(int new_capacity) {
221 var new_data = new _List(new_length); 234 var new_data = new _List(new_capacity);
222 for (int i = 0; i < length; i++) { 235 for (int i = 0; i < length; i++) {
223 new_data[i] = this[i]; 236 new_data[i] = this[i];
224 } 237 }
225 _setData(new_data); 238 _setData(new_data);
226 } 239 }
227 240
241 void _shrink(int new_capacity, int new_length) {
242 var new_data = new _List(new_capacity);
243 for (int i = 0; i < new_length; i++) {
244 new_data[i] = this[i];
245 }
246 _setData(new_data);
247 }
248
228 // Iterable interface. 249 // Iterable interface.
229 250
230 void forEach(f(T element)) { 251 void forEach(f(T element)) {
231 int initialLength = length; 252 int initialLength = length;
232 for (int i = 0; i < length; i++) { 253 for (int i = 0; i < length; i++) {
233 f(this[i]); 254 f(this[i]);
234 if (length != initialLength) throw new ConcurrentModificationError(this); 255 if (length != initialLength) throw new ConcurrentModificationError(this);
235 } 256 }
236 } 257 }
237 258
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 result._setLength(length); 356 result._setLength(length);
336 return result; 357 return result;
337 } 358 }
338 return growable ? <T>[] : new List<T>(0); 359 return growable ? <T>[] : new List<T>(0);
339 } 360 }
340 361
341 Set<T> toSet() { 362 Set<T> toSet() {
342 return new Set<T>.from(this); 363 return new Set<T>.from(this);
343 } 364 }
344 } 365 }
OLDNEW
« 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