Skip to content

Commit 616930e

Browse files
authored
Merge pull request #3998 from eregon/no-source-bytes-for-java
Do not pass source bytes to Loader.java at all and mark newlines for lazy DefNodes
2 parents 787b88c + 96edaf3 commit 616930e

5 files changed

Lines changed: 18 additions & 20 deletions

File tree

java-wasm/src/main/java/org/jruby/parser/prism/wasm/Prism.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public byte[] serialize(byte[] packedOptions, byte[] sourceBytes, int sourceLeng
8383
public ParseResult serializeParse(byte[] packedOptions, String source) {
8484
var sourceBytes = source.getBytes(StandardCharsets.ISO_8859_1);
8585
byte[] result = serialize(packedOptions, sourceBytes, sourceBytes.length);
86-
return Loader.load(result, sourceBytes);
86+
return Loader.load(result);
8787
}
8888

8989
@Override

java/org/ruby_lang/prism/MarkNewlinesVisitor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ final class MarkNewlinesVisitor extends AbstractNodeVisitor<Void> {
66
private final Nodes.Source source;
77
private boolean[] newlineMarked;
88

9-
MarkNewlinesVisitor(Nodes.Source source, boolean[] newlineMarked) {
9+
MarkNewlinesVisitor(Nodes.Source source) {
1010
this.source = source;
11-
this.newlineMarked = newlineMarked;
11+
this.newlineMarked = new boolean[1 + source.getLineCount()];
1212
}
1313

1414
@Override

rakelib/serialization.rake

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ task "test:java_loader:internal" => :compile do
2525
puts
2626
puts path
2727
serialized = Prism.dump_file(path)
28-
source_bytes = File.binread(path).unpack('c*')
29-
parse_result = org.ruby_lang.prism.Loader.load(serialized.unpack('c*'), source_bytes)
28+
parse_result = org.ruby_lang.prism.Loader.load(serialized.unpack('c*'))
3029
puts parse_result.value
3130
end
3231
end

templates/java/org/ruby_lang/prism/Loader.java.erb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<%- string_type = Prism::Template::JAVA_STRING_TYPE -%>
22
package org.ruby_lang.prism;
33

4-
import org.ruby_lang.prism.Nodes;
5-
64
import java.lang.Short;
75
import java.math.BigInteger;
86
import java.nio.ByteBuffer;
@@ -15,8 +13,8 @@ import java.util.Locale;
1513
// @formatter:off
1614
public class Loader {
1715

18-
public static ParseResult load(byte[] serialized, byte[] sourceBytes) {
19-
return new Loader(serialized).load(sourceBytes);
16+
public static ParseResult load(byte[] serialized) {
17+
return new Loader(serialized).load();
2018
}
2119

2220
// Overridable methods
@@ -76,15 +74,14 @@ public class Loader {
7674
private Charset encodingCharset;
7775
<%- end -%>
7876
private ConstantPool constantPool;
77+
private Nodes.Source source = null;
7978

8079
protected Loader(byte[] serialized) {
8180
this.buffer = ByteBuffer.wrap(serialized).order(ByteOrder.nativeOrder());
8281
}
8382

84-
// We pass sourceBytes here and not in the constructor to avoid keeping
85-
// the sourceBytes in memory unnecessarily with lazy DefNode's which hold on the Loader.
86-
protected ParseResult load(byte[] sourceBytes) {
87-
Nodes.Source source = new Nodes.Source(sourceBytes);
83+
protected ParseResult load() {
84+
this.source = new Nodes.Source();
8885

8986
expect((byte) 'P', "incorrect prism header");
9087
expect((byte) 'R', "incorrect prism header");
@@ -129,8 +126,7 @@ public class Loader {
129126
throw new Error("Expected to consume all bytes while deserializing but there were " + left + " bytes left");
130127
}
131128

132-
boolean[] newlineMarked = new boolean[1 + source.getLineCount()];
133-
MarkNewlinesVisitor visitor = new MarkNewlinesVisitor(source, newlineMarked);
129+
MarkNewlinesVisitor visitor = new MarkNewlinesVisitor(source);
134130
node.accept(visitor);
135131
} else {
136132
node = null;
@@ -409,11 +405,17 @@ public class Loader {
409405
}
410406

411407
Nodes.DefNode createDefNodeFromSavedPosition(<%= base_params_sig -%>, int bufferPosition) {
408+
Nodes.DefNode node;
412409
// This method mutates the buffer position and may be called from different threads so we must synchronize
413410
synchronized (this) {
414411
buffer.position(bufferPosition);
415-
return createDefNode(<%= base_params.join(", ") -%>);
412+
node = createDefNode(<%= base_params.join(", ") -%>);
416413
}
414+
415+
MarkNewlinesVisitor visitor = new MarkNewlinesVisitor(source);
416+
node.accept(visitor);
417+
418+
return node;
417419
}
418420
<%- array_types.uniq.each do |type| -%>
419421

templates/java/org/ruby_lang/prism/Nodes.java.erb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,10 @@ public abstract class Nodes {
4747
}
4848

4949
public static final class Source {
50-
public final byte[] bytes;
5150
private int startLine = 1;
5251
private int[] lineOffsets = null;
5352

54-
Source(byte[] bytes) {
55-
this.bytes = bytes;
53+
Source() {
5654
}
5755

5856
void setStartLine(int startLine) {
@@ -70,7 +68,6 @@ public abstract class Nodes {
7068

7169
// 0-based
7270
public int findLine(int byteOffset) {
73-
if (byteOffset >= bytes.length) byteOffset = bytes.length - 1;
7471
assert byteOffset >= 0 : byteOffset;
7572
int index = Arrays.binarySearch(lineOffsets, byteOffset);
7673
int line;

0 commit comments

Comments
 (0)