Skip to content

Commit c4e636e

Browse files
committed
[GR-45043] Remove ParserCache
PullRequest: truffleruby/4160
2 parents 8c3e816 + eaf4158 commit c4e636e

File tree

6 files changed

+38
-133
lines changed

6 files changed

+38
-133
lines changed

ci.jsonnet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ local composition_environment = utils.add_inclusion_tracking(part_definitions, "
528528
"ruby-test-fast-linux-amd64": $.platform.linux + $.jdk.stable + $.env.jvm + gate + $.run.test_fast + { timelimit: "45:00" }, # To catch missing slow tags
529529
"ruby-test-mri-asserts": $.platform.linux + $.jdk.stable + $.env.jvm + gate + $.run.test_mri_fast + { timelimit: "01:15:00" },
530530
"ruby-test-mri-linux-amd64": $.platform.linux + $.jdk.stable + $.env.native + gate + $.run.test_mri + { timelimit: "01:20:00" },
531-
"ruby-test-mri-linux-aarch64": $.platform.linux_aarch64 + $.jdk.stable + $.env.native + gate + $.run.test_mri + { timelimit: "01:30:00" },
531+
# "ruby-test-mri-linux-aarch64": $.platform.linux_aarch64 + $.jdk.stable + $.env.native + gate + $.run.test_mri + { timelimit: "01:30:00" }, # GR-51361
532532
"ruby-test-mri-darwin-amd64": $.platform.darwin_amd64 + $.jdk.stable + $.env.native + gate + $.run.test_mri + { timelimit: "01:30:00" },
533533
"ruby-test-mri-darwin-aarch64": $.platform.darwin_aarch64 + $.jdk.stable + $.env.native + gate + $.run.test_mri + { timelimit: "01:30:00" },
534534
"ruby-test-integration-linux-amd64": $.platform.linux + $.jdk.stable + $.env.jvm + gate + $.run.test_integration,
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 ResourceLoader.loadResource(path, language.options.CORE_AS_INTERNAL);
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: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@
1212
import java.io.FileNotFoundException;
1313
import java.io.IOException;
1414
import java.io.InputStream;
15-
import java.io.InputStreamReader;
16-
import java.nio.charset.StandardCharsets;
17-
import java.nio.file.Path;
18-
import java.nio.file.Paths;
1915
import java.util.Locale;
2016

21-
import org.truffleruby.RubyContext;
2217
import org.truffleruby.RubyLanguage;
23-
import org.truffleruby.core.string.StringUtils;
18+
import org.truffleruby.core.encoding.Encodings;
19+
import org.truffleruby.parser.MagicCommentParser;
20+
import org.truffleruby.parser.RubySource;
2421
import org.truffleruby.shared.TruffleRuby;
2522

2623
import com.oracle.truffle.api.source.Source;
@@ -30,30 +27,35 @@
3027
*/
3128
public abstract class ResourceLoader {
3229

33-
public static Source loadResource(String path, boolean internal) throws IOException {
30+
public static RubySource loadResource(String path, boolean internal) throws IOException {
3431
assert path.startsWith(RubyLanguage.RESOURCE_SCHEME);
3532

3633
if (!path.toLowerCase(Locale.ENGLISH).endsWith(".rb")) {
3734
throw new FileNotFoundException(path);
3835
}
3936

40-
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);
37+
final String resourcePath = path.substring(RubyLanguage.RESOURCE_SCHEME.length());
4438

45-
if (stream == null) {
46-
throw new FileNotFoundException(path);
39+
final byte[] sourceBytes;
40+
try (InputStream stream = ResourceLoader.class.getResourceAsStream(resourcePath)) {
41+
if (stream == null) {
42+
throw new FileNotFoundException(resourcePath);
43+
}
44+
45+
sourceBytes = stream.readAllBytes();
4746
}
4847

49-
final Source source;
5048

51-
// We guarantee that we only put UTF-8 source files into resources
52-
try (final InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
53-
source = Source.newBuilder(TruffleRuby.LANGUAGE_ID, reader, path).internal(internal).build();
54-
}
49+
var sourceTString = MagicCommentParser.createSourceTStringBasedOnMagicEncodingComment(sourceBytes,
50+
Encodings.UTF_8);
51+
52+
Source source = Source
53+
.newBuilder(TruffleRuby.LANGUAGE_ID, new ByteBasedCharSequence(sourceTString), path)
54+
.mimeType(RubyLanguage.getMimeType(false))
55+
.internal(internal)
56+
.build();
5557

56-
return source;
58+
return new RubySource(source, path, sourceTString);
5759
}
5860

5961
}

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)