Skip to content

Commit a9748eb

Browse files
committed
Refactor rb_lastline to use new fast path and remove old primitive.
1 parent 388f01c commit a9748eb

File tree

3 files changed

+11
-46
lines changed

3 files changed

+11
-46
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Performance:
3636
* Improved `String#gsub` performance by adding a fast path for the `string_byte_index` primitive (#2380, @nirvdrum).
3737
* Improved `String#index` performance by adding a fast path for the `string_character_index` primitive (#2383, @LillianZ).
3838
* Optimized conversion of strings to integers if the string contained a numeric value (#2401, @nirvdrum).
39-
* Provide a new fast path for get and setting backrefs from C extensions.
39+
* Provide a new fast path for `rb_backref*` and `rb_lastline*`functions from C extensions.
4040

4141
Changes:
4242

lib/truffle/truffle/cext.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,16 +1733,18 @@ def rb_class_inherited_p(ruby_module, object)
17331733
end
17341734
end
17351735

1736-
def rb_backref_get
1736+
def rb_get_special_vars
17371737
vars = Primitive.cext_special_variables_from_stack
17381738
vars = Truffle::ThreadOperations.ruby_caller_special_variables([Truffle::CExt, Truffle::Interop.singleton_class]) if vars.nil?
1739-
Primitive.regexp_last_match_get(vars)
1739+
vars
1740+
end
1741+
1742+
def rb_backref_get
1743+
Primitive.regexp_last_match_get(rb_get_special_vars())
17401744
end
17411745

17421746
def rb_backref_set(value)
1743-
vars = Primitive.cext_special_variables_from_stack
1744-
vars = Truffle::ThreadOperations.ruby_caller_special_variables([Truffle::CExt, Truffle::Interop.singleton_class]) if vars.nil?
1745-
Truffle::RegexpOperations::LAST_MATCH_SET.call(value, vars)
1747+
Truffle::RegexpOperations::LAST_MATCH_SET.call(value, rb_get_special_vars())
17461748
end
17471749

17481750
def rb_gv_set(name, value)
@@ -1760,7 +1762,7 @@ def rb_gv_get(name)
17601762

17611763
def rb_reg_match(re, str)
17621764
result = str ? Truffle::RegexpOperations.match(re, str, 0) : nil
1763-
Primitive.regexp_last_match_set(Truffle::ThreadOperations.ruby_caller_special_variables([Truffle::CExt, Truffle::Interop.singleton_class]), result)
1765+
Primitive.regexp_last_match_set(rb_get_special_vars(), result)
17641766

17651767
result.begin(0) if result
17661768
end
@@ -1882,13 +1884,11 @@ def rb_syserr_new(errno, mesg)
18821884
end
18831885

18841886
def rb_lastline_set(str)
1885-
storage = Primitive.ruby_caller_special_variables
1886-
Primitive.io_last_line_set(storage, str)
1887+
Primitive.io_last_line_set(rb_get_special_vars(), str)
18871888
end
18881889

18891890
def rb_lastline_get
1890-
storage = Primitive.ruby_caller_special_variables
1891-
Primitive.io_last_line_get(storage)
1891+
Primitive.io_last_line_get(rb_get_special_vars())
18921892
end
18931893

18941894
def rb_cFiber

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

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import org.truffleruby.language.ReadOwnFrameAndVariablesNode;
3838
import org.truffleruby.language.RubyBaseNode;
3939
import org.truffleruby.language.RubyNode;
40-
import org.truffleruby.language.RubyRootNode;
4140
import org.truffleruby.language.arguments.MaybeReadCallerVariablesNode;
4241
import org.truffleruby.language.arguments.ReadCallerVariablesNode;
4342
import org.truffleruby.language.arguments.RubyArguments;
@@ -62,17 +61,13 @@
6261
import com.oracle.truffle.api.dsl.NodeChild;
6362
import com.oracle.truffle.api.dsl.Specialization;
6463
import com.oracle.truffle.api.frame.FrameDescriptor;
65-
import com.oracle.truffle.api.frame.FrameInstance.FrameAccess;
6664
import com.oracle.truffle.api.frame.FrameSlot;
6765
import com.oracle.truffle.api.frame.FrameSlotKind;
6866
import com.oracle.truffle.api.frame.FrameUtil;
6967
import com.oracle.truffle.api.frame.MaterializedFrame;
7068
import com.oracle.truffle.api.frame.VirtualFrame;
7169
import com.oracle.truffle.api.nodes.IndirectCallNode;
72-
import com.oracle.truffle.api.nodes.Node;
73-
import com.oracle.truffle.api.nodes.RootNode;
7470
import com.oracle.truffle.api.profiles.ConditionProfile;
75-
import com.oracle.truffle.api.Truffle;
7671

7772
@CoreModule("Truffle::KernelOperations")
7873
public abstract class TruffleKernelNodes {
@@ -364,36 +359,6 @@ protected Object storage(VirtualFrame frame,
364359
}
365360
}
366361

367-
/* When getting special variables from the wrong side of a C call we know it's going to be slow. */
368-
@Primitive(name = "ruby_caller_special_variables")
369-
public abstract static class GetSlowCallerSpecialVariableStorage extends PrimitiveArrayArgumentsNode {
370-
371-
@Child GetSpecialVariableStorage getStorageNode = GetSpecialVariableStorage.create();
372-
373-
@Specialization
374-
@TruffleBoundary
375-
protected Object storage() {
376-
return getStorageNode.execute(Truffle.getRuntime().iterateFrames(frameInstance -> {
377-
final Node callNode = frameInstance.getCallNode();
378-
379-
if (callNode != null) {
380-
final RootNode rootNode = callNode.getRootNode();
381-
// Skip Ruby frames in cext.rb file since they are implementing methods which are implemented
382-
// with C in MRI, and therefore are also implicitly skipped when when looking up the block passed
383-
// to a C API function.
384-
if (rootNode instanceof RubyRootNode &&
385-
rootNode.getSourceSection().isAvailable() &&
386-
!rootNode.getSourceSection().getSource().getName().endsWith("cext.rb") &&
387-
!rootNode.getSourceSection().getSource().getName().endsWith("cext_ruby.rb")) {
388-
return frameInstance.getFrame(FrameAccess.MATERIALIZE).materialize();
389-
}
390-
}
391-
392-
return null;
393-
}));
394-
}
395-
}
396-
397362
@Primitive(name = "proc_special_variables")
398363
public abstract static class GetProcSpecialVariableStorage extends PrimitiveArrayArgumentsNode {
399364

0 commit comments

Comments
 (0)