r61644 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r61643‎ | r61644 | r61645 >
Date:20:10, 28 January 2010
Author:catrope
Status:deferred
Tags:
Comment:
UsabilityInitiative: Optimize beforeSelection, which calls $() in a tight loop. Speedup was about 12x on a large article
Modified paths:
  • /trunk/extensions/UsabilityInitiative/UsabilityInitiative.hooks.php (modified) (history)
  • /trunk/extensions/UsabilityInitiative/js/plugins.combined.js (modified) (history)
  • /trunk/extensions/UsabilityInitiative/js/plugins.combined.min.js (modified) (history)
  • /trunk/extensions/UsabilityInitiative/js/plugins/jquery.wikiEditor.js (modified) (history)

Diff [purge]

Index: trunk/extensions/UsabilityInitiative/UsabilityInitiative.hooks.php
@@ -72,7 +72,7 @@
7373 array( 'src' => 'js/plugins/jquery.namespaceSelect.js', 'version' => 1 ),
7474 array( 'src' => 'js/plugins/jquery.suggestions.js', 'version' => 7 ),
7575 array( 'src' => 'js/plugins/jquery.textSelection.js', 'version' => 26 ),
76 - array( 'src' => 'js/plugins/jquery.wikiEditor.js', 'version' => 81 ),
 76+ array( 'src' => 'js/plugins/jquery.wikiEditor.js', 'version' => 82 ),
7777 array( 'src' => 'js/plugins/jquery.wikiEditor.highlight.js', 'version' => 25 ),
7878 array( 'src' => 'js/plugins/jquery.wikiEditor.toolbar.js', 'version' => 45 ),
7979 array( 'src' => 'js/plugins/jquery.wikiEditor.dialogs.js', 'version' => 11 ),
@@ -82,10 +82,10 @@
8383 array( 'src' => 'js/plugins/jquery.wikiEditor.publish.js', 'version' => 2 ),
8484 ),
8585 'combined' => array(
86 - array( 'src' => 'js/plugins.combined.js', 'version' => 194 ),
 86+ array( 'src' => 'js/plugins.combined.js', 'version' => 195 ),
8787 ),
8888 'minified' => array(
89 - array( 'src' => 'js/plugins.combined.min.js', 'version' => 194 ),
 89+ array( 'src' => 'js/plugins.combined.min.js', 'version' => 195 ),
9090 ),
9191 ),
9292 );
Index: trunk/extensions/UsabilityInitiative/js/plugins/jquery.wikiEditor.js
@@ -367,7 +367,7 @@
368368 .replace( /\r?\n/g, "" ) // IE7 inserts newlines before block elements
369369 .replace( / /g, " " ) // We inserted these to prevent IE from collapsing spaces
370370 + '</pre>' );
371 - // Get rid of the noincludes when getting text
 371+ // TODO: Optimize this, maybe by converting <br>->\n when not at the beginning or end
372372 $pre.find( '.wikiEditor-noinclude' ).each( function() { $( this ).remove(); } );
373373 // Convert tabs, <p>s and <br>s back
374374 $pre.find( '.wikiEditor-tab' ).each( function() { $( this ).text( "\t" ) } );
@@ -716,9 +716,20 @@
717717 }
718718 e = newE || e;
719719 }
720 - while ( e ) {
721 - if ( $( e ).is( selector ) && !strict )
722 - return $( e );
 720+
 721+ // We'd normally use if( $( e ).is( selector ) in the while loop, but running the jQuery
 722+ // constructor thousands of times is very inefficient
 723+ // Instead, tag all occurrences of selector
 724+ if ( selector != '*' ) {
 725+ var occurrences = context.$content.find( selector );
 726+ occurrences.addClass( 'wikiEditor-beforeSelection-tagged' );
 727+ }
 728+ var retval = null;
 729+ while ( e && !retval ) {
 730+ if ( !strict && ( selector == '*' || ( ' ' + e.className + ' ' )
 731+ .indexOf( ' wikiEditor-beforeSelection-tagged ' ) != -1 ) ) {
 732+ retval = $( e );
 733+ }
723734 var next = e.previousSibling;
724735 while ( next && next.lastChild ) {
725736 next = next.lastChild;
@@ -726,7 +737,9 @@
727738 e = next || e.parentNode;
728739 strict = false;
729740 }
730 - return $( [] );
 741+ if ( selector != '*' )
 742+ occurrences.removeClass( 'wikiEditor-beforeSelection-tagged' );
 743+ return retval || $( [] );
731744 },
732745 /**
733746 * Get an object used to traverse the leaf nodes in the iframe DOM. This traversal skips leaf nodes
Index: trunk/extensions/UsabilityInitiative/js/plugins.combined.js
@@ -6791,7 +6791,7 @@
67926792 .replace( /\r?\n/g, "" ) // IE7 inserts newlines before block elements
67936793 .replace( /&nbsp;/g, " " ) // We inserted these to prevent IE from collapsing spaces
67946794 + '</pre>' );
6795 - // Get rid of the noincludes when getting text
 6795+ // TODO: Optimize this, maybe by converting <br>->\n when not at the beginning or end
67966796 $pre.find( '.wikiEditor-noinclude' ).each( function() { $( this ).remove(); } );
67976797 // Convert tabs, <p>s and <br>s back
67986798 $pre.find( '.wikiEditor-tab' ).each( function() { $( this ).text( "\t" ) } );
@@ -7140,9 +7140,20 @@
71417141 }
71427142 e = newE || e;
71437143 }
7144 - while ( e ) {
7145 - if ( $( e ).is( selector ) && !strict )
7146 - return $( e );
 7144+
 7145+ // We'd normally use if( $( e ).is( selector ) in the while loop, but running the jQuery
 7146+ // constructor thousands of times is very inefficient
 7147+ // Instead, tag all occurrences of selector
 7148+ if ( selector != '*' ) {
 7149+ var occurrences = context.$content.find( selector );
 7150+ occurrences.addClass( 'wikiEditor-beforeSelection-tagged' );
 7151+ }
 7152+ var retval = null;
 7153+ while ( e && !retval ) {
 7154+ if ( !strict && ( selector == '*' || ( ' ' + e.className + ' ' )
 7155+ .indexOf( ' wikiEditor-beforeSelection-tagged ' ) != -1 ) ) {
 7156+ retval = $( e );
 7157+ }
71477158 var next = e.previousSibling;
71487159 while ( next && next.lastChild ) {
71497160 next = next.lastChild;
@@ -7150,7 +7161,9 @@
71517162 e = next || e.parentNode;
71527163 strict = false;
71537164 }
7154 - return $( [] );
 7165+ if ( selector != '*' )
 7166+ occurrences.removeClass( 'wikiEditor-beforeSelection-tagged' );
 7167+ return retval || $( [] );
71557168 },
71567169 /**
71577170 * Get an object used to traverse the leaf nodes in the iframe DOM. This traversal skips leaf nodes
Index: trunk/extensions/UsabilityInitiative/js/plugins.combined.min.js
@@ -479,10 +479,12 @@
480480 if(e.nodeName!='#text'){var newE=e.firstChild;for(var i=0;i<offset-1&&newE;i++){newE=newE.nextSibling;}
481481 while(newE&&newE.lastChild){newE=newE.lastChild;}
482482 e=newE||e;}
483 -while(e){if($(e).is(selector)&&!strict)
484 -return $(e);var next=e.previousSibling;while(next&&next.lastChild){next=next.lastChild;}
 483+if(selector!='*'){var occurrences=context.$content.find(selector);occurrences.addClass('wikiEditor-beforeSelection-tagged');}
 484+var retval=null;while(e&&!retval){if(!strict&&(selector=='*'||(' '+e.className+' ').indexOf(' wikiEditor-beforeSelection-tagged ')!=-1)){retval=$(e);}
 485+var next=e.previousSibling;while(next&&next.lastChild){next=next.lastChild;}
485486 e=next||e.parentNode;strict=false;}
486 -return $([]);},'traverser':function(start){function Traverser(node,depth,inP){this.node=node;this.depth=depth;this.inP=inP;this.next=function(){var p=this.node;var nextDepth=this.depth;var nextInP=this.inP;while(p&&!p.nextSibling){if(p.nodeName=="P"){nextInP=false;}
 487+if(selector!='*')
 488+occurrences.removeClass('wikiEditor-beforeSelection-tagged');return retval||$([]);},'traverser':function(start){function Traverser(node,depth,inP){this.node=node;this.depth=depth;this.inP=inP;this.next=function(){var p=this.node;var nextDepth=this.depth;var nextInP=this.inP;while(p&&!p.nextSibling){if(p.nodeName=="P"){nextInP=false;}
487489 p=p.parentNode;nextDepth--;if(nextDepth==0){p=null;}}
488490 p=p?p.nextSibling:null;do{while(p&&(' '+p.className+' ').indexOf(' wikiEditor-noinclude ')!=-1){p=p.nextSibling;}
489491 if(p&&p.firstChild){p=p.firstChild;nextDepth++;if(p.nodeName=="P"){nextInP=true;}}}while(p&&p.firstChild);return p?new Traverser(p,nextDepth,nextInP):null;};}

Status & tagging log