Skip to content

Commit

Permalink
Don't double-search for requires.
Browse files Browse the repository at this point in the history
requireCommon and smartLoadInternal had almost the same logic to
check the load against others in progress and do the eventual
search for the file, and the former caller the latter. I added
the one item from rC that was missing from sML (the profile check
for requireability) and removed rC completely.
headius committed Aug 19, 2016
1 parent 5f939b2 commit 4ff65a7
Showing 1 changed file with 6 additions and 28 deletions.
34 changes: 6 additions & 28 deletions core/src/main/java/org/jruby/runtime/load/LoadService.java
Original file line number Diff line number Diff line change
@@ -393,43 +393,17 @@ public SearchState findFileForLoad(String file) {
}

public boolean require(String requireName) {
return requireCommon(requireName, true) == RequireState.LOADED;
return smartLoadInternal(requireName, true) == RequireState.LOADED;
}

public boolean autoloadRequire(String requireName) {
return requireCommon(requireName, false) != RequireState.CIRCULAR;
return smartLoadInternal(requireName, false) != RequireState.CIRCULAR;
}

private enum RequireState {
LOADED, ALREADY_LOADED, CIRCULAR
}

private RequireState requireCommon(String file, boolean circularRequireWarning) {
checkEmptyLoad(file);

// check with short name
if (featureAlreadyLoaded(file)) {
return RequireState.ALREADY_LOADED;
}

SearchState state = findFileForLoad(file);

if (state.library == null) {
throw runtime.newLoadError("no such file to load -- " + state.searchFile, state.searchFile);
}

// check with long name
if (featureAlreadyLoaded(state.loadName)) {
return RequireState.ALREADY_LOADED;
}

if (!runtime.getProfile().allowRequire(file)) {
throw runtime.newLoadError("no such file to load -- " + file, file);
}

return smartLoadInternal(file, circularRequireWarning);
}

private final RequireLocks requireLocks = new RequireLocks();

private static final class RequireLocks {
@@ -519,6 +493,10 @@ private RequireState smartLoadInternal(String file, boolean circularRequireWarni
return RequireState.ALREADY_LOADED;
}

if (!runtime.getProfile().allowRequire(file)) {
throw runtime.newLoadError("no such file to load -- " + file, file);
}

if (!requireLocks.lock(state.loadName)) {
if (circularRequireWarning && runtime.isVerbose()) {
warnCircularRequire(state.loadName);

0 comments on commit 4ff65a7

Please sign in to comment.