Skip to content

Commit 87763e9

Browse files
committed
Refactor AllocateNode node to support DSL inlining
1 parent 4eff8c1 commit 87763e9

File tree

3 files changed

+46
-27
lines changed

3 files changed

+46
-27
lines changed

src/main/java/org/truffleruby/core/kernel/KernelNodes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@
8282
import org.truffleruby.core.proc.RubyProc;
8383
import org.truffleruby.core.range.RangeNodes;
8484
import org.truffleruby.core.range.RubyIntOrLongRange;
85+
import org.truffleruby.core.string.AllocateNode;
8586
import org.truffleruby.core.string.RubyString;
8687
import org.truffleruby.core.string.StringHelperNodes;
87-
import org.truffleruby.core.string.StringNodes;
8888
import org.truffleruby.core.support.TypeNodes;
8989
import org.truffleruby.core.support.TypeNodes.CheckFrozenNode;
9090
import org.truffleruby.core.support.TypeNodes.ObjectInstanceVariablesNode;
@@ -539,7 +539,7 @@ protected RubyDynamicObject copyRubyClass(RubyClass self,
539539

540540
@Specialization
541541
protected RubyDynamicObject copy(ImmutableRubyString string,
542-
@Cached StringNodes.AllocateNode allocateStringNode) {
542+
@Cached AllocateNode allocateStringNode) {
543543
return allocateStringNode.execute(coreLibrary().stringClass);
544544
}
545545

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. This
3+
* code is released under a tri EPL/GPL/LGPL license. You can use it,
4+
* redistribute it and/or modify it under the terms of the:
5+
*
6+
* Eclipse Public License version 2.0, or
7+
* GNU General Public License version 2, or
8+
* GNU Lesser General Public License version 2.1.
9+
*/
10+
package org.truffleruby.core.string;
11+
12+
import com.oracle.truffle.api.dsl.GenerateUncached;
13+
import com.oracle.truffle.api.dsl.Specialization;
14+
import org.truffleruby.core.encoding.Encodings;
15+
import org.truffleruby.core.klass.RubyClass;
16+
import org.truffleruby.language.RubyBaseNode;
17+
import org.truffleruby.language.objects.AllocationTracing;
18+
19+
import static org.truffleruby.core.string.TStringConstants.EMPTY_BINARY;
20+
21+
@GenerateUncached
22+
public abstract class AllocateNode extends RubyBaseNode {
23+
24+
public abstract RubyString execute(RubyClass rubyClass);
25+
26+
@Specialization
27+
protected RubyString allocate(RubyClass rubyClass) {
28+
final RubyString string = new RubyString(
29+
rubyClass,
30+
getLanguage().stringShape,
31+
false,
32+
EMPTY_BINARY,
33+
Encodings.BINARY);
34+
AllocationTracing.trace(string, this);
35+
return string;
36+
}
37+
}

src/main/java/org/truffleruby/core/string/StringNodes.java

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@
155155
import org.truffleruby.language.control.RaiseException;
156156
import org.truffleruby.language.dispatch.DispatchNode;
157157
import org.truffleruby.language.library.RubyStringLibrary;
158-
import org.truffleruby.language.objects.AllocationTracing;
159158
import org.truffleruby.language.objects.WriteObjectFieldNode;
160159
import org.truffleruby.language.threadlocal.SpecialVariableStorage;
161160
import org.truffleruby.language.yield.CallBlockNode;
@@ -167,7 +166,6 @@
167166
import com.oracle.truffle.api.dsl.Cached;
168167
import com.oracle.truffle.api.dsl.CreateCast;
169168
import com.oracle.truffle.api.dsl.Fallback;
170-
import com.oracle.truffle.api.dsl.GenerateUncached;
171169
import com.oracle.truffle.api.dsl.ImportStatic;
172170
import com.oracle.truffle.api.dsl.NodeChild;
173171
import com.oracle.truffle.api.dsl.ReportPolymorphism;
@@ -182,40 +180,24 @@
182180
@CoreModule(value = "String", isClass = true)
183181
public abstract class StringNodes {
184182

185-
@GenerateUncached
186183
@GenerateNodeFactory
187184
@CoreMethod(names = { "__allocate__", "__layout_allocate__" }, constructor = true, visibility = Visibility.PRIVATE)
188185
@NodeChild(value = "rubyClassNode", type = RubyNode.class)
189-
public abstract static class AllocateNode extends RubySourceNode {
190-
191-
@NeverDefault
192-
public static AllocateNode create() {
193-
return StringNodesFactory.AllocateNodeFactory.create(null);
194-
}
195-
196-
public static AllocateNode create(RubyNode rubyClassNode) {
197-
return StringNodesFactory.AllocateNodeFactory.create(rubyClassNode);
198-
}
199-
200-
public abstract RubyString execute(RubyClass rubyClass);
186+
public abstract static class StringAllocateNode extends RubySourceNode {
201187

202188
abstract RubyNode getRubyClassNode();
203189

204190
@Specialization
205-
protected RubyString allocate(RubyClass rubyClass) {
206-
final RubyString string = new RubyString(
207-
rubyClass,
208-
getLanguage().stringShape,
209-
false,
210-
EMPTY_BINARY,
211-
Encodings.BINARY);
212-
AllocationTracing.trace(string, this);
213-
return string;
191+
protected RubyString allocate(RubyClass rubyClass,
192+
@Cached AllocateNode allocateNode) {
193+
return allocateNode.execute(rubyClass);
214194
}
215195

196+
216197
@Override
217198
public RubyNode cloneUninitialized() {
218-
return create(getRubyClassNode().cloneUninitialized()).copyFlags(this);
199+
return StringNodesFactory.StringAllocateNodeFactory.create(getRubyClassNode().cloneUninitialized())
200+
.copyFlags(this);
219201
}
220202

221203
}

0 commit comments

Comments
 (0)