Skip to content

Commit f828a77

Browse files
committed
Refactor CheckEncodingNode so stringLibrary.getRope(string) is only PE'd once
1 parent 26ab8dd commit f828a77

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

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

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -85,56 +85,56 @@ public static CheckEncodingNode create() {
8585
return TruffleRegexpNodesFactory.CheckEncodingNodeGen.create();
8686
}
8787

88-
public abstract Encoding executeCheckEncoding(RubyRegexp regexp, Object str);
88+
public final Encoding executeCheckEncoding(RubyRegexp regexp, Object string) {
89+
return executeInternal(regexp, stringLibrary.getRope(string));
90+
}
91+
92+
public abstract Encoding executeInternal(RubyRegexp regexp, Rope rope);
8993

9094
@Specialization(guards = {
91-
"!isSameEncoding(regexp, string)",
92-
"isUSASCII(regexp, string)"
95+
"!isSameEncoding(regexp, rope)",
96+
"isUSASCII(regexp, rope)"
9397
})
94-
protected Encoding checkEncodingAsciiOnly(RubyRegexp regexp, Object string) {
98+
protected Encoding checkEncodingAsciiOnly(RubyRegexp regexp, Rope rope) {
9599
return USASCIIEncoding.INSTANCE;
96100
}
97101

98102
@Specialization(guards = {
99-
"isSameEncoding(regexp, string)"
103+
"isSameEncoding(regexp, rope)"
100104
})
101-
protected Encoding checkEncodingSameEncoding(RubyRegexp regexp, Object string) {
105+
protected Encoding checkEncodingSameEncoding(RubyRegexp regexp, Rope rope) {
102106
return regexp.regex.getEncoding();
103107
}
104108

105109
@Specialization(guards = {
106-
"!isSameEncoding(regexp, string)",
107-
"!isUSASCII(regexp, string)",
108-
"isFixedEncoding(regexp, string)",
110+
"!isSameEncoding(regexp, rope)",
111+
"!isUSASCII(regexp, rope)",
112+
"isFixedEncoding(regexp, rope)",
109113
})
110-
protected Encoding checkEncodingFixedEncoding(RubyRegexp regexp, Object string) {
114+
protected Encoding checkEncodingFixedEncoding(RubyRegexp regexp, Rope rope) {
111115
return regexp.regex.getEncoding();
112116
}
113117

114118
@Specialization(guards = {
115-
"!isSameEncoding(regexp, string)",
116-
"!isUSASCII(regexp, string)",
117-
"!isFixedEncoding(regexp, string)"
119+
"!isSameEncoding(regexp, rope)",
120+
"!isUSASCII(regexp, rope)",
121+
"!isFixedEncoding(regexp, rope)"
118122
})
119-
protected Encoding fallback(RubyRegexp regexp, Object string) {
120-
return stringEncoding(string);
121-
}
122-
123-
protected boolean isUSASCII(RubyRegexp regexp, Object string) {
124-
return regexp.regex.getEncoding() == USASCIIEncoding.INSTANCE &&
125-
codeRangeNode.execute(stringLibrary.getRope(string)) == CodeRange.CR_7BIT;
123+
protected Encoding fallback(RubyRegexp regexp, Rope rope) {
124+
return rope.encoding;
126125
}
127126

128-
protected boolean isFixedEncoding(RubyRegexp regexp, Object string) {
129-
return regexp.options.isFixed() && stringEncoding(string).isAsciiCompatible();
127+
protected boolean isSameEncoding(RubyRegexp regexp, Rope rope) {
128+
return regexp.regex.getEncoding() == rope.encoding;
130129
}
131130

132-
protected boolean isSameEncoding(RubyRegexp regexp, Object string) {
133-
return regexp.regex.getEncoding() == stringEncoding(string);
131+
protected boolean isUSASCII(RubyRegexp regexp, Rope rope) {
132+
return regexp.regex.getEncoding() == USASCIIEncoding.INSTANCE &&
133+
codeRangeNode.execute(rope) == CodeRange.CR_7BIT;
134134
}
135135

136-
protected Encoding stringEncoding(Object string) {
137-
return stringLibrary.getRope(string).getEncoding();
136+
protected boolean isFixedEncoding(RubyRegexp regexp, Rope rope) {
137+
return regexp.options.isFixed() && rope.encoding.isAsciiCompatible();
138138
}
139139

140140
}

0 commit comments

Comments
 (0)