Skip to content

Commit

Permalink
Cache the regexp source list (like vscode-textmate). See
Browse files Browse the repository at this point in the history
  • Loading branch information
angelozerr committed May 5, 2017
1 parent 5d846ac commit 81fcb8d
Showing 1 changed file with 20 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<RegExpSource> _items;
private boolean _hasAnchors;
private ICompiledRule _cached;
private IRegExpSourceListAnchorCache _anchorCache;
private final RegExpSourceListAnchorCache _anchorCache;

public RegExpSourceList() {
this._items = new ArrayList<RegExpSource>();
this._hasAnchors = false;
this._cached = null;
this._anchorCache = new IRegExpSourceListAnchorCache(null, null, null, null);
this._anchorCache = new RegExpSourceListAnchorCache();
}

public void push(RegExpSource item) {
Expand Down Expand Up @@ -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;
Expand Down

4 comments on commit 81fcb8d

@mickaelistria
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of things like allowA == true && allowB == false, it's usually better to write allowA && !allowB

@angelozerr
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I know but I would like to keep the same code than vscode-textmate. It's helpful when you compare tm4e code with vscode-textmate code.

@angelozerr
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mickaelistria do you want I fix it to avoid having sonarcube errors?

@mickaelistria
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not high priority, don't worry about it.

Please sign in to comment.