Skip to content

Commit 21efe56

Browse files
committed
[GR-61665] Backport for 24.2: Enable the Panama NFI backend by default
PullRequest: truffleruby/4461
2 parents a13be8c + 5092419 commit 21efe56

File tree

6 files changed

+25
-4
lines changed

6 files changed

+25
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Compatibility:
6363

6464
Performance:
6565

66+
* Speedup some C extensions like `sqlite3`, `trilogy` and `json` by 2 to 3 times by using the Panama NFI backend for faster upcalls in JVM mode (@eregon).
6667
* Optimize encoding negotiation for ASCII-compatible encodings (@eregon, @andrykonchin).
6768

6869
Changes:

lib/truffle/truffle/cext.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,20 @@ def self.init_libtrufflerubytrampoline(libtrampoline)
190190

191191
init_functions = libtrampoline[:rb_tr_trampoline_init_functions]
192192
init_functions = Primitive.interop_eval_nfi('(env,(string):pointer):void').bind(init_functions)
193-
if Truffle::Boot.get_option 'cexts-panama' and Primitive.vm_java_version >= 22 and !TruffleRuby.native?
193+
194+
panama = Truffle::Boot.get_option('cexts-panama') && Primitive.vm_java_version >= 22 && !TruffleRuby.native?
195+
if panama
196+
# Check if the Panama backend is available, it might not be when embedding TruffleRuby
197+
rb_tr_invoke = LIBTRUFFLERUBY['rb_tr_invoke']
198+
begin
199+
keep_alive << rb_tr_invoke.createNativeClosure('panama')
200+
rescue Polyglot::ForeignException => e
201+
warn "warning: the Panama Truffle NFI backend for running C extensions faster is not available (#{e.message}). Add 'org.graalvm.truffle:truffle-nfi-panama' to Maven dependencies to resolve or use '--ruby.cexts-panama=false' to ignore."
202+
panama = false
203+
end
204+
end
205+
206+
if panama
194207
init_functions.call(-> name {
195208
closure = LIBTRUFFLERUBY[name].createNativeClosure('panama')
196209
keep_alive << closure

src/main/java/org/truffleruby/options/Options.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public final class Options {
107107
public final boolean CEXTS;
108108
/** --cexts-lock=true */
109109
public final boolean CEXT_LOCK;
110-
/** --cexts-panama=false */
110+
/** --cexts-panama=true */
111111
public final boolean CEXTS_PANAMA;
112112
/** --options-log=false */
113113
public final boolean OPTIONS_LOG;

src/options.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ EXPERT:
132132
# C extension options
133133
CEXTS: [cexts, boolean, true, Enable use of C extensions]
134134
CEXT_LOCK: [cexts-lock, boolean, true, Use a Global Lock when running C extensions]
135-
CEXTS_PANAMA: [cexts-panama, boolean, false, 'Use Panama for native to Ruby calls in C extensions. Only available in --jvm mode on JDK 22+.']
135+
CEXTS_PANAMA: [cexts-panama, boolean, true, 'Use Panama for native to Ruby calls in C extensions. Only available in --jvm mode on JDK 22+.']
136136

137137
# Debugging the values of options
138138
OPTIONS_LOG: [options-log, boolean, false, Log the final value of all options]

src/shared/java/org/truffleruby/shared/options/OptionsCatalog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public final class OptionsCatalog {
7171
public static final OptionKey<Boolean> BACKTRACE_ON_NEW_FIBER_KEY = new OptionKey<>(false);
7272
public static final OptionKey<Boolean> CEXTS_KEY = new OptionKey<>(true);
7373
public static final OptionKey<Boolean> CEXT_LOCK_KEY = new OptionKey<>(true);
74-
public static final OptionKey<Boolean> CEXTS_PANAMA_KEY = new OptionKey<>(false);
74+
public static final OptionKey<Boolean> CEXTS_PANAMA_KEY = new OptionKey<>(true);
7575
public static final OptionKey<Boolean> OPTIONS_LOG_KEY = new OptionKey<>(false);
7676
public static final OptionKey<Boolean> LOG_LOAD_KEY = new OptionKey<>(false);
7777
public static final OptionKey<Boolean> LOG_AUTOLOAD_KEY = new OptionKey<>(false);

src/test-embedding/java/org/truffleruby/test/embedding/ContextPermissionsTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ public void testRequireGem() {
7777
}
7878
}
7979

80+
@Test
81+
public void testRequireCExtension() {
82+
try (Context context = Context.newBuilder("ruby").allowIO(IOAccess.ALL).allowNativeAccess(true).build()) {
83+
Assert.assertEquals("Etc", context.eval("ruby", "require 'etc'; Etc.to_s").asString());
84+
}
85+
}
86+
8087
@Test
8188
public void testThreadsNoNative() throws Throwable {
8289
// The ruby.single_threaded option needs to be set because --single-threaded defaults to --embedded.

0 commit comments

Comments
 (0)