Skip to content

Commit ade0b88

Browse files
committed
Optimize perf & allocations
1 parent 7853bc3 commit ade0b88

6 files changed

Lines changed: 169 additions & 98 deletions

File tree

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rlbot-flatbuffers-py"
3-
version = "0.11.1"
3+
version = "0.11.2"
44
edition = "2021"
55
description = "A Python module implemented in Rust for serializing and deserializing RLBot's flatbuffers"
66
repository = "https://github.com/VirxEC/rlbot_flatbuffers_py"
@@ -19,7 +19,7 @@ name = "rlbot_flatbuffers"
1919
crate-type = ["cdylib"]
2020

2121
[dependencies]
22-
pyo3 = { version = "0.23.0", features = ["py-clone"] }
22+
pyo3 = { version = "0.23.0", features = [] }
2323
serde = "1.0.197"
2424
flatbuffers = "24.3.25"
2525
# get-size appears to be unmaintained but it's too useful here

codegen/structs.rs

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl StructBindGenerator {
9191
if (is_frozen && !frozen_needs_py) || is_all_base_types || types.is_empty() {
9292
"use crate::{flat_err_to_py, generated::rlbot::flat, FromGil};"
9393
} else {
94-
"use crate::{flat_err_to_py, generated::rlbot::flat, FromGil, IntoGil};"
94+
"use crate::{flat_err_to_py, generated::rlbot::flat, FromGil, IntoGil, PyDefault};"
9595
},
9696
));
9797

@@ -288,19 +288,23 @@ impl StructBindGenerator {
288288
format!("{variable_name}=None")
289289
}
290290
RustType::Union(_) => {
291-
needs_python = !self.is_frozen;
291+
if !self.is_frozen {
292+
needs_python = true;
293+
}
292294

293295
format!("{variable_name}=None")
294296
}
295297
RustType::Box(_) | RustType::Custom(_) => {
296298
if self.is_frozen {
297299
format!("{variable_name}=Default::default()")
298300
} else {
299-
format!("{variable_name}=crate::get_py_default()")
301+
needs_python = !self.is_frozen;
302+
format!("{variable_name}=None")
300303
}
301304
}
302305
RustType::Vec(InnerVecType::U8) => {
303-
format!("{variable_name}=crate::get_empty_pybytes()")
306+
needs_python = true;
307+
format!("{variable_name}=None")
304308
}
305309
_ => {
306310
format!("{variable_name}=Default::default()")
@@ -339,11 +343,11 @@ impl StructBindGenerator {
339343
Cow::Owned(format!("Vec<{}>", inner_type))
340344
}
341345
RustType::Vec(InnerVecType::String) => Cow::Borrowed("Vec<String>"),
342-
RustType::Vec(InnerVecType::U8) => Cow::Borrowed("Py<PyBytes>"),
346+
RustType::Vec(InnerVecType::U8) => Cow::Borrowed("Option<Py<PyBytes>>"),
343347
RustType::Box(inner_type) => Cow::Owned(if self.is_frozen {
344348
format!("super::{inner_type}")
345349
} else {
346-
format!("Py<super::{inner_type}>")
350+
format!("Option<Py<super::{inner_type}>>")
347351
}),
348352
RustType::Option(InnerOptionType::BaseType, inner_type)
349353
| RustType::Option(InnerOptionType::String, inner_type) => {
@@ -366,7 +370,7 @@ impl StructBindGenerator {
366370
RustType::Custom(inner_type) => Cow::Owned(if self.is_frozen {
367371
format!("super::{inner_type}")
368372
} else {
369-
format!("Py<super::{inner_type}>")
373+
format!("Option<Py<super::{inner_type}>>")
370374
}),
371375
RustType::Other(inner_type) => Cow::Owned(format!("super::{inner_type}")),
372376
};
@@ -385,22 +389,38 @@ impl StructBindGenerator {
385389
self,
386390
" {variable_name}: {variable_name}.map(|x| x.into_gil(py)),"
387391
);
388-
} else if let RustType::Union(inner_type) = &variable_info.rust_type {
389-
if self.is_frozen {
392+
continue;
393+
}
394+
395+
match &variable_info.rust_type {
396+
RustType::Union(inner_type) => {
397+
if self.is_frozen {
398+
write_fmt!(
399+
self,
400+
" {variable_name}: super::{}::new({variable_name}),",
401+
inner_type
402+
);
403+
} else {
404+
write_fmt!(
405+
self,
406+
" {variable_name}: Py::new(py, super::{}::new({variable_name})).unwrap(),",
407+
inner_type
408+
);
409+
}
410+
}
411+
RustType::Box(inner_type) | RustType::Custom(inner_type) if !self.is_frozen => {
390412
write_fmt!(
391413
self,
392-
" {variable_name}: super::{}::new({variable_name}),",
393-
inner_type
414+
" {variable_name}: {variable_name}.unwrap_or_else(|| super::{inner_type}::py_default(py)),",
394415
);
395-
} else {
416+
}
417+
RustType::Vec(InnerVecType::U8) => {
396418
write_fmt!(
397419
self,
398-
" {variable_name}: Py::new(py, super::{}::new({variable_name})).unwrap(),",
399-
inner_type
420+
" {variable_name}: {variable_name}.unwrap_or_else(|| PyBytes::new(py, &[]).unbind()),"
400421
);
401422
}
402-
} else {
403-
write_fmt!(self, " {variable_name},");
423+
_ => write_fmt!(self, " {variable_name},"),
404424
}
405425
}
406426

@@ -723,7 +743,7 @@ impl Generator for StructBindGenerator {
723743
"#[derive(Debug, Default, Clone, Copy)]"
724744
}
725745
} else {
726-
"#[derive(Debug, Clone)]"
746+
"#[derive(Debug)]"
727747
}
728748
);
729749
write_fmt!(self, "pub struct {} {{", self.struct_name);

0 commit comments

Comments
 (0)