r112099 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112098‎ | r112099 | r112100 >
Date:11:25, 22 February 2012
Author:gwicke
Status:deferred
Tags:
Comment:
Speed up flatten slightly by avoiding garbage for already flat arrays. Also,
use simple string concatenation instead of arrays as the strings tend to be
few and short.
Modified paths:
  • /trunk/extensions/VisualEditor/modules/parser/pegTokenizer.pegjs.txt (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/parser/pegTokenizer.pegjs.txt
@@ -20,20 +20,51 @@
2121 /* Static utilities */
2222
2323 // Flatten a list of lists.
 24+ //var simple_flatten = function ( e ) {
 25+ // // Fast single-level flatten:
 26+ // //return [].concat.apply([], e);
 27+ //
 28+ // var es = [];
 29+ // // flatten sub-arrays
 30+ // for(var i = 0, length = e.length; i < length; i++) {
 31+ // var ei = e[i];
 32+ // if ($.isArray(ei))
 33+ // es = es.concat(flatten(ei));
 34+ // else
 35+ // es.push(ei);
 36+ // };
 37+ // return es;
 38+ //};
 39+
2440 var flatten = function ( e ) {
2541 // Fast single-level flatten:
2642 //return [].concat.apply([], e);
2743
2844 var es = [];
2945 // flatten sub-arrays
30 - for(var i = 0, length = e.length; i < length; i++) {
31 - var ei = e[i];
32 - if ($.isArray(ei))
33 - es = es.concat(flatten(ei));
34 - else
35 - es.push(ei);
 46+ var chunkStart = null,
 47+ modified = false;
 48+ for(var i = 0, l = e.length; i < l; i++) {
 49+ if ( e[i].constructor === Array ) {
 50+ if ( chunkStart !== null ) {
 51+ es = es.concat( e.slice( chunkStart, i ), flatten( e[i] ) );
 52+ chunkStart = null;
 53+ } else {
 54+ es = es.concat(flatten(e[i]));
 55+ }
 56+ modified = true;
 57+ } else if ( chunkStart === null ) {
 58+ chunkStart = i;
 59+ }
3660 };
37 - return es;
 61+ if ( modified ) {
 62+ if ( chunkStart !== null ) {
 63+ es = es.concat( e.slice( chunkStart, e.length ) );
 64+ }
 65+ return es;
 66+ } else {
 67+ return e;
 68+ }
3869 };
3970
4071
@@ -48,24 +79,24 @@
4980
5081 var flatten_stringlist = function ( c ) {
5182 var out = [],
52 - text = [];
 83+ text = '';
5384 c = flatten(c);
5485 for (var i = 0, l = c.length; i < l; i++) {
5586 var ci = c[i];
5687 if (ci.constructor === String) {
5788 if(ci !== '') {
58 - text.push(ci);
 89+ text += ci;
5990 }
6091 } else {
61 - if (text.length) {
62 - out.push( text.join('') );
63 - text = [];
 92+ if (text !== '') {
 93+ out.push( text );
 94+ text = '';
6495 }
6596 out.push(ci);
6697 }
6798 }
68 - if (text.length) {
69 - out.push( text.join('') );
 99+ if (text !== '' ) {
 100+ out.push( text );
70101 }
71102 return out;
72103 }

Status & tagging log