Skip to content

Commit eefc08e

Browse files
committed
Remove ParserCache
* Unfortunately it cannot work anymore because we need to find libyarpbindings.so to use the Prism parser but there is no way to locate it for the case of embedding truffleruby as a native image, while running in a static initializer. For example when using https://github.com/graalvm/polyglot-embedding-demo and building via Maven. In that case the resources/ direcotry is only created after the image is built, and also Class#getResourceAsStream() does not seem to work + would be messy. * Going forward we likely want to cache the serialized bytes of core and maybe stdlib sources, on the filesystem or in a jar, by running some java process to do so while building TruffleRuby. * This cache was only useful for native and when pre-initialization is not used, which is a narrow use case.
1 parent 677ac08 commit eefc08e

File tree

5 files changed

+19
-120
lines changed

5 files changed

+19
-120
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"resources": {
3+
"includes": [
4+
{ "pattern": "^truffleruby/core/.+\\.rb$" },
5+
{ "pattern": "^truffleruby/post-boot/.+\\.rb$" }
6+
]
7+
}
8+
}

src/main/java/org/truffleruby/aot/ParserCache.java

Lines changed: 0 additions & 74 deletions
This file was deleted.

src/main/java/org/truffleruby/core/CoreLibrary.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.truffleruby.RubyLanguage;
3030
import org.truffleruby.annotations.CoreMethod;
3131
import org.truffleruby.annotations.SuppressFBWarnings;
32-
import org.truffleruby.aot.ParserCache;
3332
import org.truffleruby.builtins.BuiltinsClasses;
3433
import org.truffleruby.builtins.CoreMethodNodeManager;
3534
import org.truffleruby.core.array.RubyArray;
@@ -73,7 +72,6 @@
7372
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
7473
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
7574
import com.oracle.truffle.api.Truffle;
76-
import com.oracle.truffle.api.TruffleOptions;
7775
import com.oracle.truffle.api.object.DynamicObjectLibrary;
7876
import com.oracle.truffle.api.source.Source;
7977
import com.oracle.truffle.api.source.SourceSection;
@@ -785,12 +783,7 @@ public void loadRubyCoreLibraryAndPostBoot() {
785783

786784
public RubySource loadCoreFileSource(String path) throws IOException {
787785
if (path.startsWith(RubyLanguage.RESOURCE_SCHEME)) {
788-
if (TruffleOptions.AOT || ParserCache.INSTANCE != null) {
789-
Source source = ParserCache.INSTANCE.get(path).getRight();
790-
return new RubySource(source, path);
791-
} else {
792-
return new RubySource(ResourceLoader.loadResource(path, language.options.CORE_AS_INTERNAL), path);
793-
}
786+
return new RubySource(ResourceLoader.loadResource(path, language.options.CORE_AS_INTERNAL), path);
794787
} else {
795788
final FileLoader fileLoader = new FileLoader(context, language);
796789
return fileLoader.loadFile(path);

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,10 @@
1414
import java.io.InputStream;
1515
import java.io.InputStreamReader;
1616
import java.nio.charset.StandardCharsets;
17-
import java.nio.file.Path;
18-
import java.nio.file.Paths;
1917
import java.util.Locale;
2018

2119
import org.truffleruby.RubyContext;
2220
import org.truffleruby.RubyLanguage;
23-
import org.truffleruby.core.string.StringUtils;
2421
import org.truffleruby.shared.TruffleRuby;
2522

2623
import com.oracle.truffle.api.source.Source;
@@ -38,12 +35,11 @@ public static Source loadResource(String path, boolean internal) throws IOExcept
3835
}
3936

4037
final Class<?> relativeClass = RubyContext.class;
41-
final Path relativePath = Paths.get(path.substring(RubyLanguage.RESOURCE_SCHEME.length()));
42-
final String normalizedPath = StringUtils.replace(relativePath.normalize().toString(), '\\', '/');
43-
final InputStream stream = relativeClass.getResourceAsStream(normalizedPath);
38+
final String resourcePath = path.substring(RubyLanguage.RESOURCE_SCHEME.length());
39+
final InputStream stream = relativeClass.getResourceAsStream(resourcePath);
4440

4541
if (stream == null) {
46-
throw new FileNotFoundException(path);
42+
throw new FileNotFoundException(resourcePath);
4743
}
4844

4945
final Source source;

src/main/java/org/truffleruby/parser/YARPTranslatorDriver.java

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import org.truffleruby.RubyContext;
4949
import org.truffleruby.RubyLanguage;
5050
import org.truffleruby.annotations.Split;
51-
import org.truffleruby.aot.ParserCache;
5251
import org.truffleruby.core.CoreLibrary;
5352
import org.truffleruby.core.DummyNode;
5453
import org.truffleruby.core.binding.BindingNodes;
@@ -158,22 +157,13 @@ public RootCallTarget parse(RubySource rubySource, ParserContext parserContext,
158157

159158
final String sourcePath = rubySource.getSourcePath(language).intern();
160159

161-
// Only use the cache while loading top-level core library files, as eval() later could use
162-
// the same Source name but should not use the cache. For instance,
163-
// TOPLEVEL_BINDING.eval("self") would use the cache which is wrong.
164-
final ParseResult parseResult;
165-
if (ParserCache.INSTANCE != null && parserContext == ParserContext.TOP_LEVEL &&
166-
ParserCache.INSTANCE.containsKey(source.getName())) {
167-
parseResult = ParserCache.INSTANCE.get(source.getName()).getLeft();
168-
} else {
169-
printParseTranslateExecuteMetric("before-parsing", context, source);
170-
parseResult = context.getMetricsProfiler().callWithMetrics(
171-
"parsing",
172-
source.getName(),
173-
() -> parseToYARPAST(rubySource, sourcePath, yarpSource, localsInScopes,
174-
language.options.FROZEN_STRING_LITERALS));
175-
printParseTranslateExecuteMetric("after-parsing", context, source);
176-
}
160+
printParseTranslateExecuteMetric("before-parsing", context, source);
161+
final ParseResult parseResult = context.getMetricsProfiler().callWithMetrics(
162+
"parsing",
163+
source.getName(),
164+
() -> parseToYARPAST(rubySource, sourcePath, yarpSource, localsInScopes,
165+
language.options.FROZEN_STRING_LITERALS));
166+
printParseTranslateExecuteMetric("after-parsing", context, source);
177167

178168
handleWarningsErrorsPrimitives(context, parseResult, rubySource, sourcePath, parseEnvironment, rubyWarnings);
179169

@@ -458,20 +448,6 @@ public static void handleWarningsErrorsPrimitives(RubyContext context, ParseResu
458448
parseEnvironment.allowTruffleRubyPrimitives = allowTruffleRubyPrimitives;
459449
}
460450

461-
public static void handleWarningsErrorsNoContext(ParseResult parseResult, String sourcePath,
462-
Nodes.Source yarpSource) {
463-
if (parseResult.errors.length > 0) {
464-
var error = parseResult.errors[0];
465-
throw CompilerDirectives.shouldNotReachHere("Parse error in " +
466-
sourcePath + ":" + yarpSource.line(error.location.startOffset) + ": " + error.message);
467-
}
468-
469-
for (var warning : parseResult.warnings) {
470-
throw CompilerDirectives.shouldNotReachHere("Warning in " +
471-
sourcePath + ":" + yarpSource.line(warning.location.startOffset) + ": " + warning.message);
472-
}
473-
}
474-
475451
public static Nodes.Source createYARPSource(byte[] sourceBytes) {
476452
return new Nodes.Source(sourceBytes);
477453
}

0 commit comments

Comments
 (0)