Skip to content

Commit b70ef5a

Browse files
committed
Remove custom regex cache and just use a weak-valued map. #2078.
1 parent e316ec7 commit b70ef5a

File tree

3 files changed

+102
-69
lines changed

3 files changed

+102
-69
lines changed

core/src/main/java/org/jruby/RubyRegexp.java

+15-33
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@
3838
import static org.jruby.anno.FrameField.BACKREF;
3939
import static org.jruby.anno.FrameField.LASTLINE;
4040

41-
import java.lang.ref.SoftReference;
42-
import java.util.Iterator;
43-
import java.util.Map;
44-
import java.util.concurrent.ConcurrentHashMap;
45-
4641
import org.jcodings.Encoding;
4742
import org.jcodings.specific.ASCIIEncoding;
4843
import org.jcodings.specific.USASCIIEncoding;
@@ -78,6 +73,9 @@
7873
import org.jruby.util.Sprintf;
7974
import org.jruby.util.StringSupport;
8075
import org.jruby.util.TypeConverter;
76+
import org.jruby.util.collections.WeakValuedMap;
77+
78+
import java.util.Iterator;
8179

8280
@JRubyClass(name="Regexp")
8381
public class RubyRegexp extends RubyObject implements ReOptions, EncodingCapable, MarshalEncoding {
@@ -140,22 +138,10 @@ public boolean shouldMarshalEncoding() {
140138
public Encoding getMarshalEncoding() {
141139
return getEncoding();
142140
}
143-
144-
private static final class RegexpCache {
145-
private volatile SoftReference<Map<ByteList, Regex>> cache = new SoftReference<Map<ByteList, Regex>>(null);
146-
private Map<ByteList, Regex> get() {
147-
Map<ByteList, Regex> patternCache = cache.get();
148-
if (patternCache == null) {
149-
patternCache = new ConcurrentHashMap<ByteList, Regex>(5);
150-
cache = new SoftReference<Map<ByteList, Regex>>(patternCache);
151-
}
152-
return patternCache;
153-
}
154-
}
155-
156-
private static final RegexpCache patternCache = new RegexpCache();
157-
private static final RegexpCache quotedPatternCache = new RegexpCache();
158-
private static final RegexpCache preprocessedPatternCache = new RegexpCache();
141+
// FIXME: Maybe these should not be static?
142+
private static final WeakValuedMap<ByteList, Regex> patternCache = new WeakValuedMap();
143+
private static final WeakValuedMap<ByteList, Regex> quotedPatternCache = new WeakValuedMap();
144+
private static final WeakValuedMap<ByteList, Regex> preprocessedPatternCache = new WeakValuedMap();
159145

160146
private static Regex makeRegexp(Ruby runtime, ByteList bytes, RegexpOptions options, Encoding enc) {
161147
try {
@@ -172,46 +158,42 @@ private static Regex makeRegexp(Ruby runtime, ByteList bytes, RegexpOptions opti
172158
}
173159

174160
static Regex getRegexpFromCache(Ruby runtime, ByteList bytes, Encoding enc, RegexpOptions options) {
175-
Map<ByteList, Regex> cache = patternCache.get();
176-
Regex regex = cache.get(bytes);
161+
Regex regex = patternCache.get(bytes);
177162
if (regex != null && regex.getEncoding() == enc && regex.getOptions() == options.toJoniOptions()) return regex;
178163
regex = makeRegexp(runtime, bytes, options, enc);
179164
regex.setUserObject(bytes);
180-
cache.put(bytes, regex);
165+
patternCache.put(bytes, regex);
181166
return regex;
182167
}
183168

184169
static Regex getQuotedRegexpFromCache(Ruby runtime, ByteList bytes, Encoding enc, RegexpOptions options) {
185-
Map<ByteList, Regex> cache = quotedPatternCache.get();
186-
Regex regex = cache.get(bytes);
170+
Regex regex = quotedPatternCache.get(bytes);
187171
if (regex != null && regex.getEncoding() == enc && regex.getOptions() == options.toJoniOptions()) return regex;
188172
ByteList quoted = quote(bytes, enc);
189173
regex = makeRegexp(runtime, quoted, options, enc);
190174
regex.setUserObject(quoted);
191-
cache.put(bytes, regex);
175+
quotedPatternCache.put(bytes, regex);
192176
return regex;
193177
}
194178

195179
static Regex getQuotedRegexpFromCache19(Ruby runtime, ByteList bytes, RegexpOptions options, boolean asciiOnly) {
196-
Map<ByteList, Regex> cache = quotedPatternCache.get();
197-
Regex regex = cache.get(bytes);
180+
Regex regex = quotedPatternCache.get(bytes);
198181
Encoding enc = asciiOnly ? USASCIIEncoding.INSTANCE : bytes.getEncoding();
199182
if (regex != null && regex.getEncoding() == enc && regex.getOptions() == options.toJoniOptions()) return regex;
200183
ByteList quoted = quote19(bytes, asciiOnly);
201184
regex = makeRegexp(runtime, quoted, options, quoted.getEncoding());
202185
regex.setUserObject(quoted);
203-
cache.put(bytes, regex);
186+
quotedPatternCache.put(bytes, regex);
204187
return regex;
205188
}
206189

207190
private static Regex getPreprocessedRegexpFromCache(Ruby runtime, ByteList bytes, Encoding enc, RegexpOptions options, ErrorMode mode) {
208-
Map<ByteList, Regex> cache = preprocessedPatternCache.get();
209-
Regex regex = cache.get(bytes);
191+
Regex regex = preprocessedPatternCache.get(bytes);
210192
if (regex != null && regex.getEncoding() == enc && regex.getOptions() == options.toJoniOptions()) return regex;
211193
ByteList preprocessed = preprocess(runtime, bytes, enc, new Encoding[]{null}, ErrorMode.RAISE);
212194
regex = makeRegexp(runtime, preprocessed, options, enc);
213195
regex.setUserObject(preprocessed);
214-
cache.put(bytes, regex);
196+
preprocessedPatternCache.put(bytes, regex);
215197
return regex;
216198
}
217199

core/src/main/java/org/jruby/util/collections/WeakValuedIdentityMap.java

+4-36
Original file line numberDiff line numberDiff line change
@@ -32,47 +32,15 @@
3232

3333
import java.lang.ref.ReferenceQueue;
3434
import java.lang.ref.WeakReference;
35+
import java.util.Collections;
3536
import java.util.IdentityHashMap;
3637
import java.util.Map;
3738

3839
/**
3940
* A Map that holds its values weakly and uses object identity for keys.
4041
*/
41-
public class WeakValuedIdentityMap<Key, Value> {
42-
private final ReferenceQueue deadReferences = new ReferenceQueue();
43-
private final Map<Key, KeyedReference<Key, Value>> references = new IdentityHashMap<Key, KeyedReference<Key, Value>>();
44-
45-
public synchronized void put(Key key, Value value) {
46-
cleanReferences();
47-
references.put(key, new KeyedReference(value, key, deadReferences));
48-
}
49-
50-
public synchronized Value get(Key key) {
51-
cleanReferences();
52-
KeyedReference<Key, Value> reference = references.get(key);
53-
if (reference == null) {
54-
return null;
55-
}
56-
return reference.get();
57-
}
58-
59-
private void cleanReferences() {
60-
KeyedReference ref;
61-
while ((ref = (KeyedReference) deadReferences.poll()) != null) {
62-
references.remove((ref.key()));
63-
}
64-
}
65-
66-
private static class KeyedReference<Key, Value> extends WeakReference<Value> {
67-
private final Key key;
68-
69-
public KeyedReference(Value object, Key key, ReferenceQueue queue) {
70-
super(object, queue);
71-
this.key = key;
72-
}
73-
74-
public Key key() {
75-
return key;
76-
}
42+
public class WeakValuedIdentityMap<Key, Value> extends WeakValuedMap<Key, Value> {
43+
protected Map<Key, KeyedReference<Key, Value>> newMap() {
44+
return Collections.synchronizedMap(new IdentityHashMap());
7745
}
7846
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/***** BEGIN LICENSE BLOCK *****
2+
* Version: EPL 1.0/GPL 2.0/LGPL 2.1
3+
*
4+
* The contents of this file are subject to the Eclipse Public
5+
* License Version 1.0 (the "License"); you may not use this file
6+
* except in compliance with the License. You may obtain a copy of
7+
* the License at http://www.eclipse.org/legal/epl-v10.html
8+
*
9+
* Software distributed under the License is distributed on an "AS
10+
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11+
* implied. See the License for the specific language governing
12+
* rights and limitations under the License.
13+
*
14+
* Copyright (C) 2002-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
15+
* Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
16+
* Copyright (C) 2004-2006 Charles O Nutter <headius@headius.com>
17+
* Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
18+
*
19+
* Alternatively, the contents of this file may be used under the terms of
20+
* either of the GNU General Public License Version 2 or later (the "GPL"),
21+
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
22+
* in which case the provisions of the GPL or the LGPL are applicable instead
23+
* of those above. If you wish to allow use of your version of this file only
24+
* under the terms of either the GPL or the LGPL, and not to allow others to
25+
* use your version of this file under the terms of the EPL, indicate your
26+
* decision by deleting the provisions above and replace them with the notice
27+
* and other provisions required by the GPL or the LGPL. If you do not delete
28+
* the provisions above, a recipient may use your version of this file under
29+
* the terms of any one of the EPL, the GPL or the LGPL.
30+
***** END LICENSE BLOCK *****/
31+
package org.jruby.util.collections;
32+
33+
import java.lang.ref.ReferenceQueue;
34+
import java.lang.ref.WeakReference;
35+
import java.util.IdentityHashMap;
36+
import java.util.Map;
37+
import java.util.concurrent.ConcurrentHashMap;
38+
39+
/**
40+
* A Map that holds its values weakly and uses object identity for keys.
41+
*/
42+
public class WeakValuedMap<Key, Value> {
43+
private final ReferenceQueue deadReferences = new ReferenceQueue();
44+
private final Map<Key, KeyedReference<Key, Value>> references = newMap();
45+
46+
public void put(Key key, Value value) {
47+
cleanReferences();
48+
references.put(key, new KeyedReference(value, key, deadReferences));
49+
}
50+
51+
public Value get(Key key) {
52+
cleanReferences();
53+
KeyedReference<Key, Value> reference = references.get(key);
54+
if (reference == null) {
55+
return null;
56+
}
57+
return reference.get();
58+
}
59+
60+
protected Map<Key, KeyedReference<Key, Value>> newMap() {
61+
return new ConcurrentHashMap();
62+
}
63+
64+
protected static class KeyedReference<Key, Value> extends WeakReference<Value> {
65+
private final Key key;
66+
67+
public KeyedReference(Value object, Key key, ReferenceQueue queue) {
68+
super(object, queue);
69+
this.key = key;
70+
}
71+
72+
public Key key() {
73+
return key;
74+
}
75+
}
76+
77+
private void cleanReferences() {
78+
KeyedReference ref;
79+
while ((ref = (KeyedReference) deadReferences.poll()) != null) {
80+
references.remove((ref.key()));
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)