Skip to content

Commit 9e79159

Browse files
committed
reduce the classloader lookups for require
most creation of the URLResource are via require 'some/thing' where it is clear before hand that it is a file and not a directory URLResource always returns a FileResource, it can be either a file or a directory or it is marked as not existing. some classloaders return a stream to directory resource and some classloaders do not. to distinguish a file from directory you need to check for the .jrubydir file or dig deeper into the classloader to find out whether this is directory or not.
1 parent f29d80c commit 9e79159

File tree

5 files changed

+34
-28
lines changed

5 files changed

+34
-28
lines changed

core/src/main/java/org/jruby/RubyInstanceConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ public InputStream getScriptSource() {
394394
} else if (script.startsWith("classpath:")) {
395395
stream = getScriptSourceFromJar(script);
396396
} else if (script.startsWith("uri:classloader:")) {
397-
FileResource urlResource = URLResource.create(loader, script);
397+
FileResource urlResource = URLResource.create(loader, script, true);
398398
stream = urlResource.inputStream();
399399
} else {
400400
File file = JRubyFile.create(getCurrentDirectory(), getScriptFileName());

core/src/main/java/org/jruby/runtime/load/LibrarySearcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private FoundLibrary findFileResourceWithLoadPath(String searchName, String suff
174174
String pathWithSuffix = fullPath + suffix;
175175

176176
DebugLog.Resource.logTry(pathWithSuffix);
177-
FileResource resource = JRubyFile.createResource(runtime, pathWithSuffix);
177+
FileResource resource = JRubyFile.createResourceAsFile(runtime, pathWithSuffix);
178178
if (resource.exists()) {
179179
DebugLog.Resource.logFound(pathWithSuffix);
180180
String scriptName = resolveScriptName(resource, pathWithSuffix);

core/src/main/java/org/jruby/util/JRubyFile.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,19 @@ public static FileResource createResource(ThreadContext context, String pathname
6363
return createResource(context.runtime, pathname);
6464
}
6565

66+
public static FileResource createResourceAsFile(Ruby runtime, String pathname) {
67+
return createResource(runtime, runtime.getCurrentDirectory(), pathname, true);
68+
}
69+
6670
public static FileResource createResource(Ruby runtime, String pathname) {
67-
return createResource(runtime, runtime.getCurrentDirectory(), pathname);
71+
return createResource(runtime, runtime.getCurrentDirectory(), pathname, false);
6872
}
6973

7074
public static FileResource createResource(Ruby runtime, String cwd, String pathname) {
75+
return createResource(runtime, cwd, pathname, false);
76+
}
77+
78+
private static FileResource createResource(Ruby runtime, String cwd, String pathname, boolean isFile) {
7179
FileResource emptyResource = EmptyFileResource.create(pathname);
7280
if (emptyResource != null) return emptyResource;
7381

@@ -87,7 +95,7 @@ public static FileResource createResource(Ruby runtime, String cwd, String pathn
8795
}
8896

8997
// replace is needed for maven/jruby-complete/src/it/app_using_classpath_uri to work
90-
if (pathname.startsWith("uri:")) return URLResource.create(runtime, pathname);
98+
if (pathname.startsWith("uri:")) return URLResource.create(runtime, pathname, isFile);
9199

92100
if (pathname.startsWith("file:")) {
93101
pathname = pathname.substring(5);

core/src/main/java/org/jruby/util/URLResource.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -149,38 +149,38 @@ public Channel openChannel( ModeFlags flags, int perm ) throws ResourceException
149149
return Channels.newChannel(inputStream());
150150
}
151151

152-
public static FileResource create(ClassLoader cl, String pathname) {
152+
public static FileResource create(ClassLoader cl, String pathname, boolean isFile) {
153153
try
154154
{
155155
pathname = new URI(pathname.replaceFirst("^/*", "/")).normalize().getPath().replaceAll("^/([.][.]/)*", "");
156156
} catch (URISyntaxException e) {
157157
pathname = pathname.replaceAll("^[.]?/*", "");
158158
}
159159
URL url = cl.getResource(pathname);
160-
String[] files = listClassLoaderFiles(cl, pathname);
160+
String[] files = isFile ? null : listClassLoaderFiles(cl, pathname);
161161
return new URLResource(URI_CLASSLOADER + pathname,
162162
cl,
163163
url == null ? null : pathname,
164164
files);
165165
}
166166

167-
public static FileResource createClassloaderURI(Ruby runtime, String pathname) {
168-
return create(runtime.getJRubyClassLoader(), pathname);
167+
public static FileResource createClassloaderURI(Ruby runtime, String pathname, boolean isFile) {
168+
return create(runtime.getJRubyClassLoader(), pathname, isFile);
169169
}
170170

171-
public static FileResource create(Ruby runtime, String pathname)
171+
public static FileResource create(Ruby runtime, String pathname, boolean isFile)
172172
{
173173
if (!pathname.startsWith(URI)) {
174174
return null;
175175
}
176176
pathname = pathname.substring(URI.length());
177177
if (pathname.startsWith(CLASSLOADER)) {
178-
return createClassloaderURI(runtime, pathname.substring(CLASSLOADER.length()));
178+
return createClassloaderURI(runtime, pathname.substring(CLASSLOADER.length()), isFile);
179179
}
180-
return createRegularURI(pathname);
180+
return createRegularURI(pathname, isFile);
181181
}
182182

183-
private static FileResource createRegularURI(String pathname) {
183+
private static FileResource createRegularURI(String pathname, boolean isFile) {
184184
URL url;
185185
try
186186
{
@@ -200,7 +200,7 @@ private static FileResource createRegularURI(String pathname) {
200200
// file does not exists
201201
return new URLResource(URI + pathname, (URL)null, null);
202202
}
203-
String[] files = listFiles(pathname);
203+
String[] files = isFile ? null : listFiles(pathname);
204204
if (files != null) {
205205
return new URLResource(URI + pathname, (URL)null, files);
206206
}
@@ -252,12 +252,9 @@ private static String[] listFilesFromInputStream(InputStream is) {
252252
}
253253

254254
private static String[] listClassLoaderFiles(ClassLoader classloader, String pathname) {
255-
if (pathname.endsWith(".rb") || pathname.endsWith(".class") || pathname.endsWith(".jar")) {
256-
return null;
257-
}
258255
try
259256
{
260-
pathname = pathname + (pathname.equals("") ? ".jrubydir" : "/.jrubydir");
257+
pathname += pathname.equals("") ? ".jrubydir" : "/.jrubydir";
261258
Enumeration<URL> urls = classloader.getResources(pathname);
262259
if (!urls.hasMoreElements()) {
263260
return null;
@@ -280,9 +277,6 @@ private static String[] listClassLoaderFiles(ClassLoader classloader, String pat
280277
}
281278

282279
private static String[] listFiles(String pathname) {
283-
if (pathname.endsWith(".rb") || pathname.endsWith(".class") || pathname.endsWith(".jar")) {
284-
return null;
285-
}
286280
try
287281
{
288282
InputStream is = new URL(pathname + "/.jrubydir").openStream();

core/src/test/java/org/jruby/util/URLResourceTest.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class URLResourceTest extends TestCase {
1010

1111
public void testDirectory(){
1212
String uri = Thread.currentThread().getContextClassLoader().getResource( "somedir" ).toExternalForm();
13-
FileResource resource = URLResource.create(Ruby.getGlobalRuntime(), "uri:" + uri);
13+
FileResource resource = URLResource.create(Ruby.getGlobalRuntime(), "uri:" + uri, false);
1414

1515
assertNotNull(resource );
1616
assertFalse(resource.isFile());
@@ -22,7 +22,7 @@ public void testDirectory(){
2222

2323
public void testNoneDirectory(){
2424
String uri = Thread.currentThread().getContextClassLoader().getResource( "somedir/dir_without_listing" ).toExternalForm();
25-
FileResource resource = URLResource.create(Ruby.getGlobalRuntime(), "uri:" + uri);
25+
FileResource resource = URLResource.create(Ruby.getGlobalRuntime(), "uri:" + uri, false);
2626

2727
assertNotNull(resource );
2828
// you can open streams on file-system directories
@@ -34,7 +34,7 @@ public void testNoneDirectory(){
3434

3535
public void testFile(){
3636
String uri = Thread.currentThread().getContextClassLoader().getResource( "somedir/.jrubydir" ).toExternalForm();
37-
FileResource resource = URLResource.create(Ruby.getGlobalRuntime(), "uri:" + uri);
37+
FileResource resource = URLResource.create(Ruby.getGlobalRuntime(), "uri:" + uri, false);
3838

3939
assertNotNull(resource );
4040
// you can open streams on file-system directories
@@ -46,7 +46,7 @@ public void testFile(){
4646

4747
public void testNonExistingFile(){
4848
String uri = Thread.currentThread().getContextClassLoader().getResource( "somedir" ).toExternalForm();
49-
FileResource resource = URLResource.create(Ruby.getGlobalRuntime(), "uri:" + uri + "/not_there");
49+
FileResource resource = URLResource.create(Ruby.getGlobalRuntime(), "uri:" + uri + "/not_there", false);
5050

5151
assertNotNull(resource );
5252
assertFalse(resource.isFile());
@@ -57,7 +57,8 @@ public void testNonExistingFile(){
5757

5858
public void testDirectoryClassloader()
5959
{
60-
FileResource resource = URLResource.create(Ruby.getGlobalRuntime(), "uri:classloader:/somedir");
60+
FileResource resource = URLResource.create(Ruby.getGlobalRuntime(),
61+
"uri:classloader:/somedir", false);
6162

6263
assertNotNull( resource );
6364
assertFalse( resource.isFile() );
@@ -70,7 +71,8 @@ public void testDirectoryClassloader()
7071

7172
public void testNoneDirectoryClassloader()
7273
{
73-
FileResource resource = URLResource.create(Ruby.getGlobalRuntime(), "uri:classloader:/somedir/dir_without_listing");
74+
FileResource resource = URLResource.create(Ruby.getGlobalRuntime(),
75+
"uri:classloader:/somedir/dir_without_listing", false);
7476

7577
assertNotNull( resource );
7678
// you can open streams on file-system directories
@@ -82,7 +84,8 @@ public void testNoneDirectoryClassloader()
8284

8385
public void testFileClassloader()
8486
{
85-
FileResource resource = URLResource.create(Ruby.getGlobalRuntime(), "uri:classloader:/somedir/.jrubydir" );
87+
FileResource resource = URLResource.create(Ruby.getGlobalRuntime(),
88+
"uri:classloader:/somedir/.jrubydir", false );
8689

8790
assertNotNull( resource );
8891
// you can open streams on file-system directories
@@ -94,7 +97,8 @@ public void testFileClassloader()
9497

9598
public void testNonExistingFileClassloader()
9699
{
97-
FileResource resource = URLResource.create(Ruby.getGlobalRuntime(), "uri:classloader:/somedir/not_there" );
100+
FileResource resource = URLResource.create(Ruby.getGlobalRuntime(),
101+
"uri:classloader:/somedir/not_there", false );
98102

99103
assertNotNull( resource );
100104
assertFalse( resource.isFile() );

0 commit comments

Comments
 (0)