Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GR-52676] Enable the Panama NFI backend by default #3714

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Compatibility:

Performance:

* 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).
* Optimize encoding negotiation for ASCII-compatible encodings (@eregon, @andrykonchin).

Changes:
Expand Down
16 changes: 8 additions & 8 deletions common.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

"COMMENT.jdks": "When adding or removing JDKs keep in sync with JDKs in ci/common.jsonnet",
"jdks": {
"galahad-jdk": {"name": "jpg-jdk", "version": "24", "build_id": "jdk-24+22-2651", "platformspecific": true, "extrabundles": ["static-libs"]},
"galahad-jdk": {"name": "jpg-jdk", "version": "24", "build_id": "jdk-24+23-2783", "platformspecific": true, "extrabundles": ["static-libs"]},

"oraclejdk17": {"name": "jpg-jdk", "version": "17.0.7", "build_id": "jdk-17.0.7+8", "platformspecific": true, "extrabundles": ["static-libs"]},
"labsjdk-ce-17": {"name": "labsjdk", "version": "ce-17.0.7+4-jvmci-23.1-b02", "platformspecific": true },
Expand Down Expand Up @@ -45,13 +45,13 @@

"oraclejdk23": {"name": "jpg-jdk", "version": "23", "build_id": "jdk-23+37", "platformspecific": true, "extrabundles": ["static-libs"]},

"oraclejdk-latest": {"name": "jpg-jdk", "version": "24", "build_id": "jdk-24+22", "platformspecific": true, "extrabundles": ["static-libs"]},
"labsjdk-ce-latest": {"name": "labsjdk", "version": "ce-24+22-jvmci-b01", "platformspecific": true },
"labsjdk-ce-latestDebug": {"name": "labsjdk", "version": "ce-24+22-jvmci-b01-debug", "platformspecific": true },
"labsjdk-ce-latest-llvm": {"name": "labsjdk", "version": "ce-24+22-jvmci-b01-sulong", "platformspecific": true },
"labsjdk-ee-latest": {"name": "labsjdk", "version": "ee-24+22-jvmci-b01", "platformspecific": true },
"labsjdk-ee-latestDebug": {"name": "labsjdk", "version": "ee-24+22-jvmci-b01-debug", "platformspecific": true },
"labsjdk-ee-latest-llvm": {"name": "labsjdk", "version": "ee-24+22-jvmci-b01-sulong", "platformspecific": true }
"oraclejdk-latest": {"name": "jpg-jdk", "version": "24", "build_id": "jdk-24+23", "platformspecific": true, "extrabundles": ["static-libs"]},
"labsjdk-ce-latest": {"name": "labsjdk", "version": "ce-24+23-jvmci-b01", "platformspecific": true },
"labsjdk-ce-latestDebug": {"name": "labsjdk", "version": "ce-24+23-jvmci-b01-debug", "platformspecific": true },
"labsjdk-ce-latest-llvm": {"name": "labsjdk", "version": "ce-24+23-jvmci-b01-sulong", "platformspecific": true },
"labsjdk-ee-latest": {"name": "labsjdk", "version": "ee-24+23-jvmci-b01", "platformspecific": true },
"labsjdk-ee-latestDebug": {"name": "labsjdk", "version": "ee-24+23-jvmci-b01-debug", "platformspecific": true },
"labsjdk-ee-latest-llvm": {"name": "labsjdk", "version": "ee-24+23-jvmci-b01-sulong", "platformspecific": true }
},

"eclipse": {
Expand Down
15 changes: 14 additions & 1 deletion lib/truffle/truffle/cext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,20 @@ def self.init_libtrufflerubytrampoline(libtrampoline)

init_functions = libtrampoline[:rb_tr_trampoline_init_functions]
init_functions = Primitive.interop_eval_nfi('(env,(string):pointer):void').bind(init_functions)
if Truffle::Boot.get_option 'cexts-panama' and Primitive.vm_java_version >= 22 and !TruffleRuby.native?

panama = Truffle::Boot.get_option('cexts-panama') && Primitive.vm_java_version >= 22 && !TruffleRuby.native?
if panama
# Check if the Panama backend is available, it might not be when embedding TruffleRuby
rb_tr_invoke = LIBTRUFFLERUBY['rb_tr_invoke']
begin
keep_alive << rb_tr_invoke.createNativeClosure('panama')
rescue Polyglot::ForeignException => e
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."
panama = false
end
end

if panama
init_functions.call(-> name {
closure = LIBTRUFFLERUBY[name].createNativeClosure('panama')
keep_alive << closure
Expand Down
4 changes: 2 additions & 2 deletions mx.truffleruby/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
{
"name": "regex",
"subdir": True,
"version": "e38604e7078ca4f8c66fdd3f9c3fbf7ec835d894",
"version": "367e521345b28b954a071924b3b129ad6546e7ed",
"urls": [
{"url": "https://github.com/oracle/graal.git", "kind": "git"},
{"url": "https://curio.ssw.jku.at/nexus/content/repositories/snapshots", "kind": "binary"},
Expand All @@ -31,7 +31,7 @@
{
"name": "sulong",
"subdir": True,
"version": "e38604e7078ca4f8c66fdd3f9c3fbf7ec835d894",
"version": "367e521345b28b954a071924b3b129ad6546e7ed",
"urls": [
{"url": "https://github.com/oracle/graal.git", "kind": "git"},
{"url": "https://curio.ssw.jku.at/nexus/content/repositories/snapshots", "kind": "binary"},
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/truffleruby/options/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public final class Options {
public final boolean CEXTS;
/** --cexts-lock=true */
public final boolean CEXT_LOCK;
/** --cexts-panama=false */
/** --cexts-panama=true */
public final boolean CEXTS_PANAMA;
/** --options-log=false */
public final boolean OPTIONS_LOG;
Expand Down
2 changes: 1 addition & 1 deletion src/options.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ EXPERT:
# C extension options
CEXTS: [cexts, boolean, true, Enable use of C extensions]
CEXT_LOCK: [cexts-lock, boolean, true, Use a Global Lock when running C extensions]
CEXTS_PANAMA: [cexts-panama, boolean, false, 'Use Panama for native to Ruby calls in C extensions. Only available in --jvm mode on JDK 22+.']
CEXTS_PANAMA: [cexts-panama, boolean, true, 'Use Panama for native to Ruby calls in C extensions. Only available in --jvm mode on JDK 22+.']

# Debugging the values of options
OPTIONS_LOG: [options-log, boolean, false, Log the final value of all options]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public final class OptionsCatalog {
public static final OptionKey<Boolean> BACKTRACE_ON_NEW_FIBER_KEY = new OptionKey<>(false);
public static final OptionKey<Boolean> CEXTS_KEY = new OptionKey<>(true);
public static final OptionKey<Boolean> CEXT_LOCK_KEY = new OptionKey<>(true);
public static final OptionKey<Boolean> CEXTS_PANAMA_KEY = new OptionKey<>(false);
public static final OptionKey<Boolean> CEXTS_PANAMA_KEY = new OptionKey<>(true);
public static final OptionKey<Boolean> OPTIONS_LOG_KEY = new OptionKey<>(false);
public static final OptionKey<Boolean> LOG_LOAD_KEY = new OptionKey<>(false);
public static final OptionKey<Boolean> LOG_AUTOLOAD_KEY = new OptionKey<>(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ public void testRequireGem() {
}
}

@Test
public void testRequireCExtension() {
try (Context context = Context.newBuilder("ruby").allowIO(IOAccess.ALL).allowNativeAccess(true).build()) {
Assert.assertEquals("Etc", context.eval("ruby", "require 'etc'; Etc.to_s").asString());
}
}

@Test
public void testThreadsNoNative() throws Throwable {
// The ruby.single_threaded option needs to be set because --single-threaded defaults to --embedded.
Expand Down
Loading