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

Side by Side Diff: runtime/lib/string_buffer_patch.dart

Issue 25087006: Improve performance of string buffer by modifying concatAll native to allow growable array and an i… (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 2 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 | Annotate | Revision Log
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 patch class StringBuffer { 5 patch class StringBuffer {
6 static const int _BUFFER_SIZE = 64; 6 static const int _BUFFER_SIZE = 64;
7 static const int _PARTS_TO_COMPACT = 128; 7 static const int _PARTS_TO_COMPACT = 128;
8 static const int _PARTS_TO_COMPACT_SIZE_LIMIT = _PARTS_TO_COMPACT * 8; 8 static const int _PARTS_TO_COMPACT_SIZE_LIMIT = _PARTS_TO_COMPACT * 8;
9 9
10 /** 10 /**
11 * When strings are written to the string buffer, we add them to a 11 * When strings are written to the string buffer, we add them to a
12 * list of string parts. 12 * list of string parts.
13 */ 13 */
14 List _parts = null; 14 List<String> _parts = null;
15 15
16 /** 16 /**
17 * Total number of code units in the string parts. Does not include 17 * Total number of code units in the string parts. Does not include
18 * the code units added to the buffer. 18 * the code units added to the buffer.
19 */ 19 */
20 int _partsCodeUnits = 0; 20 int _partsCodeUnits = 0;
21 21
22 /** 22 /**
23 * To preserve memory, we sometimes compact the parts. This combines 23 * To preserve memory, we sometimes compact the parts. This combines
24 * several smaller parts into a single larger part to cut down on the 24 * several smaller parts into a single larger part to cut down on the
25 * cost that comes from the per-object memory overhead. We keep track 25 * cost that comes from the per-object memory overhead. We keep track
26 * of the last index where we ended our compaction and the number of 26 * of the last index where we ended our compaction and the number of
27 * code units added since the last compaction. 27 * code units added since the last compaction.
28 */ 28 */
29 int _partsCompactionIndex = 0; 29 int _partsCompactionIndex = 0;
30 int _partsCodeUnitsSinceCompaction = 0; 30 int _partsCodeUnitsSinceCompaction = 0;
31 _ObjectArray _partsCompactionArray = null;
32 31
33 /** 32 /**
34 * The buffer is used to build up a string from code units. It is 33 * The buffer is used to build up a string from code units. It is
35 * used when writing short strings or individul char codes to the 34 * used when writing short strings or individul char codes to the
36 * buffer. The buffer is allocated on demand. 35 * buffer. The buffer is allocated on demand.
37 */ 36 */
38 Uint16List _buffer = null; 37 Uint16List _buffer = null;
39 int _bufferPosition = 0; 38 int _bufferPosition = 0;
40 39
41 /** 40 /**
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 93
95 /** Makes the buffer empty. */ 94 /** Makes the buffer empty. */
96 /* patch */ void clear() { 95 /* patch */ void clear() {
97 _parts = null; 96 _parts = null;
98 _partsCodeUnits = _bufferPosition = _bufferCodeUnitMagnitude = 0; 97 _partsCodeUnits = _bufferPosition = _bufferCodeUnitMagnitude = 0;
99 } 98 }
100 99
101 /** Returns the contents of buffer as a string. */ 100 /** Returns the contents of buffer as a string. */
102 /* patch */ String toString() { 101 /* patch */ String toString() {
103 _consumeBuffer(); 102 _consumeBuffer();
104 if (_partsCodeUnits == 0) return ""; 103 return (_partsCodeUnits == 0) ?
105 104 "" :
106 // TODO(kasperl): It would be nice if concatAllNative would 105 _StringBase._concatAllNative(_parts, 0, _parts.length);
107 // allow me to pass in a grownable array directly, but for
108 // now we have to copy the contents to a non-growable array.
109 int length = _parts.length;
110 _ObjectArray array = new _ObjectArray(length);
111 for (int i = 0; i < length; i++) array[i] = _parts[i];
112 return _StringBase._concatAllNative(array);
113 } 106 }
114 107
115 /** Ensures that the buffer has enough capacity to add n code units. */ 108 /** Ensures that the buffer has enough capacity to add n code units. */
116 void _ensureCapacity(int n) { 109 void _ensureCapacity(int n) {
117 if (_buffer == null) { 110 if (_buffer == null) {
118 _buffer = new Uint16List(_BUFFER_SIZE); 111 _buffer = new Uint16List(_BUFFER_SIZE);
119 } else if (_bufferPosition + n > _buffer.length) { 112 } else if (_bufferPosition + n > _buffer.length) {
120 _consumeBuffer(); 113 _consumeBuffer();
121 } 114 }
122 } 115 }
(...skipping 30 matching lines...) Expand all
153 } 146 }
154 } 147 }
155 } 148 }
156 149
157 /** 150 /**
158 * Compacts the last N parts if their average size allows us to save a 151 * Compacts the last N parts if their average size allows us to save a
159 * lot of memory by turning them all into a single part. 152 * lot of memory by turning them all into a single part.
160 */ 153 */
161 void _compact() { 154 void _compact() {
162 if (_partsCodeUnitsSinceCompaction < _PARTS_TO_COMPACT_SIZE_LIMIT) { 155 if (_partsCodeUnitsSinceCompaction < _PARTS_TO_COMPACT_SIZE_LIMIT) {
163 if (_partsCompactionArray == null) { 156 String compacted = _StringBase._concatAllNative(
164 _partsCompactionArray = new _ObjectArray(_PARTS_TO_COMPACT); 157 _parts,
165 } 158 _partsCompactionIndex, // Start
166 for (int i = 0; i < _PARTS_TO_COMPACT; i++) { 159 _partsCompactionIndex + _PARTS_TO_COMPACT // End
167 _partsCompactionArray[i] = _parts[i + _partsCompactionIndex]; 160 );
168 }
169 String compacted = _StringBase._concatAllNative(_partsCompactionArray);
170 _parts.length = _parts.length - _PARTS_TO_COMPACT; 161 _parts.length = _parts.length - _PARTS_TO_COMPACT;
171 _parts.add(compacted); 162 _parts.add(compacted);
172 for (int i = 0; i < _PARTS_TO_COMPACT; i++) {
173 _partsCompactionArray[i] = null;
174 }
175 } 163 }
176 _partsCodeUnitsSinceCompaction = 0; 164 _partsCodeUnitsSinceCompaction = 0;
177 _partsCompactionIndex = _parts.length; 165 _partsCompactionIndex = _parts.length;
178 } 166 }
179 167
180 /** 168 /**
181 * Create a [String] from the UFT-16 code units in buffer. 169 * Create a [String] from the UFT-16 code units in buffer.
182 */ 170 */
183 static String _create(Uint16List buffer, int length, bool isLatin1) 171 static String _create(Uint16List buffer, int length, bool isLatin1)
184 native "StringBuffer_createStringFromUint16Array"; 172 native "StringBuffer_createStringFromUint16Array";
185 } 173 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698