Skip to content

Commit 1be8c38

Browse files
committed
[GR-19220] Add more comprehensive regexp instrumentation (#2430)
PullRequest: truffleruby/2890
2 parents 4f2e278 + 30fd5fb commit 1be8c38

File tree

10 files changed

+545
-44
lines changed

10 files changed

+545
-44
lines changed

src/main/java/org/truffleruby/core/hash/HashOperations.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,19 @@
99
*/
1010
package org.truffleruby.core.hash;
1111

12+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
1213
import org.truffleruby.RubyContext;
1314
import org.truffleruby.RubyLanguage;
1415
import org.truffleruby.core.hash.library.EmptyHashStore;
16+
import org.truffleruby.core.hash.library.HashStoreLibrary;
1517
import org.truffleruby.core.numeric.BigIntegerOps;
1618
import org.truffleruby.core.numeric.RubyBignum;
1719
import org.truffleruby.language.RubyBaseNode;
1820

21+
import java.util.Map;
22+
import java.util.Optional;
23+
import java.util.function.Function;
24+
1925
public abstract class HashOperations {
2026

2127
public static RubyHash newEmptyHash(RubyContext context, RubyLanguage language) {
@@ -27,6 +33,22 @@ public static RubyHash newEmptyHash(RubyContext context, RubyLanguage language)
2733
0);
2834
}
2935

36+
@TruffleBoundary
37+
public static <K, V> RubyHash toRubyHash(RubyContext context, RubyLanguage language,
38+
HashStoreLibrary hashStoreLibrary, Map<K, V> map, Optional<Function<K, Object>> keyMapper,
39+
Optional<Function<V, Object>> valueMapper, boolean byIdentity) {
40+
final RubyHash ret = newEmptyHash(context, language);
41+
42+
for (Map.Entry<K, V> entry : map.entrySet()) {
43+
final Object key = keyMapper.isPresent() ? keyMapper.get().apply(entry.getKey()) : entry.getKey();
44+
final Object value = valueMapper.isPresent() ? valueMapper.get().apply(entry.getValue()) : entry.getValue();
45+
46+
hashStoreLibrary.set(ret.store, ret, key, value, byIdentity);
47+
}
48+
49+
return ret;
50+
}
51+
3052
// random number, stops hashes for similar values but different classes being the same, static because we want deterministic hashes
3153
public static final int BOOLEAN_CLASS_SALT = 55927484;
3254
public static final int INTEGER_CLASS_SALT = 1028093337;

src/main/java/org/truffleruby/core/regexp/InterpolatedRegexpNode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ protected RubyRegexp createRegexp(RopeWithEncoding[] strings) {
110110
null,
111111
preprocessed,
112112
options,
113+
false,
113114
this);
114115
} catch (DeferredRaiseException dre) {
115116
throw dre.getException(getContext());

src/main/java/org/truffleruby/core/regexp/RegexpCacheKey.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ public RegexpCacheKey(Rope rope, RubyEncoding encoding, RegexpOptions options, H
3030
this.hashing = hashing;
3131
}
3232

33+
public Rope getRope() {
34+
return rope;
35+
}
36+
37+
public RubyEncoding getEncoding() {
38+
return encoding;
39+
}
40+
41+
public int getJoniOptions() {
42+
return joniOptions;
43+
}
44+
3345
@Override
3446
public int hashCode() {
3547
return hashing.hash(rope.hashCode());
@@ -39,7 +51,7 @@ public int hashCode() {
3951
public boolean equals(Object o) {
4052
if (o instanceof RegexpCacheKey) {
4153
final RegexpCacheKey other = (RegexpCacheKey) o;
42-
return rope.equals(other.rope) && encoding == other.encoding && joniOptions == other.joniOptions;
54+
return encoding == other.encoding && joniOptions == other.joniOptions && rope.equals(other.getRope());
4355
} else {
4456
return false;
4557
}
@@ -48,6 +60,7 @@ public boolean equals(Object o) {
4860
@Override
4961
public String toString() {
5062
return '/' + RopeOperations.decodeOrEscapeBinaryRope(rope) + '/' +
51-
RegexpOptions.fromJoniOptions(joniOptions).toOptionsString();
63+
RegexpOptions.fromJoniOptions(joniOptions).toOptionsString() +
64+
" -- " + RopeOperations.decodeOrEscapeBinaryRope(encoding.name.rope);
5265
}
5366
}

src/main/java/org/truffleruby/core/regexp/RegexpNodes.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public static RubyRegexp create(RubyLanguage language,
6464
null,
6565
new RopeWithEncoding(setSource, setSourceEncoding),
6666
regexpOptions,
67+
false,
6768
currentNode);
6869
return new RubyRegexp(regex, regexpOptions);
6970
}

0 commit comments

Comments
 (0)