MWReferenceSearchWidget buildIndex optimisations

* Each item builds an MWReferenceModel which creates a document
  slice clone. This is very expensive and we only use the model
  for getting basic attributes, so defer the evaluation of the
  document.
* $.show is expensive and, in this case, unnecessary.

Change-Id: I99abc4c1b17f05559a9cae68b15121a8be6d23fb
This commit is contained in:
Ed Sanders 2014-07-21 14:32:17 +01:00
parent 365453f2b3
commit d4cd816b6f
2 changed files with 17 additions and 9 deletions

View file

@ -23,6 +23,7 @@ ve.dm.MWReferenceModel = function VeDmMWReferenceModel() {
this.listIndex = null;
this.group = '';
this.doc = null;
this.deferDoc = null;
};
/* Inheritance */
@ -47,7 +48,10 @@ ve.dm.MWReferenceModel.static.newFromReferenceNode = function ( node ) {
ref.setListGroup( attr.listGroup );
ref.setListIndex( attr.listIndex );
ref.setGroup( attr.refGroup );
ref.setDocument( doc.cloneFromRange( internalList.getItemNode( attr.listIndex ).getRange() ) );
ref.deferDoc = function () {
// cloneFromRange is very expensive, so lazy evaluate it
return doc.cloneFromRange( internalList.getItemNode( attr.listIndex ).getRange() );
};
return ref;
};
@ -149,7 +153,7 @@ ve.dm.MWReferenceModel.prototype.updateInternalItem = function ( surfaceModel )
itemNodeRange = internalList.getItemNode( this.listIndex ).getRange();
surfaceModel.change( ve.dm.Transaction.newFromRemoval( doc, itemNodeRange, true ) );
surfaceModel.change(
ve.dm.Transaction.newFromDocumentInsertion( doc, itemNodeRange.start, this.doc )
ve.dm.Transaction.newFromDocumentInsertion( doc, itemNodeRange.start, this.getDocument() )
);
};
@ -219,6 +223,9 @@ ve.dm.MWReferenceModel.prototype.getGroup = function () {
*/
ve.dm.MWReferenceModel.prototype.getDocument = function () {
if ( !this.doc ) {
if ( this.deferDoc ) {
this.doc = this.deferDoc();
} else {
this.doc = new ve.dm.Document( [
{ 'type': 'paragraph', 'internal': { 'generated': 'wrapper' } },
{ 'type': '/paragraph' },
@ -226,6 +233,7 @@ ve.dm.MWReferenceModel.prototype.getDocument = function () {
{ 'type': '/internalList' }
] );
}
}
return this.doc;
};

View file

@ -131,7 +131,7 @@ ve.ui.MWReferenceSearchWidget.prototype.buildIndex = function ( internalList ) {
view.$element.find( 'a[href]' ).each( extractAttrs );
this.index.push( {
'$element': view.$element.clone().show(),
'$element': view.$element.clone(),
'text': text,
'reference': ref,
'citation': citation,