Chromium Code Reviews

Side by Side Diff: src/array.js

Issue 6173004: Simplify Join and speedup joining arrays of numbers. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | | 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 2010 the V8 project authors. All rights reserved. 1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 99 matching lines...)
110 110
111 // Attempt to convert the elements. 111 // Attempt to convert the elements.
112 try { 112 try {
113 if (UseSparseVariant(array, length, is_array) && (separator.length == 0)) { 113 if (UseSparseVariant(array, length, is_array) && (separator.length == 0)) {
114 return SparseJoin(array, length, convert); 114 return SparseJoin(array, length, convert);
115 } 115 }
116 116
117 // Fast case for one-element arrays. 117 // Fast case for one-element arrays.
118 if (length == 1) { 118 if (length == 1) {
119 var e = array[0]; 119 var e = array[0];
120 if (!IS_UNDEFINED(e) || (0 in array)) { 120 if (IS_STRING(e)) return e;
121 if (IS_STRING(e)) return e; 121 return convert(e);
Lasse Reichstein 2011/01/10 10:51:44 How about checking against undefined after the IS_
sandholm 2011/01/10 11:40:52 Doesn't matter.
122 return convert(e);
123 }
124 return '';
125 } 122 }
126 123
127 // Construct an array for the elements. 124 // Construct an array for the elements.
128 var elements = new $Array(length); 125 var elements = new $Array(length);
129 var elements_length = 0;
130 126
131 // We pull the empty separator check outside the loop for speed! 127 // We pull the empty separator check outside the loop for speed!
132 if (separator.length == 0) { 128 if (separator.length == 0) {
129 var elements_length = 0;
133 for (var i = 0; i < length; i++) { 130 for (var i = 0; i < length; i++) {
134 var e = array[i]; 131 var e = array[i];
135 if (!IS_UNDEFINED(e)) { 132 if (!(e == null)) {
Lasse Reichstein 2011/01/10 10:51:44 Use IS_NULL_OR_UNDEFINED (which reduces to this bu
sandholm 2011/01/10 11:40:52 I changed this back to IS_UNDEFINED after all.
136 if (!IS_STRING(e)) e = convert(e); 133 if (!IS_STRING(e)) e = convert(e);
137 elements[elements_length++] = e; 134 elements[elements_length++] = e;
138 } 135 }
139 } 136 }
140 elements.length = elements_length; 137 elements.length = elements_length;
141 var result = %_FastAsciiArrayJoin(elements, ''); 138 var result = %_FastAsciiArrayJoin(elements, '');
142 if (!IS_UNDEFINED(result)) return result; 139 if (!IS_UNDEFINED(result)) return result;
143 return %StringBuilderConcat(elements, elements_length, ''); 140 return %StringBuilderConcat(elements, elements_length, '');
144 } 141 }
145 // Non-empty separator. 142 // Non-empty separator.
146 for (var i = 0; i < length; i++) { 143 if (!IS_NUMBER(array[0])) {
147 var e = array[i]; 144 for (var i = 0; i < length; i++) {
148 if (!IS_UNDEFINED(e)) { 145 var e = array[i];
149 if (!IS_STRING(e)) e = convert(e); 146 if (!IS_STRING(e)) e = convert(e);
150 elements[i] = e; 147 elements[i] = e;
151 } else {
152 elements[i] = '';
153 } 148 }
154 } 149 } else {
150 for (var i = 0; i < length; i++) {
151 var e = array[i];
152 if (IS_NUMBER(e)) {
153 elements[i] = %_NumberToString(e);
154 } else {
155 if (!IS_STRING(e)) e = convert(e);
156 elements[i] = e;
157 }
Lasse Reichstein 2011/01/10 10:51:44 How about: if (IS_NUMBER(e)) { e = %_NumberToSt
sandholm 2011/01/10 11:40:52 I want the expected path (IS_NUMBER) to be as fast
158 }
159 }
155 var result = %_FastAsciiArrayJoin(elements, separator); 160 var result = %_FastAsciiArrayJoin(elements, separator);
156 if (!IS_UNDEFINED(result)) return result; 161 if (!IS_UNDEFINED(result)) return result;
157 162
158 var length2 = (length << 1) - 1; 163 var length2 = (length << 1) - 1;
159 var j = length2; 164 var j = length2;
160 var i = length; 165 var i = length;
161 elements[--j] = elements[--i]; 166 elements[--j] = elements[--i];
162 while (i > 0) { 167 while (i > 0) {
163 elements[--j] = separator; 168 elements[--j] = separator;
164 elements[--j] = elements[--i]; 169 elements[--j] = elements[--i];
(...skipping 1056 matching lines...)
1221 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1), 1226 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1),
1222 "reduce", getFunction("reduce", ArrayReduce, 1), 1227 "reduce", getFunction("reduce", ArrayReduce, 1),
1223 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1) 1228 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1)
1224 )); 1229 ));
1225 1230
1226 %FinishArrayPrototypeSetup($Array.prototype); 1231 %FinishArrayPrototypeSetup($Array.prototype);
1227 } 1232 }
1228 1233
1229 1234
1230 SetupArray(); 1235 SetupArray();
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine