Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollback of ThreadLocal optimization change #43

Merged
merged 2 commits into from
Dec 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion java/com/google/re2j/Pattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public final class Pattern implements Serializable {
}

/**
* Releases current thread memory used by internal caches associated with this pattern. Does
* Releases memory used by internal caches associated with this pattern. Does
* not change the observable behaviour. Useful for tests that detect memory
* leaks via allocation tracking.
*/
Expand Down
42 changes: 29 additions & 13 deletions java/com/google/re2j/RE2.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ class RE2 {
int prefixRune; // first rune in prefix

// Cache of machines for running regexp.
// Warning: Don't use initialValue, GWT doesn't have that method.
private final ThreadLocal<Machine> machineThreadLocal = new ThreadLocal<Machine>();
// Accesses must be serialized using |this| monitor.
private final List<Machine> machine = new ArrayList<Machine>();

// This is visible for testing.
RE2(String expr) {
Expand Down Expand Up @@ -199,11 +199,6 @@ static RE2 compileImpl(String expr, int mode, boolean longest)
return re2;
}

// Clears the memory associated with this machine for the current thread.
void reset() {
machineThreadLocal.remove();
}

/**
* Returns the number of parenthesized subexpressions in this regular
* expression.
Expand All @@ -212,6 +207,29 @@ int numberOfCapturingGroups() {
return numSubexp;
}

// get() returns a machine to use for matching |this|. It uses |this|'s
// machine cache if possible, to avoid unnecessary allocation.
synchronized Machine get() {
int n = machine.size();
if (n > 0) {
return machine.remove(n - 1);
}
return new Machine(this);
}

// Clears the memory associated with this machine.
synchronized void reset() {
machine.clear();
}

// put() returns a machine to |this|'s machine cache. There is no attempt to
// limit the size of the cache, so it will grow to the maximum number of
// simultaneous matches run using |this|. (The cache empties when |this|
// gets garbage collected.)
synchronized void put(Machine m) {
machine.add(m);
}

@Override
public String toString() {
return expr;
Expand All @@ -221,13 +239,11 @@ public String toString() {
// the position of its subexpressions.
// Derived from exec.go.
private int[] doExecute(MachineInput in, int pos, int anchor, int ncap) {
Machine m = machineThreadLocal.get();
if (m == null) {
m = new Machine(this);
machineThreadLocal.set(m);
}
Machine m = get();
m.init(ncap);
return m.match(in, pos, anchor) ? m.submatches() : null;
int[] cap = m.match(in, pos, anchor) ? m.submatches() : null;
put(m);
return cap;
}

/**
Expand Down