Skip to content

Commit 23fb8d3

Browse files
committed
PackNode is using DSL inlining node
1 parent 153ae07 commit 23fb8d3

File tree

4 files changed

+61
-40
lines changed

4 files changed

+61
-40
lines changed

src/main/java/org/truffleruby/core/array/ArrayNodes.java

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import java.util.Arrays;
1717

1818
import com.oracle.truffle.api.TruffleSafepoint;
19+
import com.oracle.truffle.api.dsl.GenerateCached;
20+
import com.oracle.truffle.api.dsl.GenerateInline;
1921
import com.oracle.truffle.api.dsl.NeverDefault;
2022
import com.oracle.truffle.api.nodes.Node;
2123
import com.oracle.truffle.api.object.Shape;
@@ -1540,89 +1542,101 @@ public void accept(CallBlockNode yieldNode, RubyArray array, Object state, Objec
15401542
@NodeChild(value = "format", type = RubyBaseNodeWithExecute.class)
15411543
@CoreMethod(names = "pack", required = 1)
15421544
@ReportPolymorphism
1543-
public abstract static class PackNode extends CoreMethodNode {
1545+
public abstract static class ArrayPackNode extends CoreMethodNode {
15441546

1545-
@Child private TruffleString.FromByteArrayNode fromByteArrayNode = TruffleString.FromByteArrayNode.create();
1547+
@Specialization
1548+
protected RubyString pack(RubyArray array, Object format,
1549+
@Cached ToStrNode toStrNode,
1550+
@Cached PackNode packNode) {
1551+
final var formatAsString = toStrNode.execute(this, format);
1552+
return packNode.execute(this, array, formatAsString);
1553+
}
1554+
}
1555+
1556+
@GenerateCached(false)
1557+
@GenerateInline
1558+
public abstract static class PackNode extends RubyBaseNode {
1559+
1560+
public abstract RubyString execute(Node node, RubyArray array, Object format);
15461561

15471562
@Specialization(
15481563
guards = {
1549-
"libFormat.isRubyString(formatAsString)",
1550-
"equalNode.execute(libFormat, formatAsString, cachedFormat, cachedEncoding)" },
1564+
"libFormat.isRubyString(format)",
1565+
"equalNode.execute(libFormat, format, cachedFormat, cachedEncoding)" },
15511566
limit = "getCacheLimit()")
1552-
protected RubyString packCached(RubyArray array, Object format,
1553-
@Cached @Shared ToStrNode toStrNode,
1554-
@Bind("toStrNode.execute(this, format)") Object formatAsString,
1567+
protected static RubyString packCached(Node node, RubyArray array, Object format,
15551568
@Cached @Shared InlinedBranchProfile exceptionProfile,
15561569
@Cached @Shared InlinedConditionProfile resizeProfile,
15571570
@Cached @Shared RubyStringLibrary libFormat,
15581571
@Cached @Shared WriteObjectFieldNode writeAssociatedNode,
1559-
@Cached("asTruffleStringUncached(formatAsString)") TruffleString cachedFormat,
1560-
@Cached("libFormat.getEncoding(formatAsString)") RubyEncoding cachedEncoding,
1572+
@Cached @Shared TruffleString.FromByteArrayNode fromByteArrayNode,
1573+
@Cached("asTruffleStringUncached(format)") TruffleString cachedFormat,
1574+
@Cached("libFormat.getEncoding(format)") RubyEncoding cachedEncoding,
15611575
@Cached("cachedFormat.byteLength(cachedEncoding.tencoding)") int cachedFormatLength,
1562-
@Cached("create(compileFormat(getJavaString(formatAsString)))") DirectCallNode callPackNode,
1576+
@Cached("create(compileFormat(node, getJavaString(format)))") DirectCallNode callPackNode,
15631577
@Cached StringHelperNodes.EqualNode equalNode) {
15641578
final BytesResult result;
15651579
try {
15661580
result = (BytesResult) callPackNode.call(
15671581
new Object[]{ array.getStore(), array.size, false, null });
15681582
} catch (FormatException e) {
1569-
exceptionProfile.enter(this);
1570-
throw FormatExceptionTranslator.translate(getContext(), this, e);
1583+
exceptionProfile.enter(node);
1584+
throw FormatExceptionTranslator.translate(getContext(node), node, e);
15711585
}
15721586

1573-
return finishPack(cachedFormatLength, result, resizeProfile, writeAssociatedNode);
1587+
return finishPack(node, cachedFormatLength, result, resizeProfile, writeAssociatedNode, fromByteArrayNode);
15741588
}
15751589

1576-
@Specialization(guards = { "libFormat.isRubyString(formatAsString)" }, replaces = "packCached", limit = "1")
1577-
protected RubyString packUncached(RubyArray array, Object format,
1578-
@Cached @Shared ToStrNode toStrNode,
1579-
@Bind("toStrNode.execute(this, format)") Object formatAsString,
1590+
@Specialization(guards = { "libFormat.isRubyString(format)" }, replaces = "packCached", limit = "1")
1591+
protected static RubyString packUncached(Node node, RubyArray array, Object format,
15801592
@Cached @Shared InlinedBranchProfile exceptionProfile,
15811593
@Cached @Shared InlinedConditionProfile resizeProfile,
15821594
@Cached @Shared RubyStringLibrary libFormat,
15831595
@Cached @Shared WriteObjectFieldNode writeAssociatedNode,
1596+
@Cached @Shared TruffleString.FromByteArrayNode fromByteArrayNode,
15841597
@Cached ToJavaStringNode toJavaStringNode,
15851598
@Cached IndirectCallNode callPackNode) {
1586-
final String formatString = toJavaStringNode.execute(formatAsString);
1599+
final String formatString = toJavaStringNode.execute(format);
15871600

15881601
final BytesResult result;
15891602
try {
15901603
result = (BytesResult) callPackNode.call(
1591-
compileFormat(formatString),
1604+
compileFormat(node, formatString),
15921605
new Object[]{ array.getStore(), array.size, false, null });
15931606
} catch (FormatException e) {
1594-
exceptionProfile.enter(this);
1595-
throw FormatExceptionTranslator.translate(getContext(), this, e);
1607+
exceptionProfile.enter(node);
1608+
throw FormatExceptionTranslator.translate(getContext(node), node, e);
15961609
}
15971610

1598-
int formatLength = libFormat.getTString(formatAsString).byteLength(libFormat.getTEncoding(formatAsString));
1599-
return finishPack(formatLength, result, resizeProfile, writeAssociatedNode);
1611+
int formatLength = libFormat.getTString(format).byteLength(libFormat.getTEncoding(format));
1612+
return finishPack(node, formatLength, result, resizeProfile, writeAssociatedNode, fromByteArrayNode);
16001613
}
16011614

1602-
private RubyString finishPack(int formatLength, BytesResult result, InlinedConditionProfile resizeProfile,
1603-
WriteObjectFieldNode writeAssociatedNode) {
1615+
private static RubyString finishPack(Node node, int formatLength, BytesResult result,
1616+
InlinedConditionProfile resizeProfile,
1617+
WriteObjectFieldNode writeAssociatedNode, TruffleString.FromByteArrayNode fromByteArrayNode) {
16041618
byte[] bytes = result.getOutput();
16051619

1606-
if (resizeProfile.profile(this, bytes.length != result.getOutputLength())) {
1620+
if (resizeProfile.profile(node, bytes.length != result.getOutputLength())) {
16071621
bytes = Arrays.copyOf(bytes, result.getOutputLength());
16081622
}
16091623

16101624
final RubyEncoding rubyEncoding = result.getEncoding().getEncodingForLength(formatLength);
1611-
final RubyString string = createString(fromByteArrayNode, bytes, rubyEncoding);
1625+
final RubyString string = createString(node, fromByteArrayNode, bytes, rubyEncoding);
16121626

16131627
if (result.getAssociated() != null) {
1614-
writeAssociatedNode.execute(this, string, Layouts.ASSOCIATED_IDENTIFIER, result.getAssociated());
1628+
writeAssociatedNode.execute(node, string, Layouts.ASSOCIATED_IDENTIFIER, result.getAssociated());
16151629
}
16161630

16171631
return string;
16181632
}
16191633

16201634
@TruffleBoundary
1621-
protected RootCallTarget compileFormat(String format) {
1635+
protected static RootCallTarget compileFormat(Node node, String format) {
16221636
try {
1623-
return new PackCompiler(getLanguage(), this).compile(format);
1637+
return new PackCompiler(getLanguage(node), node).compile(format);
16241638
} catch (DeferredRaiseException dre) {
1625-
throw dre.getException(getContext());
1639+
throw dre.getException(getContext(node));
16261640
}
16271641
}
16281642

src/main/java/org/truffleruby/core/exception/CoreExceptions.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ public RubyException argumentErrorProcWithoutBlock(RubyBaseNode currentNode) {
134134
null);
135135
}
136136

137-
public RubyException argumentErrorTooFewArguments(RubyBaseNode currentNode) {
138-
return argumentError(coreStrings().TOO_FEW_ARGUMENTS.createInstance(currentNode.getContext()), currentNode,
137+
public RubyException argumentErrorTooFewArguments(Node currentNode) {
138+
return argumentError(coreStrings().TOO_FEW_ARGUMENTS.createInstance(RubyContext.get(currentNode)), currentNode,
139139
null);
140140
}
141141

@@ -144,13 +144,15 @@ public RubyException argumentErrorTimeIntervalPositive(RubyBaseNode currentNode)
144144
currentNode, null);
145145
}
146146

147-
public RubyException argumentErrorXOutsideOfString(RubyBaseNode currentNode) {
148-
return argumentError(coreStrings().X_OUTSIDE_OF_STRING.createInstance(currentNode.getContext()), currentNode,
147+
public RubyException argumentErrorXOutsideOfString(Node currentNode) {
148+
return argumentError(coreStrings().X_OUTSIDE_OF_STRING.createInstance(RubyContext.get(currentNode)),
149+
currentNode,
149150
null);
150151
}
151152

152-
public RubyException argumentErrorCantCompressNegativeNumbers(RubyBaseNode currentNode) {
153-
return argumentError(coreStrings().CANT_COMPRESS_NEGATIVE.createInstance(currentNode.getContext()), currentNode,
153+
public RubyException argumentErrorCantCompressNegativeNumbers(Node currentNode) {
154+
return argumentError(coreStrings().CANT_COMPRESS_NEGATIVE.createInstance(RubyContext.get(currentNode)),
155+
currentNode,
154156
null);
155157
}
156158

src/main/java/org/truffleruby/core/format/FormatExceptionTranslator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import com.oracle.truffle.api.CompilerDirectives;
1313
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
14+
import com.oracle.truffle.api.nodes.Node;
1415
import org.truffleruby.RubyContext;
1516
import org.truffleruby.core.exception.CoreExceptions;
1617
import org.truffleruby.core.format.exceptions.CantCompressNegativeException;
@@ -21,13 +22,12 @@
2122
import org.truffleruby.core.format.exceptions.OutsideOfStringException;
2223
import org.truffleruby.core.format.exceptions.RangeException;
2324
import org.truffleruby.core.format.exceptions.TooFewArgumentsException;
24-
import org.truffleruby.language.RubyBaseNode;
2525
import org.truffleruby.language.control.RaiseException;
2626

2727
public abstract class FormatExceptionTranslator {
2828

2929
@TruffleBoundary
30-
public static RuntimeException translate(RubyContext context, RubyBaseNode currentNode, FormatException exception) {
30+
public static RuntimeException translate(RubyContext context, Node currentNode, FormatException exception) {
3131
final CoreExceptions coreExceptions = context.getCoreExceptions();
3232

3333
if (exception instanceof TooFewArgumentsException) {

src/main/java/org/truffleruby/language/RubyBaseNode.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,13 @@ public final RubyString createString(TStringWithEncoding tStringWithEncoding) {
272272

273273
protected final RubyString createString(TruffleString.FromByteArrayNode fromByteArrayNode, byte[] bytes,
274274
RubyEncoding encoding) {
275+
return createString(this, fromByteArrayNode, bytes, encoding);
276+
}
277+
278+
protected static RubyString createString(Node node, TruffleString.FromByteArrayNode fromByteArrayNode, byte[] bytes,
279+
RubyEncoding encoding) {
275280
var tstring = fromByteArrayNode.execute(bytes, encoding.tencoding, false);
276-
return createString(tstring, encoding);
281+
return createString(node, tstring, encoding);
277282
}
278283

279284
public final RubyString createString(TruffleString.FromJavaStringNode fromJavaStringNode, String javaString,

0 commit comments

Comments
 (0)