From 81fcb8daf32f6b832cf873dbba7412215d9b0227 Mon Sep 17 00:00:00 2001 From: angelozerr Date: Fri, 5 May 2017 21:25:48 +0200 Subject: [PATCH] Cache the regexp source list (like vscode-textmate). See https://github.com/eclipse/tm4e/issues/92 --- .../core/internal/rule/RegExpSourceList.java | 41 +++++++++---------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/org.eclipse.tm4e.core/src/main/java/org/eclipse/tm4e/core/internal/rule/RegExpSourceList.java b/org.eclipse.tm4e.core/src/main/java/org/eclipse/tm4e/core/internal/rule/RegExpSourceList.java index b26155170..50f1b3043 100644 --- a/org.eclipse.tm4e.core/src/main/java/org/eclipse/tm4e/core/internal/rule/RegExpSourceList.java +++ b/org.eclipse.tm4e.core/src/main/java/org/eclipse/tm4e/core/internal/rule/RegExpSourceList.java @@ -28,33 +28,25 @@ */ public class RegExpSourceList { - private class IRegExpSourceListAnchorCache { + private class RegExpSourceListAnchorCache { public ICompiledRule A0_G0; public ICompiledRule A0_G1; public ICompiledRule A1_G0; public ICompiledRule A1_G1; - public IRegExpSourceListAnchorCache(ICompiledRule A0_G0, ICompiledRule A0_G1, ICompiledRule A1_G0, - ICompiledRule A1_G1) { - this.A0_G0 = A0_G0; - this.A0_G1 = A0_G1; - this.A1_G0 = A1_G0; - this.A1_G1 = A1_G1; - } - } private List _items; private boolean _hasAnchors; private ICompiledRule _cached; - private IRegExpSourceListAnchorCache _anchorCache; + private final RegExpSourceListAnchorCache _anchorCache; public RegExpSourceList() { this._items = new ArrayList(); this._hasAnchors = false; this._cached = null; - this._anchorCache = new IRegExpSourceListAnchorCache(null, null, null, null); + this._anchorCache = new RegExpSourceListAnchorCache(); } public void push(RegExpSource item) { @@ -94,16 +86,23 @@ public ICompiledRule compile(IRuleRegistry grammar, boolean allowA, boolean allo this._cached = new ICompiledRule(createOnigScanner(regexps.toArray(new String[0])), getRules()); } return this._cached; - } else { - this._anchorCache = new IRegExpSourceListAnchorCache( - (this._anchorCache.A0_G0 != null || (allowA == false && allowG == false) - ? this._resolveAnchors(allowA, allowG) : null), - (this._anchorCache.A0_G1 != null || (allowA == false && allowG == true) - ? this._resolveAnchors(allowA, allowG) : null), - (this._anchorCache.A1_G0 != null || (allowA == true && allowG == false) - ? this._resolveAnchors(allowA, allowG) : null), - (this._anchorCache.A1_G1 != null || (allowA == true && allowG == true) - ? this._resolveAnchors(allowA, allowG) : null)); + } else { + if (this._anchorCache.A0_G0 == null) { + this._anchorCache.A0_G0 = (allowA == false && allowG == false) ? this._resolveAnchors(allowA, allowG) + : null; + } + if (this._anchorCache.A0_G1 == null) { + this._anchorCache.A0_G1 = (allowA == false && allowG == true) ? this._resolveAnchors(allowA, allowG) + : null; + } + if (this._anchorCache.A1_G0 == null) { + this._anchorCache.A1_G0 = (allowA == true && allowG == false) ? this._resolveAnchors(allowA, allowG) + : null; + } + if (this._anchorCache.A1_G1 == null) { + this._anchorCache.A1_G1 = (allowA == true && allowG == true) ? this._resolveAnchors(allowA, allowG) + : null; + } if (allowA) { if (allowG) { return this._anchorCache.A1_G1;