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

Side by Side Diff: pkg/analyzer/lib/src/generated/source.dart

Issue 692093004: Optimize LineInfo.getLocation(). (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 1 month 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
« 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) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 // This code was auto-generated, is not intended to be edited, and is subject to 5 // This code was auto-generated, is not intended to be edited, and is subject to
6 // significant change. Please see the README file for more information. 6 // significant change. Please see the README file for more information.
7 7
8 library engine.source; 8 library engine.source;
9 9
10 import 'dart:collection'; 10 import 'dart:collection';
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 * Instances of the class `LineInfo` encapsulate information about line and colu mn information 149 * Instances of the class `LineInfo` encapsulate information about line and colu mn information
150 * within a source file. 150 * within a source file.
151 */ 151 */
152 class LineInfo { 152 class LineInfo {
153 /** 153 /**
154 * An array containing the offsets of the first character of each line in the source code. 154 * An array containing the offsets of the first character of each line in the source code.
155 */ 155 */
156 final List<int> _lineStarts; 156 final List<int> _lineStarts;
157 157
158 /** 158 /**
159 * The zero-based [_lineStarts] index resulting from the last call to
160 * [getLocation].
161 */
162 int _previousLine = 0;
163
164 /**
159 * Initialize a newly created set of line information to represent the data en coded in the given 165 * Initialize a newly created set of line information to represent the data en coded in the given
160 * array. 166 * array.
161 * 167 *
162 * @param lineStarts the offsets of the first character of each line in the so urce code 168 * @param lineStarts the offsets of the first character of each line in the so urce code
163 */ 169 */
164 LineInfo(this._lineStarts) { 170 LineInfo(this._lineStarts) {
165 if (_lineStarts == null) { 171 if (_lineStarts == null) {
166 throw new IllegalArgumentException("lineStarts must be non-null"); 172 throw new IllegalArgumentException("lineStarts must be non-null");
167 } else if (_lineStarts.length < 1) { 173 } else if (_lineStarts.length < 1) {
168 throw new IllegalArgumentException("lineStarts must be non-empty"); 174 throw new IllegalArgumentException("lineStarts must be non-empty");
169 } 175 }
170 } 176 }
171 177
172 /** 178 /**
173 * Return the location information for the character at the given offset. 179 * Return the location information for the character at the given offset.
174 * 180 *
175 * @param offset the offset of the character for which location information is to be returned 181 * @param offset the offset of the character for which location information is to be returned
176 * @return the location information for the character at the given offset 182 * @return the location information for the character at the given offset
177 */ 183 */
178 LineInfo_Location getLocation(int offset) { 184 LineInfo_Location getLocation(int offset) {
179 int lineCount = _lineStarts.length; 185 var min = 0;
180 for (int i = 1; i < lineCount; i++) { 186 var max = _lineStarts.length - 1;
181 if (offset < _lineStarts[i]) { 187
182 return new LineInfo_Location(i, offset - _lineStarts[i - 1] + 1); 188 // Subsequent calls to [getLocation] are often for offsets near each other.
189 // To take advantage of that, we cache the index of the line start we found
190 // when this was last called. If the current offset is on that line or
191 // later, we'll skip those early indices completely when searching.
192 if (offset >= _lineStarts[_previousLine]) {
193 min = _previousLine;
194
195 // Before kicking off a full binary search, do a quick check here to see
196 // if the new offset is on that exact line.
197 if (min == _lineStarts.length - 1 || offset < _lineStarts[min + 1]) {
198 return new LineInfo_Location(min + 1, offset - _lineStarts[min] + 1);
183 } 199 }
184 } 200 }
185 return new LineInfo_Location(lineCount, offset - _lineStarts[lineCount - 1] + 1); 201
202 // Binary search to fine the line containing this offset.
203 while (min < max) {
204 var midpoint = (max - min + 1) ~/ 2 + min;
205
206 if (_lineStarts[midpoint] > offset) {
207 max = midpoint - 1;
208 } else {
209 min = midpoint;
210 }
211 }
212
213 _previousLine = min;
214
215 return new LineInfo_Location(min + 1, offset - _lineStarts[min] + 1);
186 } 216 }
187 } 217 }
188 218
189 /** 219 /**
190 * Instances of the class `Location` represent the location of a character as a line and 220 * Instances of the class `Location` represent the location of a character as a line and
191 * column pair. 221 * column pair.
192 */ 222 */
193 class LineInfo_Location { 223 class LineInfo_Location {
194 /** 224 /**
195 * The one-based index of the line containing the character. 225 * The one-based index of the line containing the character.
(...skipping 718 matching lines...) Expand 10 before | Expand all | Expand 10 after
914 944
915 /** 945 /**
916 * Return an absolute URI that represents the given source, or `null` if a val id URI cannot 946 * Return an absolute URI that represents the given source, or `null` if a val id URI cannot
917 * be computed. 947 * be computed.
918 * 948 *
919 * @param source the source to get URI for 949 * @param source the source to get URI for
920 * @return the absolute URI representing the given source 950 * @return the absolute URI representing the given source
921 */ 951 */
922 Uri restoreAbsolute(Source source) => null; 952 Uri restoreAbsolute(Source source) => null;
923 } 953 }
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