8000 GH-99554: `marshal` bytecode more efficiently by brandtbucher · Pull Request #99555 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-99554: marshal bytecode more efficiently #99555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Terminate the compressed bytecode with two zeros
  • Loading branch information
brandtbucher committed Dec 18, 2022
commit bcd798058296083c29ff46ae52af3b569c7b44d4
48 changes: 24 additions & 24 deletions Programs/test_frozenmain.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions Python/marshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,11 @@ w_bytecode(PyCodeObject *code, WFILE *p)
}
i += _PyOpcode_Caches[opcode];
}
// Terminate with two zero bytes, so that programs scanning .pyc files can
// skip over the bytecode (even if they don't know the compression scheme).
// This is simpler than writing the compressed size in the header, which
// requires two loops (one to count the bytes, then one to write them):
w_short(0, p);
Comment on lines +308 to +312
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh, sorry, I can't stomach this. It feels certain that at some point in the future we'll have a corner case where we encounter \0\0 in the middle. If an extra pass is too expensive let's just go with what you had before. Or let's not do this -- I am feeling pretty lukewarm about this scheme, especially since the register VM will require a whole new approach anyway.

}

static int
Expand Down Expand Up @@ -969,8 +974,8 @@ r_bytecode(RFILE *p)
else {
oparg = 0;
}
assert(0x00 <= opcode && opcode < 0x100);
assert(0x00 <= oparg && oparg < 0x100);
assert(0x01 <= opcode && opcode <= 0xFF);
assert(0x00 <= oparg && oparg <= 0xFF);
buffer[i].opcode = opcode;
buffer[i].oparg = oparg;
i++;
Expand All @@ -981,7 +986,10 @@ r_bytecode(RFILE *p)
i++;
}
}
if (i != size) {
// The compressed bytecode is terminated with two zero bytes (see the
// comment at the bottom of w_bytecode):
int zero_zero = r_short(p);
if (zero_zero == EOF || zero_zero != 0 || i != size) {
const char *e = "bad marshal data (bytecode size incorrect)";
PyErr_SetString(PyExc_ValueError, e);
return NULL;
Expand Down
7 changes: 4 additions & 3 deletions Tools/build/umarshal.py
65C4
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,13 @@ def r_bytecode(self) -> bytes:
oparg_byte = self.r_byte()
else:
oparg_byte = 0
assert 0x00 <= opcode_byte < 0x100
assert 0x00 <= oparg_byte < 0x100
assert 0x01 <= opcode_byte <= 0xFF
assert 0x00 <= oparg_byte <= 0xFF
bytecode.extend([opcode_byte, oparg_byte])
for _ in range(opcode._inline_cache_entries[opcode_byte]):
bytecode.extend([CACHE, 0])
assert len(bytecode) == nbytes
zero_zero = self.r_short()
assert zero_zero == 0 and len(bytecode) == nbytes
return bytes(bytecode)

def _r_object(self) -> Any:
Expand Down
0