Skip to content

Commit 40b7f90

Browse files
committed
add marshal support for interning strings - used for the co_filename serialization
1 parent 154ce8d commit 40b7f90

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/MarshalModuleBuiltins.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ Object doit(VirtualFrame frame, PMemoryView bytes,
199199
private static final char TYPE_LONG = 'l';
200200
private static final char TYPE_PINT = 'L';
201201
private static final char TYPE_STRING = 's';
202-
// private final static char TYPE_INTERNED = 't';
202+
private static final char TYPE_INTERNED = 't';
203203
// private final static char TYPE_STRINGREF = 'R';
204204
private static final char TYPE_BYTESLIKE = 'b';
205205
private static final char TYPE_TUPLE = '(';
@@ -213,6 +213,14 @@ Object doit(VirtualFrame frame, PMemoryView bytes,
213213
private static final int MAX_MARSHAL_STACK_DEPTH = 2000;
214214
private static final int CURRENT_VERSION = 1;
215215

216+
static final class InternedString {
217+
public final String string;
218+
219+
private InternedString(String string) {
220+
this.string = string;
221+
}
222+
}
223+
216224
private abstract static class PNodeWithState extends PNodeWithContext {
217225
@Child private PythonObjectFactory objectFactory;
218226
@Child private PRaiseNode raiseNode;
@@ -374,6 +382,12 @@ void handlePString(PString v, int version, DataOutputStream buffer) {
374382
writeString(v.getValue(), version, buffer);
375383
}
376384

385+
@Specialization
386+
void handleInternedString(InternedString v, int version, DataOutputStream buffer) {
387+
writeByte(TYPE_INTERNED, version, buffer);
388+
writeString(v.string, version, buffer);
389+
}
390+
377391
@Specialization
378392
void handleBytesLike(VirtualFrame frame, PIBytesLike v, int version, DataOutputStream buffer,
379393
@Cached("create()") BytesNodes.ToBytesNode toBytesNode) {
@@ -440,7 +454,7 @@ void handlePCode(VirtualFrame frame, PCode c, int version, DataOutputStream buff
440454
getRecursiveNode().execute(frame, factory().createTuple(c.getVarnames() == null ? new Object[0] : c.getVarnames()), version, buffer);
441455
getRecursiveNode().execute(frame, factory().createTuple(c.getFreeVars() == null ? new Object[0] : c.getFreeVars()), version, buffer);
442456
getRecursiveNode().execute(frame, factory().createTuple(c.getCellVars() == null ? new Object[0] : c.getCellVars()), version, buffer);
443-
getRecursiveNode().execute(frame, c.getFilename(), version, buffer);
457+
getRecursiveNode().execute(frame, new InternedString(c.getFilename()), version, buffer);
444458
getRecursiveNode().execute(frame, c.getName(), version, buffer);
445459
writeInt(c.getFirstLineNo(), version, buffer);
446460
writeBytes(c.getLnotab() == null ? new byte[0] : c.getLnotab(), version, buffer);
@@ -570,6 +584,10 @@ private String readString() {
570584
return text;
571585
}
572586

587+
private String readInternedString() {
588+
return readString().intern();
589+
}
590+
573591
private byte[] readBytes() {
574592
int len = readInt();
575593
byte[] bytes = Arrays.copyOfRange(data, index, index + len);
@@ -715,6 +733,8 @@ private Object readObject(int depth) {
715733
return readDouble();
716734
case TYPE_STRING:
717735
return readString();
736+
case TYPE_INTERNED:
737+
return readInternedString();
718738
case TYPE_BYTESLIKE:
719739
return readBytesLike();
720740
case TYPE_TUPLE: {

0 commit comments

Comments
 (0)