Skip to content

Commit 6b3582d

Browse files
committed
Reimplement RubyStringLibrary manually to save footprint and increase flexibility
1 parent acf3732 commit 6b3582d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+636
-549
lines changed

src/main/.checkstyle_checks.xml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,6 @@
215215
<property name="format" value='"isNil'/>
216216
<property name="message" value="Type the argument as Nil instead of using a positive isNil() guard."/>
217217
</module>
218-
<module name="RegexpSinglelineJava">
219-
<property name="format" value='@CachedLibrary\(".+RubyStringLibrary'/>
220-
<property name="message" value="Use @CachedLibrary(limit = &quot;LIBSTRING_CACHE&quot;) RubyStringLibrary instead."/>
221-
</module>
222218
<module name="RegexpSinglelineJava">
223219
<property name="format" value='@CachedLibrary\(".+\.store"\) ArrayStoreLibrary'/>
224220
<property name="message" value="Bind array stores to avoid races between threads causing dispatch errors."/>
@@ -247,10 +243,6 @@
247243
<property name="format" value='Double\.compare'/>
248244
<property name="message" value="Double.compare() does not match Ruby semantics, use FloatNodes.CompareNode.compareDoubles instead."/>
249245
</module>
250-
<module name="RegexpSinglelineJava">
251-
<property name="format" value='@CachedLibrary\(limit = "\d+"\) RubyStringLibrary'/>
252-
<property name="message" value='Use @CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary instead.'/>
253-
</module>
254246
<module name="RegexpSinglelineJava">
255247
<property name="format" value='GetCodeRangeNode'/>
256248
<property name="message" value='Use GetByteCodeRangeNode instead.'/>

src/main/java/org/truffleruby/cext/CExtNodes.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -642,9 +642,9 @@ protected int codeToMbcLen(int code, RubyEncoding encoding) {
642642
@CoreMethod(names = "rb_enc_codepoint_len", onSingleton = true, required = 1)
643643
public abstract static class RbEncCodePointLenNode extends CoreMethodArrayArgumentsNode {
644644

645-
@Specialization(guards = "strings.isRubyString(string)")
645+
@Specialization(guards = "strings.isRubyString(string)", limit = "1")
646646
protected RubyArray rbEncCodePointLen(Object string,
647-
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary strings,
647+
@Cached RubyStringLibrary strings,
648648
@Cached TruffleString.ByteLengthOfCodePointNode byteLengthOfCodePointNode,
649649
@Cached TruffleString.CodePointAtByteIndexNode codePointAtByteIndexNode,
650650
@Cached BranchProfile errorProfile) {
@@ -730,7 +730,7 @@ public abstract static class RbStrSetLenNode extends CoreMethodArrayArgumentsNod
730730

731731
@Specialization
732732
protected RubyString strSetLen(RubyString string, int newByteLength,
733-
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary libString,
733+
@Cached RubyStringLibrary libString,
734734
@Cached StringToNativeNode stringToNativeNode,
735735
@Cached MutableTruffleString.FromNativePointerNode fromNativePointerNode) {
736736
var pointer = stringToNativeNode.executeToNative(string);
@@ -750,7 +750,7 @@ public abstract static class RbStrResizeNode extends CoreMethodArrayArgumentsNod
750750

751751
@Specialization
752752
protected RubyString rbStrResize(RubyString string, int newByteLength,
753-
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary libString,
753+
@Cached RubyStringLibrary libString,
754754
@Cached StringToNativeNode stringToNativeNode,
755755
@Cached MutableTruffleString.FromNativePointerNode fromNativePointerNode) {
756756
var pointer = stringToNativeNode.executeToNative(string);
@@ -779,7 +779,7 @@ public abstract static class TrStrCapaResizeNode extends CoreMethodArrayArgument
779779

780780
@Specialization
781781
protected RubyString trStrCapaResize(RubyString string, int newCapacity,
782-
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary libString,
782+
@Cached RubyStringLibrary libString,
783783
@Cached StringToNativeNode stringToNativeNode,
784784
@Cached MutableTruffleString.FromNativePointerNode fromNativePointerNode) {
785785
var pointer = stringToNativeNode.executeToNative(string);
@@ -1191,7 +1191,7 @@ public static StringToNativeNode create() {
11911191

11921192
@Specialization
11931193
protected Pointer toNative(RubyString string,
1194-
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary libString,
1194+
@Cached RubyStringLibrary libString,
11951195
@Cached ConditionProfile convertProfile,
11961196
@Cached TruffleString.CopyToNativeMemoryNode copyToNativeMemoryNode,
11971197
@Cached MutableTruffleString.FromNativePointerNode fromNativePointerNode,
@@ -1382,7 +1382,7 @@ public abstract static class RbTrMbcCaseFoldNode extends CoreMethodArrayArgument
13821382

13831383
@Specialization(guards = "strings.isRubyString(string)", limit = "getCacheLimit()")
13841384
protected Object rbTrEncMbcCaseFold(int flags, Object string, Object advance_p, Object p,
1385-
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary strings,
1385+
@Cached RubyStringLibrary strings,
13861386
@CachedLibrary("advance_p") InteropLibrary receivers,
13871387
@Cached TranslateInteropExceptionNode translateInteropExceptionNode,
13881388
@Cached TruffleString.FromByteArrayNode fromByteArrayNode,
@@ -1447,9 +1447,9 @@ protected Object rbEncMinLen(RubyEncoding value) {
14471447

14481448
@CoreMethod(names = "rb_enc_mbclen", onSingleton = true, required = 1)
14491449
public abstract static class RbEncMbLenNode extends CoreMethodArrayArgumentsNode {
1450-
@Specialization(guards = "strings.isRubyString(string)")
1450+
@Specialization(guards = "strings.isRubyString(string)", limit = "1")
14511451
protected Object rbEncMbLen(Object string,
1452-
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary strings,
1452+
@Cached RubyStringLibrary strings,
14531453
@Cached TruffleString.ByteLengthOfCodePointNode byteLengthOfCodePointNode) {
14541454
var tstring = strings.getTString(string);
14551455
var tencoding = strings.getTEncoding(string);
@@ -1459,9 +1459,9 @@ protected Object rbEncMbLen(Object string,
14591459

14601460
@CoreMethod(names = "rb_enc_precise_mbclen", onSingleton = true, required = 1)
14611461
public abstract static class RbEncPreciseMbclenNode extends CoreMethodArrayArgumentsNode {
1462-
@Specialization(guards = "strings.isRubyString(string)")
1462+
@Specialization(guards = "strings.isRubyString(string)", limit = "1")
14631463
protected int rbEncPreciseMbclen(Object string,
1464-
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary strings,
1464+
@Cached RubyStringLibrary strings,
14651465
@Cached TruffleString.ByteLengthOfCodePointNode byteLengthOfCodePointNode) {
14661466
var tstring = strings.getTString(string);
14671467
var tencoding = strings.getTEncoding(string);
@@ -1473,19 +1473,19 @@ protected int rbEncPreciseMbclen(Object string,
14731473
public abstract static class RbEncLeftCharHeadNode extends CoreMethodArrayArgumentsNode {
14741474

14751475
@TruffleBoundary
1476-
@Specialization(guards = "strings.isRubyString(string)")
1476+
@Specialization(guards = "strings.isRubyString(string)", limit = "1")
14771477
protected Object rbEncLeftCharHead(RubyEncoding enc, Object string, int p,
1478-
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary strings) {
1478+
@Cached RubyStringLibrary strings) {
14791479
byte[] bytes = TStringUtils.getBytesOrFail(strings.getTString(string), strings.getEncoding(string));
14801480
return enc.jcoding.leftAdjustCharHead(bytes, 0, p, bytes.length);
14811481
}
14821482
}
14831483

14841484
@CoreMethod(names = "rb_enc_mbc_to_codepoint", onSingleton = true, required = 1)
14851485
public abstract static class RbEncMbcToCodepointNode extends CoreMethodArrayArgumentsNode {
1486-
@Specialization(guards = "strings.isRubyString(string)")
1486+
@Specialization(guards = "strings.isRubyString(string)", limit = "1")
14871487
protected int rbEncMbcToCodepoint(Object string,
1488-
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary strings,
1488+
@Cached RubyStringLibrary strings,
14891489
@Cached TruffleString.CodePointAtByteIndexNode codePointAtByteIndexNode,
14901490
@Cached TruffleString.GetInternalByteArrayNode byteArrayNode,
14911491
@Cached ConditionProfile brokenProfile) {
@@ -1684,9 +1684,9 @@ protected Object wrapFunction() {
16841684

16851685
@CoreMethod(names = "rb_check_symbol_cstr", onSingleton = true, required = 1)
16861686
public abstract static class RbCheckSymbolCStrNode extends CoreMethodArrayArgumentsNode {
1687-
@Specialization(guards = "strings.isRubyString(string)")
1687+
@Specialization(guards = "strings.isRubyString(string)", limit = "1")
16881688
protected Object checkSymbolCStr(Object string,
1689-
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary strings) {
1689+
@Cached RubyStringLibrary strings) {
16901690
final RubySymbol sym = getLanguage().symbolTable.getSymbolIfExists(
16911691
strings.getTString(string),
16921692
strings.getEncoding(string));
@@ -1717,17 +1717,17 @@ public abstract static class RBSprintfFormatNode extends CoreMethodArrayArgument
17171717
"equalNode.execute(libFormat, format, cachedFormat, cachedEncoding)" },
17181718
limit = "2")
17191719
protected Object typesCached(VirtualFrame frame, Object format,
1720-
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary libFormat,
1720+
@Cached RubyStringLibrary libFormat,
17211721
@Cached("asTruffleStringUncached(format)") TruffleString cachedFormat,
17221722
@Cached("libFormat.getEncoding(format)") RubyEncoding cachedEncoding,
17231723
@Cached("compileArgTypes(cachedFormat, cachedEncoding, byteArrayNode)") RubyArray cachedTypes,
17241724
@Cached StringHelperNodes.EqualSameEncodingNode equalNode) {
17251725
return cachedTypes;
17261726
}
17271727

1728-
@Specialization(guards = "libFormat.isRubyString(format)")
1728+
@Specialization(guards = "libFormat.isRubyString(format)", limit = "1")
17291729
protected RubyArray typesUncached(VirtualFrame frame, Object format,
1730-
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary libFormat) {
1730+
@Cached RubyStringLibrary libFormat) {
17311731
return compileArgTypes(libFormat.getTString(format), libFormat.getEncoding(format), byteArrayNode);
17321732
}
17331733

@@ -1762,7 +1762,7 @@ protected RubyString formatCached(Object format, Object stringReader, RubyArray
17621762
@Cached ArrayToObjectArrayNode arrayToObjectArrayNode,
17631763
@Cached WrapNode wrapNode,
17641764
@Cached UnwrapNode unwrapNode,
1765-
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary libFormat,
1765+
@Cached RubyStringLibrary libFormat,
17661766
@Cached("asTruffleStringUncached(format)") TruffleString cachedFormat,
17671767
@Cached("libFormat.getEncoding(format)") RubyEncoding cachedEncoding,
17681768
@Cached("cachedFormat.byteLength(cachedEncoding.tencoding)") int cachedFormatLength,
@@ -1782,14 +1782,14 @@ protected RubyString formatCached(Object format, Object stringReader, RubyArray
17821782

17831783
@Specialization(
17841784
guards = "libFormat.isRubyString(format)",
1785-
replaces = "formatCached")
1785+
replaces = "formatCached", limit = "1")
17861786
protected RubyString formatUncached(Object format, Object stringReader, RubyArray argArray,
17871787
@Cached TranslateInteropExceptionNode translateInteropExceptionNode,
17881788
@Cached WrapNode wrapNode,
17891789
@Cached UnwrapNode unwrapNode,
17901790
@Cached IndirectCallNode formatNode,
17911791
@Cached ArrayToObjectArrayNode arrayToObjectArrayNode,
1792-
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary libFormat) {
1792+
@Cached RubyStringLibrary libFormat) {
17931793
var tstring = libFormat.getTString(format);
17941794
var encoding = libFormat.getEncoding(format);
17951795
final Object[] arguments = arrayToObjectArrayNode.executeToObjectArray(argArray);

src/main/java/org/truffleruby/core/TruffleSystemNodes.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import java.util.Set;
4444
import java.util.logging.Level;
4545

46-
import com.oracle.truffle.api.library.CachedLibrary;
4746
import com.oracle.truffle.api.strings.TruffleString;
4847
import org.truffleruby.RubyLanguage;
4948
import org.truffleruby.builtins.CoreMethod;
@@ -99,9 +98,9 @@ protected RubyArray envVars() {
9998
@Primitive(name = "java_get_env")
10099
public abstract static class JavaGetEnv extends CoreMethodArrayArgumentsNode {
101100

102-
@Specialization(guards = "strings.isRubyString(name)")
101+
@Specialization(guards = "strings.isRubyString(name)", limit = "1")
103102
protected Object javaGetEnv(Object name,
104-
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary strings,
103+
@Cached RubyStringLibrary strings,
105104
@Cached ToJavaStringNode toJavaStringNode,
106105
@Cached FromJavaStringNode fromJavaStringNode,
107106
@Cached ConditionProfile nullValueProfile) {
@@ -126,9 +125,9 @@ private String getEnv(String name) {
126125
public abstract static class SetTruffleWorkingDirNode extends PrimitiveArrayArgumentsNode {
127126

128127
@TruffleBoundary
129-
@Specialization(guards = "stringsDir.isRubyString(dir)")
128+
@Specialization(guards = "stringsDir.isRubyString(dir)", limit = "1")
130129
protected Object setTruffleWorkingDir(Object dir,
131-
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary stringsDir) {
130+
@Cached RubyStringLibrary stringsDir) {
132131
TruffleFile truffleFile = getContext()
133132
.getEnv()
134133
.getPublicTruffleFile(RubyGuards.getJavaString(dir));
@@ -182,9 +181,9 @@ public abstract static class GetJavaPropertyNode extends CoreMethodArrayArgument
182181

183182
@Child private TruffleString.FromJavaStringNode fromJavaStringNode = TruffleString.FromJavaStringNode.create();
184183

185-
@Specialization(guards = "strings.isRubyString(property)")
184+
@Specialization(guards = "strings.isRubyString(property)", limit = "1")
186185
protected Object getJavaProperty(Object property,
187-
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary strings,
186+
@Cached RubyStringLibrary strings,
188187
@Cached ToJavaStringNode toJavaStringNode) {
189188
String value = getProperty(toJavaStringNode.executeToJavaString(property));
190189
if (value == null) {
@@ -227,19 +226,19 @@ protected RubyString hostOS() {
227226
@CoreMethod(names = "log", onSingleton = true, required = 2)
228227
public abstract static class LogNode extends CoreMethodArrayArgumentsNode {
229228

230-
@Specialization(guards = { "strings.isRubyString(message)", "level == cachedLevel" })
229+
@Specialization(guards = { "strings.isRubyString(message)", "level == cachedLevel" }, limit = "3")
231230
protected Object logCached(RubySymbol level, Object message,
232-
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary strings,
231+
@Cached RubyStringLibrary strings,
233232
@Cached ToJavaStringNode toJavaStringNode,
234233
@Cached("level") RubySymbol cachedLevel,
235234
@Cached("getLevel(cachedLevel)") Level javaLevel) {
236235
log(javaLevel, toJavaStringNode.executeToJavaString(message));
237236
return nil;
238237
}
239238

240-
@Specialization(guards = "strings.isRubyString(message)", replaces = "logCached")
239+
@Specialization(guards = "strings.isRubyString(message)", replaces = "logCached", limit = "1")
241240
protected Object log(RubySymbol level, Object message,
242-
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary strings,
241+
@Cached RubyStringLibrary strings,
243242
@Cached ToJavaStringNode toJavaStringNode) {
244243
log(getLevel(level), toJavaStringNode.executeToJavaString(message));
245244
return nil;

src/main/java/org/truffleruby/core/VMPrimitiveNodes.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,11 @@ protected Object doThrow(Object tag, Object value) {
268268
public abstract static class VMWatchSignalNode extends PrimitiveArrayArgumentsNode {
269269

270270
@TruffleBoundary
271-
@Specialization(guards = { "libSignalString.isRubyString(signalString)", "libAction.isRubyString(action)" })
271+
@Specialization(guards = { "libSignalString.isRubyString(signalString)", "libAction.isRubyString(action)" },
272+
limit = "1")
272273
protected boolean watchSignalString(Object signalString, boolean isRubyDefaultHandler, Object action,
273-
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary libSignalString,
274-
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary libAction) {
274+
@Cached RubyStringLibrary libSignalString,
275+
@Cached RubyStringLibrary libAction) {
275276
final String actionString = RubyGuards.getJavaString(action);
276277
final String signalName = RubyGuards.getJavaString(signalString);
277278

@@ -288,9 +289,9 @@ protected boolean watchSignalString(Object signalString, boolean isRubyDefaultHa
288289
}
289290

290291
@TruffleBoundary
291-
@Specialization(guards = "libSignalString.isRubyString(signalString)")
292+
@Specialization(guards = "libSignalString.isRubyString(signalString)", limit = "1")
292293
protected boolean watchSignalProc(Object signalString, boolean isRubyDefaultHandler, RubyProc action,
293-
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary libSignalString) {
294+
@Cached RubyStringLibrary libSignalString) {
294295
final RubyContext context = getContext();
295296

296297
if (getLanguage().getCurrentThread() != context.getThreadManager().getRootThread()) {
@@ -560,9 +561,9 @@ protected Object initStackOverflowClassesEagerly() {
560561
@Primitive(name = "should_not_reach_here")
561562
public abstract static class ShouldNotReachHereNode extends PrimitiveArrayArgumentsNode {
562563

563-
@Specialization(guards = "libString.isRubyString(message)")
564+
@Specialization(guards = "libString.isRubyString(message)", limit = "1")
564565
protected Object shouldNotReachHere(Object message,
565-
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary libString) {
566+
@Cached RubyStringLibrary libString) {
566567
CompilerDirectives.transferToInterpreterAndInvalidate();
567568
throw CompilerDirectives.shouldNotReachHere(RubyGuards.getJavaString(message));
568569
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,7 +1555,7 @@ protected ToStrNode coerceFormat(RubyBaseNodeWithExecute format) {
15551555
"equalNode.execute(libFormat, format, cachedFormat, cachedEncoding)" },
15561556
limit = "getCacheLimit()")
15571557
protected RubyString packCached(RubyArray array, Object format,
1558-
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary libFormat,
1558+
@Cached RubyStringLibrary libFormat,
15591559
@Cached("asTruffleStringUncached(format)") TruffleString cachedFormat,
15601560
@Cached("libFormat.getEncoding(format)") RubyEncoding cachedEncoding,
15611561
@Cached("cachedFormat.byteLength(cachedEncoding.tencoding)") int cachedFormatLength,
@@ -1573,9 +1573,9 @@ protected RubyString packCached(RubyArray array, Object format,
15731573
return finishPack(cachedFormatLength, result);
15741574
}
15751575

1576-
@Specialization(guards = { "libFormat.isRubyString(format)" }, replaces = "packCached")
1576+
@Specialization(guards = { "libFormat.isRubyString(format)" }, replaces = "packCached", limit = "1")
15771577
protected RubyString packUncached(RubyArray array, Object format,
1578-
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary libFormat,
1578+
@Cached RubyStringLibrary libFormat,
15791579
@Cached ToJavaStringNode toJavaStringNode,
15801580
@Cached IndirectCallNode callPackNode) {
15811581
final String formatRope = toJavaStringNode.executeToJavaString(format);

0 commit comments

Comments
 (0)