Skip to content

Commit

Permalink
Optimize feature loading when require is called with absolute .rb path
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rwstauner committed Sep 24, 2023
1 parent f1c7d51 commit a95fbb8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Compatibility:
Performance:

* 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).
* Optimize feature loading when require is called with an absolute path to a .rb file (@rwstauner).

Changes:

Expand Down
18 changes: 15 additions & 3 deletions src/main/java/org/truffleruby/language/loader/FeatureLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,16 @@ private String translateIfNativePath(String feature) {
private String findFeatureWithAndWithoutExtension(String path) {
assert new File(path).isAbsolute();

boolean triedWithoutExtension = false;

if (path.endsWith(TruffleRuby.EXTENSION)) {
final String withoutExtension = findFeatureWithExactPath(path);
if (withoutExtension != null) {
return withoutExtension;
}
triedWithoutExtension = true;
}

if (path.endsWith(".so")) {
final String pathWithNativeExt = translateIfNativePath(path);
final String asCExt = findFeatureWithExactPath(pathWithNativeExt);
Expand All @@ -397,9 +407,11 @@ private String findFeatureWithAndWithoutExtension(String path) {
return asCExt;
}

final String withoutExtension = findFeatureWithExactPath(path);
if (withoutExtension != null) {
return withoutExtension;
if (!triedWithoutExtension) {
final String withoutExtension = findFeatureWithExactPath(path);
if (withoutExtension != null) {
return withoutExtension;
}
}

return null;
Expand Down

0 comments on commit a95fbb8

Please sign in to comment.