Skip to content

Commit a95fbb8

Browse files
committed
Optimize feature loading when require is called with absolute .rb path
If require is called with an absolute path like `/path/to/thing.rb` we can try that as is first rather than trying `.rb.rb` and `.rb.so` first.
1 parent f1c7d51 commit a95fbb8

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Compatibility:
1616
Performance:
1717

1818
* Optimize calls with `ruby2_keywords` forwarding by deciding it per call site instead of per callee thanks to [my fix in CRuby 3.2](https://bugs.ruby-lang.org/issues/18625) (@eregon).
19+
* Optimize feature loading when require is called with an absolute path to a .rb file (@rwstauner).
1920

2021
Changes:
2122

src/main/java/org/truffleruby/language/loader/FeatureLoader.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,16 @@ private String translateIfNativePath(String feature) {
379379
private String findFeatureWithAndWithoutExtension(String path) {
380380
assert new File(path).isAbsolute();
381381

382+
boolean triedWithoutExtension = false;
383+
384+
if (path.endsWith(TruffleRuby.EXTENSION)) {
385+
final String withoutExtension = findFeatureWithExactPath(path);
386+
if (withoutExtension != null) {
387+
return withoutExtension;
388+
}
389+
triedWithoutExtension = true;
390+
}
391+
382392
if (path.endsWith(".so")) {
383393
final String pathWithNativeExt = translateIfNativePath(path);
384394
final String asCExt = findFeatureWithExactPath(pathWithNativeExt);
@@ -397,9 +407,11 @@ private String findFeatureWithAndWithoutExtension(String path) {
397407
return asCExt;
398408
}
399409

400-
final String withoutExtension = findFeatureWithExactPath(path);
401-
if (withoutExtension != null) {
402-
return withoutExtension;
410+
if (!triedWithoutExtension) {
411+
final String withoutExtension = findFeatureWithExactPath(path);
412+
if (withoutExtension != null) {
413+
return withoutExtension;
414+
}
403415
}
404416

405417
return null;

0 commit comments

Comments
 (0)