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

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

Issue 74423005: Add optimized String.fromCharCodes path for Uint8List and Int8List. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 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
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 String { 5 patch class String {
6 /* patch */ factory String.fromCharCodes(Iterable<int> charCodes) { 6 /* patch */ factory String.fromCharCodes(Iterable<int> charCodes) {
7 return _StringBase.createFromCharCodes(charCodes); 7 return _StringBase.createFromCharCodes(charCodes);
8 } 8 }
9 9
10 /* patch */ const factory String.fromEnvironment(String name, 10 /* patch */ const factory String.fromEnvironment(String name,
(...skipping 16 matching lines...) Expand all
27 Type get runtimeType => String; 27 Type get runtimeType => String;
28 28
29 int get hashCode native "String_getHashCode"; 29 int get hashCode native "String_getHashCode";
30 30
31 /** 31 /**
32 * Create the most efficient string representation for specified 32 * Create the most efficient string representation for specified
33 * [codePoints]. 33 * [codePoints].
34 */ 34 */
35 static String createFromCharCodes(Iterable<int> charCodes) { 35 static String createFromCharCodes(Iterable<int> charCodes) {
36 if (charCodes != null) { 36 if (charCodes != null) {
37 if (charCodes is Uint8List || charCodes is Int8List) {
Anders Johnsen 2013/11/18 20:26:31 Should we use _classId here as well?
srdjan 2013/11/18 20:45:15 Yes, using cid-s is much quicker.
Anders Johnsen 2013/11/19 07:38:43 I encountered an issue here. These classes are in
srdjan 2013/11/19 16:47:38 This would not slow down existing code: if ((ccid
38 return _OneByteString._allocateFromOneByteList(charCodes);
39 }
37 // TODO(srdjan): Also skip copying of typed arrays. 40 // TODO(srdjan): Also skip copying of typed arrays.
38 final ccid = charCodes._cid; 41 final ccid = charCodes._cid;
39 if ((ccid != _List._classId) && 42 if ((ccid != _List._classId) &&
40 (ccid != _GrowableList._classId) && 43 (ccid != _GrowableList._classId) &&
41 (ccid != _ImmutableList._classId)) { 44 (ccid != _ImmutableList._classId)) {
42 charCodes = new List<int>.from(charCodes, growable: false); 45 charCodes = new List<int>.from(charCodes, growable: false);
43 } 46 }
44 47
45 bool isOneByteString = true; 48 bool isOneByteString = true;
46 for (int i = 0; i < charCodes.length; i++) { 49 for (int i = 0; i < charCodes.length; i++) {
47 int e = charCodes[i]; 50 int e = charCodes[i];
48 if (e is! _Smi) throw new ArgumentError(e); 51 if (e is! _Smi) throw new ArgumentError(e);
49 // Is e Latin1? 52 // Is e Latin1?
50 if ((e < 0) || (e > 0xFF)) { 53 if ((e < 0) || (e > 0xFF)) {
51 isOneByteString = false; 54 isOneByteString = false;
52 break; 55 break;
53 } 56 }
54 } 57 }
55 if (isOneByteString) { 58 if (isOneByteString) {
56 var s = _OneByteString._allocate(charCodes.length); 59 return _OneByteString._allocateFromOneByteList(charCodes);
srdjan 2013/11/18 20:45:15 Calling to native is quite slow, i.e., for small l
Anders Johnsen 2013/11/18 21:10:51 Before, we already did the runtime-call to allocat
57 for (int i = 0; i < charCodes.length; i++) {
58 s._setAt(i, charCodes[i]);
59 }
60 return s;
61 } 60 }
62 } 61 }
63 return _createFromCodePoints(charCodes); 62 return _createFromCodePoints(charCodes);
64 } 63 }
65 64
66 static String _createFromCodePoints(List<int> codePoints) 65 static String _createFromCodePoints(List<int> codePoints)
67 native "StringBase_createFromCodePoints"; 66 native "StringBase_createFromCodePoints";
68 67
69 String operator [](int index) native "String_charAt"; 68 String operator [](int index) native "String_charAt";
70 69
(...skipping 550 matching lines...) Expand 10 before | Expand all | Expand 10 after
621 return false; 620 return false;
622 } 621 }
623 } 622 }
624 return super.contains(pattern, start); 623 return super.contains(pattern, start);
625 } 624 }
626 625
627 // Allocates a string of given length, expecting its content to be 626 // Allocates a string of given length, expecting its content to be
628 // set using _setAt. 627 // set using _setAt.
629 static _OneByteString _allocate(int length) native "OneByteString_allocate"; 628 static _OneByteString _allocate(int length) native "OneByteString_allocate";
630 629
630
631 static _OneByteString _allocateFromOneByteList(List<int> list)
632 native "OneByteString_allocateFromOneByteList";
633
631 // This is internal helper method. Code point value must be a valid 634 // This is internal helper method. Code point value must be a valid
632 // Latin1 value (0..0xFF), index must be valid. 635 // Latin1 value (0..0xFF), index must be valid.
633 void _setAt(int index, int codePoint) native "OneByteString_setAt"; 636 void _setAt(int index, int codePoint) native "OneByteString_setAt";
634 } 637 }
635 638
636 639
637 class _TwoByteString extends _StringBase implements String { 640 class _TwoByteString extends _StringBase implements String {
638 static final int _classId = "\u{FFFF}"._cid; 641 static final int _classId = "\u{FFFF}"._cid;
639 642
640 factory _TwoByteString._uninstantiable() { 643 factory _TwoByteString._uninstantiable() {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
723 class _CodeUnits extends Object with ListMixin<int>, 726 class _CodeUnits extends Object with ListMixin<int>,
724 UnmodifiableListMixin<int> { 727 UnmodifiableListMixin<int> {
725 /** The string that this is the code units of. */ 728 /** The string that this is the code units of. */
726 String _string; 729 String _string;
727 730
728 _CodeUnits(this._string); 731 _CodeUnits(this._string);
729 732
730 int get length => _string.length; 733 int get length => _string.length;
731 int operator[](int i) => _string.codeUnitAt(i); 734 int operator[](int i) => _string.codeUnitAt(i);
732 } 735 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698