From eb84a93fa1c34f90c198fab7ac4ab8d3143f84c0 Mon Sep 17 00:00:00 2001 From: Randy Edmunds Date: Tue, 1 Oct 2013 16:58:43 -0700 Subject: [PATCH 1/2] use precise=false for performance when parsing unchanging code --- src/language/HTMLUtils.js | 2 +- src/utils/TokenUtils.js | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/language/HTMLUtils.js b/src/language/HTMLUtils.js index e80768de554..405e7b48790 100644 --- a/src/language/HTMLUtils.js +++ b/src/language/HTMLUtils.js @@ -482,7 +482,7 @@ define(function (require, exports, module) { outerMode = editor._codeMirror.getMode(), tokenModeName; - while (TokenUtils.moveNextToken(ctx)) { + while (TokenUtils.moveNextToken(ctx, false)) { tokenModeName = CodeMirror.innerMode(outerMode, ctx.token.state).mode.name; if (inBlock) { if (!currentBlock.end) { diff --git a/src/utils/TokenUtils.js b/src/utils/TokenUtils.js index 625434aaefe..047e552dd5e 100644 --- a/src/utils/TokenUtils.js +++ b/src/utils/TokenUtils.js @@ -51,9 +51,15 @@ define(function (require, exports, module) { /** * Moves the given context backwards by one token. * @param {editor:{CodeMirror}, pos:{ch:{string}, line:{number}}, token:{object}} ctx + * @param {boolean=} precise If code is being edited, use true (default) for accuracy. + * If parsing unchanging code, use false to use cache for performance. * @return {boolean} whether the context changed */ - function movePrevToken(ctx) { + function movePrevToken(ctx, precise) { + if (precise === undefined) { + precise = true; + } + if (ctx.pos.ch <= 0 || ctx.token.start <= 0) { //move up a line if (ctx.pos.line <= 0) { @@ -64,17 +70,23 @@ define(function (require, exports, module) { } else { ctx.pos.ch = ctx.token.start; } - ctx.token = ctx.editor.getTokenAt(ctx.pos, true); + ctx.token = ctx.editor.getTokenAt(ctx.pos, precise); return true; } /** * Moves the given context forward by one token. * @param {editor:{CodeMirror}, pos:{ch:{string}, line:{number}}, token:{object}} ctx + * @param {boolean=} precise If code is being edited, use true (default) for accuracy. + * If parsing unchanging code, use false to use cache for performance. * @return {boolean} whether the context changed */ - function moveNextToken(ctx) { + function moveNextToken(ctx, precise) { var eol = ctx.editor.getLine(ctx.pos.line).length; + if (precise === undefined) { + precise = true; + } + if (ctx.pos.ch >= eol || ctx.token.end >= eol) { //move down a line if (ctx.pos.line >= ctx.editor.lineCount() - 1) { @@ -85,7 +97,7 @@ define(function (require, exports, module) { } else { ctx.pos.ch = ctx.token.end + 1; } - ctx.token = ctx.editor.getTokenAt(ctx.pos, true); + ctx.token = ctx.editor.getTokenAt(ctx.pos, precise); return true; } From dc0ed463f4d37a27bc260f1d5dc1ac8e45224f43 Mon Sep 17 00:00:00 2001 From: Randy Edmunds Date: Wed, 2 Oct 2013 08:15:46 -0700 Subject: [PATCH 2/2] performance: do not use precise token parsing --- src/extensions/default/JavaScriptCodeHints/Session.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/extensions/default/JavaScriptCodeHints/Session.js b/src/extensions/default/JavaScriptCodeHints/Session.js index 34a2a7ead5a..f8cc9fdcfc6 100644 --- a/src/extensions/default/JavaScriptCodeHints/Session.js +++ b/src/extensions/default/JavaScriptCodeHints/Session.js @@ -131,9 +131,9 @@ define(function (require, exports, module) { var cm = this.editor._codeMirror; if (cursor) { - return cm.getTokenAt(cursor, true); + return cm.getTokenAt(cursor); } else { - return cm.getTokenAt(this.getCursor(), true); + return cm.getTokenAt(this.getCursor()); } };