From ae0c695971b89eabead6d1940f43b0b048a32837 Mon Sep 17 00:00:00 2001 From: Philip Peitsch Date: Fri, 31 Jan 2014 16:29:12 +1100 Subject: [PATCH 1/4] Add paragraph alignment to the benchmark This is disabled by default because it is very very slow (8s @ 10pages) --- .../js/AlignCurrentSelectionJustified.js | 65 +++++++++++++++++++ programs/benchmark/js/EnterEditMode.js | 2 +- programs/benchmark/js/HTMLBenchmark.js | 7 +- 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 programs/benchmark/js/AlignCurrentSelectionJustified.js diff --git a/programs/benchmark/js/AlignCurrentSelectionJustified.js b/programs/benchmark/js/AlignCurrentSelectionJustified.js new file mode 100644 index 00000000..b9f849c9 --- /dev/null +++ b/programs/benchmark/js/AlignCurrentSelectionJustified.js @@ -0,0 +1,65 @@ +/** + * @license + * Copyright (C) 2014 KO GmbH + * + * @licstart + * The JavaScript code in this page is free software: you can redistribute it + * and/or modify it under the terms of the GNU Affero General Public License + * (GNU AGPL) as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. The code is distributed + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU AGPL for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this code. If not, see . + * + * As additional permission under GNU AGPL version 3 section 7, you + * may distribute non-source (e.g., minimized or compacted) forms of + * that code without the copy of the GNU GPL normally required by + * section 4, provided you include this license notice and a URL + * through which recipients can access the Corresponding Source. + * + * As a special exception to the AGPL, any HTML file which merely makes function + * calls to this code, and for that purpose includes it by reference shall be + * deemed a separate work for copyright law purposes. In addition, the copyright + * holders of this code give you permission to combine this code with free + * software libraries that are released under the GNU LGPL. You may copy and + * distribute such a system following the terms of the GNU AGPL for this code + * and the LGPL for the libraries. If you modify this code, you may extend this + * exception to your version of the code, but you are not obligated to do so. + * If you do not wish to do so, delete this exception statement from your + * version. + * + * This license applies to this entire compilation. + * @licend + * @source: http://www.webodf.org/ + * @source: https://github.com/kogmbh/WebODF/ + */ + +define(["BenchmarkAction"], function(BenchmarkAction) { + "use strict"; + + /** + * Align the current selection 'justify' + * @constructor + */ + function AlignCurrentSelectionJustified() { + var state = {description: "Align the current selection 'justify'"}, + action = new BenchmarkAction(state); + + this.subscribe = action.subscribe; + this.state = state; + + /** + * @param {!SharedState} sharedState + * @return {undefined} + */ + this.start = function(sharedState) { + action.start(); + sharedState.sessionController.getDirectFormattingController().alignParagraphJustified(); + action.complete(true); + } + } + + return AlignCurrentSelectionJustified; +}); diff --git a/programs/benchmark/js/EnterEditMode.js b/programs/benchmark/js/EnterEditMode.js index c2ae8d25..1be8fd48 100644 --- a/programs/benchmark/js/EnterEditMode.js +++ b/programs/benchmark/js/EnterEditMode.js @@ -58,7 +58,7 @@ define(["BenchmarkAction"], function(BenchmarkAction) { action = new BenchmarkAction(state), localMemberId = "localmember", sessionControllerOptions = { - directStylingEnabled: true + directParagraphStylingEnabled: true }, viewOptions = { editInfoMarkersInitiallyVisible: false, diff --git a/programs/benchmark/js/HTMLBenchmark.js b/programs/benchmark/js/HTMLBenchmark.js index a58f61e5..c8eae1fd 100644 --- a/programs/benchmark/js/HTMLBenchmark.js +++ b/programs/benchmark/js/HTMLBenchmark.js @@ -49,10 +49,12 @@ define([ "RemoveCurrentSelection", "PreloadDocument", "BoldCurrentSelection", + "AlignCurrentSelectionJustified", "MoveCursorToEnd" ], function (Benchmark, HTMLResultsRenderer, OpenDocument, EnterEditMode, MoveCursorToEndDirect,InsertLetterA, RemovePositions, MoveCursorLeft, - SelectEntireDocument, RemoveCurrentSelection, PreloadDocument, BoldCurrentSelection, MoveCursorToEnd) { + SelectEntireDocument, RemoveCurrentSelection, PreloadDocument, BoldCurrentSelection, + AlignCurrentSelectionJustified, MoveCursorToEnd) { "use strict"; /** @@ -119,6 +121,9 @@ define([ benchmark.actions.push(new RemovePositions(100, true)); benchmark.actions.push(new SelectEntireDocument()); benchmark.actions.push(new BoldCurrentSelection()); + if (config.includeSlow) { + benchmark.actions.push(new AlignCurrentSelectionJustified()); + } benchmark.actions.push(new RemoveCurrentSelection()); this.start = benchmark.start; From eb3d8aefbf97499a5040c9c49ecd0b87ba3fc92b Mon Sep 17 00:00:00 2001 From: Philip Peitsch Date: Fri, 31 Jan 2014 00:27:34 +1100 Subject: [PATCH 2/4] Batch up calls that refresh the canvas CSS, size or annotations These calls are UI render-related operations, and don't need to be done immediately after every operation. Batching these up and doing them all once after all ops are processed greatly improves the performance of paragraph alignment updates with multiple paragraphs selected. --- programs/benchmark/js/HTMLBenchmark.js | 4 +-- webodf/lib/manifest.json | 2 ++ webodf/lib/odf/OdfCanvas.js | 43 +++++++++++++++++++++----- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/programs/benchmark/js/HTMLBenchmark.js b/programs/benchmark/js/HTMLBenchmark.js index c8eae1fd..30dcf06b 100644 --- a/programs/benchmark/js/HTMLBenchmark.js +++ b/programs/benchmark/js/HTMLBenchmark.js @@ -121,9 +121,7 @@ define([ benchmark.actions.push(new RemovePositions(100, true)); benchmark.actions.push(new SelectEntireDocument()); benchmark.actions.push(new BoldCurrentSelection()); - if (config.includeSlow) { - benchmark.actions.push(new AlignCurrentSelectionJustified()); - } + benchmark.actions.push(new AlignCurrentSelectionJustified()); benchmark.actions.push(new RemoveCurrentSelection()); this.start = benchmark.start; diff --git a/webodf/lib/manifest.json b/webodf/lib/manifest.json index f29aa41f..edb78bf6 100644 --- a/webodf/lib/manifest.json +++ b/webodf/lib/manifest.json @@ -305,7 +305,9 @@ "odf/OdfContainer.js" ], "odf/OdfCanvas.js": [ + "core/Async.js", "core/DomUtils.js", + "core/ScheduledTask.js", "gui/AnnotationViewManager.js", "odf/FontLoader.js", "odf/Formatting.js", diff --git a/webodf/lib/odf/OdfCanvas.js b/webodf/lib/odf/OdfCanvas.js index 888055bd..99e899f3 100644 --- a/webodf/lib/odf/OdfCanvas.js +++ b/webodf/lib/odf/OdfCanvas.js @@ -39,7 +39,9 @@ /*jslint sub: true*/ /*global runtime, odf, xmldom, webodf_css, core, gui */ +runtime.loadClass("core.Async"); runtime.loadClass("core.DomUtils"); +runtime.loadClass("core.ScheduledTask"); runtime.loadClass("odf.OdfContainer"); runtime.loadClass("odf.Formatting"); runtime.loadClass("xmldom.XPath"); @@ -944,6 +946,7 @@ runtime.loadClass("gui.AnnotationViewManager"); "odf.OdfCanvas constructor needs DOM"); var self = this, doc = /**@type{!Document}*/(element.ownerDocument), + async = new core.Async(), /**@type{!odf.OdfContainer}*/ odfcontainer, /**@type{!odf.Formatting}*/ @@ -969,6 +972,9 @@ runtime.loadClass("gui.AnnotationViewManager"); /**@type{!Object.>}*/ eventHandlers = {}, waitingForDoneTimeoutId, + /**@type{!core.ScheduledTask}*/redrawContainerTask, + shouldRefreshCss = false, + shouldRerenderAnnotations = false, loadingQueue = new LoadingQueue(); /** @@ -1112,6 +1118,25 @@ runtime.loadClass("gui.AnnotationViewManager"); element.style.width = Math.round(zoomLevel * sizer.offsetWidth) + "px"; element.style.height = Math.round(zoomLevel * sizer.offsetHeight) + "px"; } + + /** + * @return {undefined} + */ + function redrawContainer() { + if (shouldRefreshCss) { + handleStyles(odfcontainer, formatting, stylesxmlcss); + shouldRefreshCss = false; + // different styles means different layout, thus different sizes + } + if (shouldRerenderAnnotations) { + if (annotationViewManager) { + annotationViewManager.rerenderAnnotations(); + } + shouldRerenderAnnotations = false; + } + fixContainerSize(); + } + /** * A new content.xml has been loaded. Update the live document with it. * @param {!odf.OdfContainer} container @@ -1266,9 +1291,8 @@ runtime.loadClass("gui.AnnotationViewManager"); * @return {undefined} */ this.refreshCSS = function () { - handleStyles(odfcontainer, formatting, stylesxmlcss); - // different styles means different layout, thus different sizes: - fixContainerSize(); + shouldRefreshCss = true; + redrawContainerTask.trigger(); }; /** @@ -1277,7 +1301,7 @@ runtime.loadClass("gui.AnnotationViewManager"); * @return {undefined} */ this.refreshSize = function () { - fixContainerSize(); + redrawContainerTask.trigger(); }; /** * @return {!odf.OdfContainer} @@ -1368,8 +1392,8 @@ runtime.loadClass("gui.AnnotationViewManager"); */ this.rerenderAnnotations = function () { if (annotationViewManager) { - annotationViewManager.rerenderAnnotations(); - fixContainerSize(); + shouldRerenderAnnotations = true; + redrawContainerTask.trigger(); } }; @@ -1543,7 +1567,9 @@ runtime.loadClass("gui.AnnotationViewManager"); * @return {undefined} */ this.destroy = function(callback) { - var head = /**@type{!HTMLHeadElement}*/(doc.getElementsByTagName('head')[0]); + var head = /**@type{!HTMLHeadElement}*/(doc.getElementsByTagName('head')[0]), + cleanup = [pageSwitcher.destroy, redrawContainerTask.destroy]; + runtime.clearTimeout(waitingForDoneTimeoutId); // TODO: anything to clean with annotationViewManager? if (annotationsPane && annotationsPane.parentNode) { @@ -1560,7 +1586,7 @@ runtime.loadClass("gui.AnnotationViewManager"); head.removeChild(positioncss); // TODO: loadingQueue, make sure it is empty - pageSwitcher.destroy(callback); + async.destroyAll(cleanup, callback); }; function init() { @@ -1569,6 +1595,7 @@ runtime.loadClass("gui.AnnotationViewManager"); fontcss = addStyleSheet(doc); stylesxmlcss = addStyleSheet(doc); positioncss = addStyleSheet(doc); + redrawContainerTask = new core.ScheduledTask(redrawContainer, 0); } init(); From 558a1e3e23a7e31fdf887ab896f969252c778f5d Mon Sep 17 00:00:00 2001 From: Philip Peitsch Date: Fri, 31 Jan 2014 17:12:12 +1100 Subject: [PATCH 3/4] Re-use new styles when applying direct paragraph styling We only create a new paragraph style once per unique paragraph style name. This vastly reduces the number of new styles created when formatting a number of paragraphs that all share the same existing paragraph style. --- webodf/lib/gui/DirectFormattingController.js | 55 +++++++++++++++----- 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/webodf/lib/gui/DirectFormattingController.js b/webodf/lib/gui/DirectFormattingController.js index 7a9cd966..999088f4 100644 --- a/webodf/lib/gui/DirectFormattingController.js +++ b/webodf/lib/gui/DirectFormattingController.js @@ -460,6 +460,15 @@ gui.DirectFormattingController = function DirectFormattingController(session, in return step === ops.StepsTranslator.NEXT_STEP; } + /** + * @param {!Object.} obj + * @param {!string} key + * @return {*} + */ + function getOwnProperty(obj, key) { + return obj.hasOwnProperty(key) ? obj[key] : undefined; + } + /** * @param {!function(!Object) : !Object} applyDirectStyling * @return {undefined} @@ -468,29 +477,49 @@ gui.DirectFormattingController = function DirectFormattingController(session, in var range = odtDocument.getCursor(inputMemberId).getSelectedRange(), paragraphs = odfUtils.getParagraphElements(range), formatting = odtDocument.getFormatting(), - operations = []; + operations = [], + derivedStyleNames = {}, + defaultStyleName; paragraphs.forEach(function(paragraph) { var paragraphStartPoint = odtDocument.convertDomPointToCursorStep(paragraph, 0, roundUp), paragraphStyleName = paragraph.getAttributeNS(odf.Namespaces.textns, "style-name"), - newParagraphStyleName = objectNameGenerator.generateStyleName(), + newParagraphStyleName, opAddStyle, opSetParagraphStyle, paragraphProperties; + // Try and reuse an existing paragraph style if possible if (paragraphStyleName) { - paragraphProperties = formatting.createDerivedStyleObject(paragraphStyleName, "paragraph", {}); + newParagraphStyleName = getOwnProperty(derivedStyleNames, paragraphStyleName); + } else { + newParagraphStyleName = defaultStyleName; } - paragraphProperties = applyDirectStyling(paragraphProperties || {}); - opAddStyle = new ops.OpAddStyle(); - opAddStyle.init({ - memberid: inputMemberId, - styleName: newParagraphStyleName, - styleFamily: 'paragraph', - isAutomaticStyle: true, - setProperties: paragraphProperties - }); - operations.push(opAddStyle); + + if (!newParagraphStyleName) { + newParagraphStyleName = objectNameGenerator.generateStyleName(); + if (paragraphStyleName) { + derivedStyleNames[paragraphStyleName] = newParagraphStyleName; + paragraphProperties = formatting.createDerivedStyleObject(paragraphStyleName, "paragraph", {}); + } else { + defaultStyleName = newParagraphStyleName; + paragraphProperties = {}; + } + + // The assumption is that applyDirectStyling will return the same transform given the same + // paragraph properties (e.g., there is nothing dependent on whether this is the 10th paragraph) + paragraphProperties = applyDirectStyling(paragraphProperties); + opAddStyle = new ops.OpAddStyle(); + opAddStyle.init({ + memberid: inputMemberId, + styleName: newParagraphStyleName, + styleFamily: 'paragraph', + isAutomaticStyle: true, + setProperties: paragraphProperties + }); + operations.push(opAddStyle); + } + opSetParagraphStyle = new ops.OpSetParagraphStyle(); opSetParagraphStyle.init({ From 26a9252edbe9295fc1c2f027096d6d2f22ea6f52 Mon Sep 17 00:00:00 2001 From: Philip Peitsch Date: Fri, 31 Jan 2014 18:46:51 +1100 Subject: [PATCH 4/4] Save and restore focus once per batch of operations Focus saving and restoring is expensive on most browsers, so major gains in performance are realised by not doing this for every operation. A new pair of signals implemented by operation routers denotes when a batch of operations is about to processed, and when the router has completed this batch. Note, there is no implied promises about how many operations are executed within a batch. This is left as a choice to the router implementations. --- webodf/lib/gui/SessionController.js | 4 ++-- webodf/lib/manifest.json | 1 + webodf/lib/ops/OdtDocument.js | 4 ++++ webodf/lib/ops/OperationRouter.js | 14 ++++++++++++++ webodf/lib/ops/Session.js | 24 ++++++++++++++++++++++++ webodf/lib/ops/TrivialOperationRouter.js | 22 ++++++++++++++-------- 6 files changed, 59 insertions(+), 10 deletions(-) diff --git a/webodf/lib/gui/SessionController.js b/webodf/lib/gui/SessionController.js index 302757d1..668420f1 100644 --- a/webodf/lib/gui/SessionController.js +++ b/webodf/lib/gui/SessionController.js @@ -924,9 +924,9 @@ gui.SessionController = (function () { odtDocument.subscribe(ops.OdtDocument.signalOperationEnd, redrawRegionSelectionTask.trigger); odtDocument.subscribe(ops.OdtDocument.signalCursorAdded, inputMethodEditor.registerCursor); odtDocument.subscribe(ops.OdtDocument.signalCursorRemoved, inputMethodEditor.removeCursor); - odtDocument.subscribe(ops.OdtDocument.signalOperationStart, saveFocus); - odtDocument.subscribe(ops.OdtDocument.signalOperationEnd, restoreFocus); odtDocument.subscribe(ops.OdtDocument.signalOperationEnd, updateUndoStack); + odtDocument.subscribe(ops.OdtDocument.signalProcessingBatchStart, saveFocus); + odtDocument.subscribe(ops.OdtDocument.signalProcessingBatchEnd, restoreFocus); } init(); diff --git a/webodf/lib/manifest.json b/webodf/lib/manifest.json index edb78bf6..4bd243bb 100644 --- a/webodf/lib/manifest.json +++ b/webodf/lib/manifest.json @@ -576,6 +576,7 @@ "odf/OdfUtils.js" ], "ops/TrivialOperationRouter.js": [ + "core/EventNotifier.js", "ops/Operation.js", "ops/OperationFactory.js", "ops/OperationRouter.js" diff --git a/webodf/lib/ops/OdtDocument.js b/webodf/lib/ops/OdtDocument.js index 716e2cdc..ac8ba254 100644 --- a/webodf/lib/ops/OdtDocument.js +++ b/webodf/lib/ops/OdtDocument.js @@ -79,6 +79,8 @@ ops.OdtDocument = function OdtDocument(odfCanvas) { ops.OdtDocument.signalTableAdded, ops.OdtDocument.signalOperationStart, ops.OdtDocument.signalOperationEnd, + ops.OdtDocument.signalProcessingBatchStart, + ops.OdtDocument.signalProcessingBatchEnd, ops.OdtDocument.signalUndoStackChanged, ops.OdtDocument.signalStepsInserted, ops.OdtDocument.signalStepsRemoved @@ -908,6 +910,8 @@ ops.OdtDocument = function OdtDocument(odfCanvas) { /**@const*/ops.OdtDocument.signalParagraphStyleModified = "paragraphstyle/modified"; /**@const*/ops.OdtDocument.signalOperationStart = "operation/start"; /**@const*/ops.OdtDocument.signalOperationEnd = "operation/end"; +/**@const*/ops.OdtDocument.signalProcessingBatchStart = "router/batchstart"; +/**@const*/ops.OdtDocument.signalProcessingBatchEnd = "router/batchend"; /**@const*/ops.OdtDocument.signalUndoStackChanged = "undo/changed"; /**@const*/ops.OdtDocument.signalStepsInserted = "steps/inserted"; /**@const*/ops.OdtDocument.signalStepsRemoved = "steps/removed"; diff --git a/webodf/lib/ops/OperationRouter.js b/webodf/lib/ops/OperationRouter.js index 0f8a8a3c..0a40ae24 100644 --- a/webodf/lib/ops/OperationRouter.js +++ b/webodf/lib/ops/OperationRouter.js @@ -115,3 +115,17 @@ ops.OperationRouter.prototype.hasLocalUnsyncedOps = function () {"use strict"; } * @return {!boolean} */ ops.OperationRouter.prototype.hasSessionHostConnection = function () {"use strict"; }; + +/** + * Signal that the operation router is about to process a batch of operations. + * @const + * @type {string} + */ +ops.OperationRouter.signalProcessingBatchStart = "router/batchstart"; + +/** + * Signal that the operation router has just completed processing a batch of operations. + * @const + * @type {string} + */ +ops.OperationRouter.signalProcessingBatchEnd = "router/batchend"; \ No newline at end of file diff --git a/webodf/lib/ops/Session.js b/webodf/lib/ops/Session.js index 64182a98..35d0c03d 100644 --- a/webodf/lib/ops/Session.js +++ b/webodf/lib/ops/Session.js @@ -57,6 +57,24 @@ ops.Session = function Session(odfCanvas) { /**@type{?ops.OperationRouter}*/ operationRouter = null; + /** + * Forward the router's batch start signal on to the document + * @param {*} args + * @return {undefined} + */ + function forwardBatchStart(args) { + odtDocument.emit(ops.OdtDocument.signalProcessingBatchStart, args); + } + + /** + * Forward the router's batch end signal on to the document + * @param {*} args + * @return {undefined} + */ + function forwardBatchEnd(args) { + odtDocument.emit(ops.OdtDocument.signalProcessingBatchEnd, args); + } + /** * @param {!ops.OperationFactory} opFactory */ @@ -72,7 +90,13 @@ ops.Session = function Session(odfCanvas) { * @return {undefined} */ this.setOperationRouter = function (opRouter) { + if (operationRouter) { + operationRouter.unsubscribe(ops.OperationRouter.signalProcessingBatchStart, forwardBatchStart); + operationRouter.unsubscribe(ops.OperationRouter.signalProcessingBatchEnd, forwardBatchEnd); + } operationRouter = opRouter; + operationRouter.subscribe(ops.OperationRouter.signalProcessingBatchStart, forwardBatchStart); + operationRouter.subscribe(ops.OperationRouter.signalProcessingBatchEnd, forwardBatchEnd); opRouter.setPlaybackFunction(function (op) { odtDocument.emit(ops.OdtDocument.signalOperationStart, op); if (op.execute(odtDocument)) { diff --git a/webodf/lib/ops/TrivialOperationRouter.js b/webodf/lib/ops/TrivialOperationRouter.js index ee0c4dd4..b2bfd8c2 100644 --- a/webodf/lib/ops/TrivialOperationRouter.js +++ b/webodf/lib/ops/TrivialOperationRouter.js @@ -35,7 +35,9 @@ * @source: http://www.webodf.org/ * @source: https://github.com/kogmbh/WebODF/ */ -/*global ops*/ +/*global ops, runtime, core*/ + +runtime.loadClass("core.EventNotifier"); /* * route the operations. @@ -52,7 +54,11 @@ ops.TrivialOperationRouter = function TrivialOperationRouter() { "use strict"; - var operationFactory, + var events = new core.EventNotifier([ + ops.OperationRouter.signalProcessingBatchStart, + ops.OperationRouter.signalProcessingBatchEnd + ]), + operationFactory, playbackFunction, groupIdentifier = 0; @@ -89,6 +95,7 @@ ops.TrivialOperationRouter = function TrivialOperationRouter() { // The current implementation is only designed for a localeditor instance & the TrivialUndoManager. // TODO redesign this concept to work with collaborative editing groupIdentifier += 1; + events.emit(ops.OperationRouter.signalProcessingBatchStart, {}); operations.forEach(function(op) { var timedOp, opspec = op.spec(); @@ -100,6 +107,7 @@ ops.TrivialOperationRouter = function TrivialOperationRouter() { // TODO: handle return flag in error case playbackFunction(timedOp); }); + events.emit(ops.OperationRouter.signalProcessingBatchEnd, {}); }; this.close = function (cb) { @@ -111,20 +119,18 @@ ops.TrivialOperationRouter = function TrivialOperationRouter() { * @param {!Function} cb * @return {undefined} */ - /*jslint emptyblock: true, unparam: true*/ - this.subscribe = function (eventId, cb) { + this.subscribe = function(eventId, cb) { + events.subscribe(eventId, cb); }; - /*jslint emptyblock: false, unparam: false*/ /** * @param {!string} eventId * @param {!Function} cb * @return {undefined} */ - /*jslint emptyblock: true, unparam: true*/ - this.unsubscribe = function (eventId, cb) { + this.unsubscribe = function(eventId, cb) { + events.unsubscribe(eventId, cb); }; - /*jslint emptyblock: false, unparam: false*/ /** * @return {!boolean}