Skip to content

Commit 64ab147

Browse files
committed
Compress bytecode using LZ4
1 parent ad8a182 commit 64ab147

File tree

8 files changed

+36
-11
lines changed

8 files changed

+36
-11
lines changed

Cargo.lock

Lines changed: 18 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bytecode/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ license = "MIT"
99

1010

1111
[dependencies]
12+
bincode = "1.1"
1213
bitflags = "1.1"
14+
lz4-compress = "0.1.1"
1315
num-bigint = { version = "0.2", features = ["serde"] }
1416
num-complex = { version = "0.2", features = ["serde"] }
1517
serde = { version = "1.0", features = ["derive"] }

bytecode/src/bytecode.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,18 @@ impl CodeObject {
378378
}
379379
}
380380

381+
/// Load a code object from bytes
382+
pub fn from_bytes(data: &[u8]) -> Result<Self, Box<dyn std::error::Error>> {
383+
let data = lz4_compress::decompress(data)?;
384+
bincode::deserialize::<Self>(&data).map_err(|e| e.into())
385+
}
386+
387+
/// Serialize this bytecode to bytes.
388+
pub fn to_bytes(&self) -> Vec<u8> {
389+
let data = bincode::serialize(&self).expect("Code object must be serializable");
390+
lz4_compress::compress(&data)
391+
}
392+
381393
pub fn get_constants(&self) -> impl Iterator<Item = &Constant> {
382394
self.instructions.iter().filter_map(|x| {
383395
if let Instruction::LoadConst { value } = x {

derive/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,5 @@ quote = "0.6.11"
1919
proc-macro2 = "0.4.27"
2020
rustpython-compiler = { path = "../compiler", version = "0.1.1" }
2121
rustpython-bytecode = { path = "../bytecode", version = "0.1.1" }
22-
bincode = "1.1"
2322
proc-macro-hack = { version = "0.5", optional = true }
2423
maplit = "1.0"

derive/src/compile_bytecode.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
//! ```
1515
1616
use crate::{extract_spans, Diagnostic};
17-
use bincode;
1817
use proc_macro2::{Span, TokenStream as TokenStream2};
1918
use quote::quote;
2019
use rustpython_bytecode::bytecode::{CodeObject, FrozenModule};
@@ -249,11 +248,11 @@ pub fn impl_py_compile_bytecode(input: TokenStream2) -> Result<TokenStream2, Dia
249248
.into_iter()
250249
.map(|(module_name, FrozenModule { code, package })| {
251250
let module_name = LitStr::new(&module_name, Span::call_site());
252-
let bytes = bincode::serialize(&code).expect("Failed to serialize");
251+
let bytes = code.to_bytes();
253252
let bytes = LitByteStr::new(&bytes, Span::call_site());
254253
quote! {
255254
#module_name.into() => ::rustpython_vm::bytecode::FrozenModule {
256-
code: bincode::deserialize::<::rustpython_vm::bytecode::CodeObject>(
255+
code: ::rustpython_vm::bytecode::CodeObject::from_bytes(
257256
#bytes
258257
).expect("Deserializing CodeObject failed"),
259258
package: #package,
@@ -263,7 +262,6 @@ pub fn impl_py_compile_bytecode(input: TokenStream2) -> Result<TokenStream2, Dia
263262

264263
let output = quote! {
265264
({
266-
use ::rustpython_vm::__exports::bincode;
267265
use ::rustpython_vm::__exports::hashmap;
268266
hashmap! {
269267
#(#modules),*

vm/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ hex = "0.3.2"
5252
hexf-parse = "0.1.0"
5353
indexmap = "1.0.2"
5454
crc = "^1.0.0"
55-
bincode = "1.1.4"
5655
unicode_categories = "0.1.1"
5756
unicode_names2 = "0.2.2"
5857
unicode-casing = "0.1.0"

vm/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,5 @@ pub use rustpython_bytecode::*;
7979

8080
#[doc(hidden)]
8181
pub mod __exports {
82-
pub use bincode;
8382
pub use maplit::hashmap;
8483
}

vm/src/stdlib/marshal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use crate::pyobject::{PyObjectRef, PyResult};
55
use crate::vm::VirtualMachine;
66

77
fn marshal_dumps(co: PyCodeRef, _vm: &VirtualMachine) -> PyBytes {
8-
PyBytes::new(bincode::serialize(&co.code).unwrap())
8+
PyBytes::new(co.code.to_bytes())
99
}
1010

1111
fn marshal_loads(code_bytes: PyBytesRef, vm: &VirtualMachine) -> PyResult<PyCode> {
12-
let code = bincode::deserialize::<bytecode::CodeObject>(&code_bytes)
12+
let code = bytecode::CodeObject::from_bytes(&code_bytes)
1313
.map_err(|_| vm.new_value_error("Couldn't deserialize python bytecode".to_owned()))?;
1414
Ok(PyCode { code })
1515
}

0 commit comments

Comments
 (0)