Skip to content

Commit d43ae75

Browse files
committed
Refactor NegotiateCompatibleEncodingNode. Encoding moved to callers to reduce number of ToRubyEncodingNode#execute calls
1 parent 7a90daa commit d43ae75

File tree

1 file changed

+55
-61
lines changed

1 file changed

+55
-61
lines changed

src/main/java/org/truffleruby/core/encoding/EncodingNodes.java

Lines changed: 55 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -222,46 +222,41 @@ protected boolean isStandardEncoding(RubyEncoding encoding) {
222222
/** Use {@link NegotiateCompatibleStringEncodingNode} instead if both arguments are always Strings, for footprint */
223223
public abstract static class NegotiateCompatibleEncodingNode extends RubyBaseNode {
224224

225-
@Child private TruffleString.GetByteCodeRangeNode codeRangeNode;
226-
227225
@NeverDefault
228226
public static NegotiateCompatibleEncodingNode create() {
229227
return NegotiateCompatibleEncodingNodeGen.create();
230228
}
231229

232-
public abstract RubyEncoding executeNegotiate(Object first, Object second);
230+
public abstract RubyEncoding executeNegotiate(Object first, RubyEncoding firstEncoding, Object second,
231+
RubyEncoding secondEncoding);
233232

234233
@Specialization(
235234
guards = {
236-
"encodingNode.execute(this, first) == cachedEncoding",
237-
"encodingNode.execute(this, second) == cachedEncoding",
235+
"firstEncoding == cachedEncoding",
236+
"secondEncoding == cachedEncoding",
238237
"cachedEncoding != null" },
239238
limit = "getCacheLimit()")
240-
protected RubyEncoding negotiateSameEncodingCached(Object first, Object second,
241-
@Cached @Shared ToRubyEncodingNode encodingNode,
242-
@Cached("encodingNode.execute(this, first)") RubyEncoding cachedEncoding) {
239+
protected RubyEncoding negotiateSameEncodingCached(
240+
Object first, RubyEncoding firstEncoding, Object second, RubyEncoding secondEncoding,
241+
@Cached("firstEncoding") RubyEncoding cachedEncoding) {
243242
return cachedEncoding;
244243
}
245244

246245
@Specialization(
247246
guards = { "firstEncoding == secondEncoding", "firstEncoding != null" },
248-
replaces = "negotiateSameEncodingCached",
249-
limit = "1")
250-
protected RubyEncoding negotiateSameEncodingUncached(Object first, Object second,
251-
@Cached @Shared ToRubyEncodingNode encodingNode,
252-
@Bind("encodingNode.execute(this, first)") RubyEncoding firstEncoding,
253-
@Bind("encodingNode.execute(this, second)") RubyEncoding secondEncoding) {
254-
return encodingNode.execute(this, first);
247+
replaces = "negotiateSameEncodingCached")
248+
protected RubyEncoding negotiateSameEncodingUncached(
249+
Object first, RubyEncoding firstEncoding, Object second, RubyEncoding secondEncoding) {
250+
return firstEncoding;
255251
}
256252

257253
@Specialization(guards = { "libFirst.isRubyString(first)", "libSecond.isRubyString(second)" }, limit = "1")
258-
protected static RubyEncoding negotiateStringStringEncoding(Object first, Object second,
254+
protected static RubyEncoding negotiateStringStringEncoding(
255+
Object first, RubyEncoding firstEncoding, Object second, RubyEncoding secondEncoding,
259256
@Cached @Shared RubyStringLibrary libFirst,
260257
@Cached @Shared RubyStringLibrary libSecond,
261258
@Cached NegotiateCompatibleStringEncodingNode negotiateNode,
262259
@Bind("this") Node node) {
263-
final RubyEncoding firstEncoding = libFirst.getEncoding(first);
264-
final RubyEncoding secondEncoding = libSecond.getEncoding(second);
265260
return negotiateNode.execute(
266261
node,
267262
libFirst.getTString(first),
@@ -274,20 +269,20 @@ protected static RubyEncoding negotiateStringStringEncoding(Object first, Object
274269
guards = {
275270
"libFirst.isRubyString(first)",
276271
"isNotRubyString(second)",
277-
"getCodeRange(first, libFirst) == codeRange",
272+
"codeRange == codeRangeCached",
278273
"firstEncoding == firstEncodingCached",
279274
"secondEncoding == secondEncodingCached",
280275
"firstEncodingCached != secondEncodingCached" },
281276
limit = "getCacheLimit()")
282-
protected RubyEncoding negotiateStringObjectCached(Object first, Object second,
283-
@Cached @Shared ToRubyEncodingNode encodingNode,
277+
protected RubyEncoding negotiateStringObjectCached(
278+
Object first, RubyEncoding firstEncoding, Object second, RubyEncoding secondEncoding,
284279
@Cached @Shared RubyStringLibrary libFirst,
285-
@Bind("encodingNode.execute(this, first)") RubyEncoding firstEncoding,
286-
@Bind("encodingNode.execute(this, second)") RubyEncoding secondEncoding,
287-
@Cached("encodingNode.execute(this, first)") RubyEncoding firstEncodingCached,
288-
@Cached("encodingNode.execute(this, second)") RubyEncoding secondEncodingCached,
289-
@Cached("getCodeRange(first, libFirst)") TruffleString.CodeRange codeRange,
290-
@Cached("negotiateStringObjectUncached(first, second, encodingNode, firstEncoding, secondEncoding, libFirst)") RubyEncoding negotiatedEncoding) {
280+
@Cached("firstEncoding") RubyEncoding firstEncodingCached,
281+
@Cached("secondEncoding") RubyEncoding secondEncodingCached,
282+
@Cached @Shared TruffleString.GetByteCodeRangeNode codeRangeNode,
283+
@Bind("getCodeRange(codeRangeNode, first, libFirst)") TruffleString.CodeRange codeRange,
284+
@Cached("codeRange") TruffleString.CodeRange codeRangeCached,
285+
@Cached("negotiateStringObjectUncached(first, firstEncoding, second, secondEncoding, codeRangeNode, libFirst)") RubyEncoding negotiatedEncoding) {
291286
return negotiatedEncoding;
292287
}
293288

@@ -297,10 +292,9 @@ protected RubyEncoding negotiateStringObjectCached(Object first, Object second,
297292
"firstEncoding != secondEncoding",
298293
"isNotRubyString(second)" },
299294
replaces = "negotiateStringObjectCached", limit = "1")
300-
protected RubyEncoding negotiateStringObjectUncached(Object first, Object second,
301-
@Cached @Shared ToRubyEncodingNode encodingNode,
302-
@Bind("encodingNode.execute(this, first)") RubyEncoding firstEncoding,
303-
@Bind("encodingNode.execute(this, second)") RubyEncoding secondEncoding,
295+
protected RubyEncoding negotiateStringObjectUncached(
296+
Object first, RubyEncoding firstEncoding, Object second, RubyEncoding secondEncoding,
297+
@Cached @Shared TruffleString.GetByteCodeRangeNode codeRangeNode,
304298
@Cached @Shared RubyStringLibrary libFirst) {
305299

306300
if (secondEncoding == null) {
@@ -315,7 +309,7 @@ protected RubyEncoding negotiateStringObjectUncached(Object first, Object second
315309
return firstEncoding;
316310
}
317311

318-
if (getCodeRange(first, libFirst) == ASCII) {
312+
if (getCodeRange(codeRangeNode, first, libFirst) == ASCII) {
319313
return secondEncoding;
320314
}
321315

@@ -328,12 +322,12 @@ protected RubyEncoding negotiateStringObjectUncached(Object first, Object second
328322
"firstEncoding != secondEncoding",
329323
"isNotRubyString(first)" },
330324
limit = "1")
331-
protected RubyEncoding negotiateObjectString(Object first, Object second,
332-
@Cached @Shared ToRubyEncodingNode encodingNode,
333-
@Bind("encodingNode.execute(this, first)") RubyEncoding firstEncoding,
334-
@Bind("encodingNode.execute(this, second)") RubyEncoding secondEncoding,
335-
@Cached @Shared RubyStringLibrary libSecond) {
336-
return negotiateStringObjectUncached(second, first, encodingNode, secondEncoding, firstEncoding, libSecond);
325+
protected RubyEncoding negotiateObjectString(
326+
Object first, RubyEncoding firstEncoding, Object second, RubyEncoding secondEncoding,
327+
@Cached @Shared RubyStringLibrary libSecond,
328+
@Cached @Shared TruffleString.GetByteCodeRangeNode codeRangeNode) {
329+
return negotiateStringObjectUncached(second, secondEncoding, first, firstEncoding, codeRangeNode,
330+
libSecond);
337331
}
338332

339333
@Specialization(
@@ -343,14 +337,14 @@ protected RubyEncoding negotiateObjectString(Object first, Object second,
343337
"isNotRubyString(second)",
344338
"firstEncoding != null",
345339
"secondEncoding != null",
346-
"encodingNode.execute(this, first) == firstEncoding",
347-
"encodingNode.execute(this, second) == secondEncoding", },
340+
"firstEncoding == firstEncodingCached",
341+
"secondEncoding == secondEncodingCached", },
348342
limit = "getCacheLimit()")
349-
protected RubyEncoding negotiateObjectObjectCached(Object first, Object second,
350-
@Cached @Shared ToRubyEncodingNode encodingNode,
351-
@Cached("encodingNode.execute(this, first)") RubyEncoding firstEncoding,
352-
@Cached("encodingNode.execute(this, second)") RubyEncoding secondEncoding,
353-
@Cached("areCompatible(firstEncoding, secondEncoding)") RubyEncoding negotiatedEncoding) {
343+
protected RubyEncoding negotiateObjectObjectCached(
344+
Object first, RubyEncoding firstEncoding, Object second, RubyEncoding secondEncoding,
345+
@Cached("firstEncoding") RubyEncoding firstEncodingCached,
346+
@Cached("secondEncoding") RubyEncoding secondEncodingCached,
347+
@Cached("areCompatible(firstEncodingCached, secondEncodingCached)") RubyEncoding negotiatedEncoding) {
354348
return negotiatedEncoding;
355349
}
356350

@@ -359,12 +353,9 @@ protected RubyEncoding negotiateObjectObjectCached(Object first, Object second,
359353
"firstEncoding != secondEncoding",
360354
"isNotRubyString(first)",
361355
"isNotRubyString(second)" },
362-
replaces = "negotiateObjectObjectCached",
363-
limit = "1")
364-
protected RubyEncoding negotiateObjectObjectUncached(Object first, Object second,
365-
@Cached @Shared ToRubyEncodingNode encodingNode,
366-
@Bind("encodingNode.execute(this, first)") RubyEncoding firstEncoding,
367-
@Bind("encodingNode.execute(this, second)") RubyEncoding secondEncoding) {
356+
replaces = "negotiateObjectObjectCached")
357+
protected RubyEncoding negotiateObjectObjectUncached(
358+
Object first, RubyEncoding firstEncoding, Object second, RubyEncoding secondEncoding) {
368359
return areCompatible(firstEncoding, secondEncoding);
369360
}
370361

@@ -390,12 +381,8 @@ protected static RubyEncoding areCompatible(RubyEncoding enc1, RubyEncoding enc2
390381
return null;
391382
}
392383

393-
protected TruffleString.CodeRange getCodeRange(Object string, RubyStringLibrary libString) {
394-
if (codeRangeNode == null) {
395-
CompilerDirectives.transferToInterpreterAndInvalidate();
396-
codeRangeNode = insert(TruffleString.GetByteCodeRangeNode.create());
397-
}
398-
384+
protected TruffleString.CodeRange getCodeRange(TruffleString.GetByteCodeRangeNode codeRangeNode, Object string,
385+
RubyStringLibrary libString) {
399386
return codeRangeNode.execute(libString.getTString(string), libString.getTEncoding(string));
400387
}
401388

@@ -419,8 +406,12 @@ public static CompatibleQueryNode create() {
419406

420407
@Specialization
421408
protected Object isCompatible(Object first, Object second,
409+
@Cached ToRubyEncodingNode toRubyEncodingNode,
422410
@Cached InlinedConditionProfile noNegotiatedEncodingProfile) {
423-
final RubyEncoding negotiatedEncoding = negotiateCompatibleEncodingNode.executeNegotiate(first, second);
411+
final var firstEncoding = toRubyEncodingNode.execute(this, first);
412+
final var secondEncoding = toRubyEncodingNode.execute(this, second);
413+
final RubyEncoding negotiatedEncoding = negotiateCompatibleEncodingNode.executeNegotiate(first,
414+
firstEncoding, second, secondEncoding);
424415

425416
if (noNegotiatedEncodingProfile.profile(this, negotiatedEncoding == null)) {
426417
return nil;
@@ -841,13 +832,16 @@ protected static RubyEncoding checkEncoding(Node node, Object first, Object seco
841832
@Cached ToRubyEncodingNode toRubyEncodingNode,
842833
@Cached(inline = false) NegotiateCompatibleEncodingNode negotiateCompatibleEncodingNode,
843834
@Cached InlinedBranchProfile errorProfile) {
844-
final RubyEncoding negotiatedEncoding = negotiateCompatibleEncodingNode.executeNegotiate(first, second);
835+
final var firstEncoding = toRubyEncodingNode.execute(node, first);
836+
final var secondEncoding = toRubyEncodingNode.execute(node, second);
837+
final RubyEncoding negotiatedEncoding = negotiateCompatibleEncodingNode.executeNegotiate(first,
838+
firstEncoding, second, secondEncoding);
845839

846840
if (negotiatedEncoding == null) {
847841
errorProfile.enter(node);
848842
throw new RaiseException(getContext(node), coreExceptions(node).encodingCompatibilityErrorIncompatible(
849-
toRubyEncodingNode.execute(node, first),
850-
toRubyEncodingNode.execute(node, second),
843+
firstEncoding,
844+
secondEncoding,
851845
node));
852846
}
853847

0 commit comments

Comments
 (0)