Skip to content

Commit 5110ec9

Browse files
committed
store sources in serialized format (again)
1 parent 179f5e4 commit 5110ec9

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

graalpython/com.oracle.graal.python.benchmarks/java/com/oracle/graal/python/benchmarks/parser/Deserializing.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import com.oracle.graal.python.parser.PythonSSTNodeFactory;
4545
import com.oracle.graal.python.runtime.PythonParser;
4646
import com.oracle.truffle.api.source.Source;
47+
import com.oracle.truffle.api.source.SourceSection;
48+
4749
import java.util.ArrayList;
4850
import java.util.List;
4951
import org.openjdk.jmh.annotations.Benchmark;
@@ -86,7 +88,8 @@ private List<Item> createSerializations() {
8688
try {
8789
PythonSSTNodeFactory sstFactory = new PythonSSTNodeFactory(core, source, parser);
8890
PythonParserImpl.CacheItem item = parser.parseWithANTLR(PythonParser.ParserMode.File, 0, core, sstFactory, source, null, null);
89-
result.add(new Item(PythonParserImpl.serialize(source, item.getAntlrResult(), item.getGlobalScope(), true)));
91+
SourceSection section = source.createSection(0, source.getLength());
92+
result.add(new Item(PythonParserImpl.serialize(section, item.getAntlrResult(), item.getGlobalScope(), true)));
9093
} catch (RuntimeException e) {
9194
// do nothing
9295
}

graalpython/com.oracle.graal.python.benchmarks/java/com/oracle/graal/python/benchmarks/parser/Serializing.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
package com.oracle.graal.python.benchmarks.parser;
4242

4343
import com.oracle.graal.python.parser.PythonParserImpl;
44+
import com.oracle.truffle.api.source.SourceSection;
45+
4446
import java.util.List;
4547
import org.openjdk.jmh.annotations.Benchmark;
4648
import org.openjdk.jmh.annotations.Setup;
@@ -62,7 +64,8 @@ public void setup() {
6264
public void execute(Blackhole bh) {
6365
for (int n = 0; n < parsingCycles; n++) {
6466
for (PythonParserImpl.CacheItem item : ssts) {
65-
bh.consume(PythonParserImpl.serialize(item.getSource(), item.getAntlrResult(), item.getGlobalScope(), true));
67+
SourceSection section = item.getSource().createSection(0, item.getSource().getLength());
68+
bh.consume(PythonParserImpl.serialize(section, item.getAntlrResult(), item.getGlobalScope(), true));
6669
}
6770
}
6871
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/PythonParserImpl.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,17 @@ public SSTNode parseExpression(String text, PythonSSTNodeFactory nodeFactory) {
115115
null).antlrResult;
116116
}
117117

118-
public static byte[] serialize(Source source, SSTNode node, ScopeInfo scope, boolean isModule) {
118+
public static byte[] serialize(SourceSection section, SSTNode node, ScopeInfo scope, boolean isModule) {
119+
Source source = section.getSource();
119120
ByteArrayOutputStream baos = new ByteArrayOutputStream();
120121
DataOutputStream dos = new DataOutputStream(baos);
121122
try {
122123
dos.writeByte(SerializationUtils.VERSION);
123124
dos.writeUTF(source.getName() == null ? "" : encodeHome(source.getName()));
124125
dos.writeUTF(source.getPath() == null ? "" : encodeHome(source.getPath()));
126+
byte[] bytes = section.getCharacters().toString().getBytes(StandardCharsets.UTF_8);
127+
dos.writeInt(bytes.length);
128+
dos.write(bytes);
125129
ScopeInfo.write(dos, scope);
126130
dos.writeInt(isModule ? 0 : node.getStartOffset());
127131
node.accept(new SSTSerializerVisitor(dos));
@@ -155,20 +159,20 @@ public byte[] serialize(RootNode rootNode) {
155159
Source source = rootNode.getSourceSection().getSource();
156160
assert source != null;
157161
CacheItem lastParserResult = cachedLastAntlrResult;
158-
if (!source.equals(lastParserResult.source)) {
162+
if (source != lastParserResult.source && !source.equals(lastParserResult.source)) {
159163
// we need to parse the source again
160164
PythonSSTNodeFactory sstFactory = new PythonSSTNodeFactory(PythonLanguage.getCore(), source, this);
161165
lastParserResult = parseWithANTLR(ParserMode.File, 0, PythonLanguage.getCore(), sstFactory, source, null, null);
162166
}
163167
if (rootNode instanceof ModuleRootNode) {
164168
// serialize whole module
165-
return serialize(source, lastParserResult.antlrResult, lastParserResult.globalScope, true);
169+
return serialize(rootNode.getSourceSection(), lastParserResult.antlrResult, lastParserResult.globalScope, true);
166170
} else {
167171
// serialize just the part
168172
SSTNodeWithScopeFinder finder = new SSTNodeWithScopeFinder(rootNode.getSourceSection().getCharIndex(), rootNode.getSourceSection().getCharEndIndex());
169173
SSTNodeWithScope rootSST = lastParserResult.antlrResult.accept(finder);
170174
// store with parent scope
171-
return serialize(source, rootSST, rootSST.getScope().getParent(), false);
175+
return serialize(rootNode.getSourceSection(), rootSST, rootSST.getScope().getParent(), false);
172176
}
173177
}
174178

@@ -195,18 +199,17 @@ public RootNode deserialize(byte[] data, String[] cellvars, String[] freevars) {
195199
}
196200
String name = decodeHome(dis.readUTF()).intern();
197201
String path = decodeHome(dis.readUTF()).intern();
198-
if (path.isEmpty()) {
199-
source = Source.newBuilder(PythonLanguage.ID, "", name).build();
200-
} else {
201-
try {
202-
source = Source.newBuilder(PythonLanguage.ID, PythonLanguage.getContext().getEnv().getPublicTruffleFile(path)).name(name).build();
203-
} catch (IOException e) {
204-
// error accessing source file, ignore and continue with an empty source
205-
source = Source.newBuilder(PythonLanguage.ID, "", name).build();
206-
}
207-
}
202+
byte[] bytes = new byte[dis.readInt()];
203+
dis.readFully(bytes);
204+
String contents = new String(bytes, StandardCharsets.UTF_8);
208205
globalScope = ScopeInfo.read(dis, null);
209206
int offset = dis.readInt();
207+
208+
if (path.isEmpty() || offset != 0) {
209+
source = Source.newBuilder(PythonLanguage.ID, contents, name).build();
210+
} else {
211+
source = Source.newBuilder(PythonLanguage.ID, PythonLanguage.getContext().getEnv().getPublicTruffleFile(path)).content(contents).name(name).build();
212+
}
210213
sstNode = new SSTDeserializer(dis, globalScope, offset).readNode();
211214
} catch (IOException e) {
212215
throw PythonLanguage.getCore().raise(PythonBuiltinClassType.ValueError, "Is not possible get correct bytecode data %s, %s", e.getClass().getSimpleName(), e.getMessage());

0 commit comments

Comments
 (0)