Skip to content

Commit d3cc7e8

Browse files
committed
Refine optimizations
1 parent cdea642 commit d3cc7e8

File tree

5 files changed

+43
-8
lines changed

5 files changed

+43
-8
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rlbot-flatbuffers-py"
3-
version = "0.11.5"
3+
version = "0.11.6"
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"

codegen/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub enum PythonBindType {
2525

2626
impl PythonBindType {
2727
pub const BASE_TYPES: [&'static str; 6] = ["bool", "i32", "u32", "f32", "String", "u8"];
28-
pub const FROZEN_TYPES: [&'static str; 17] = [
28+
pub const FROZEN_TYPES: [&'static str; 19] = [
2929
"GoalInfo",
3030
"GamePacket",
3131
"PlayerInfo",
@@ -43,7 +43,10 @@ impl PythonBindType {
4343
"Vector2",
4444
"ControllableInfo",
4545
"ControllableTeamInfo",
46+
"BoostPad",
47+
"PredictionSlice",
4648
];
49+
pub const SEMI_FROZEN_TYPES: [&'static str; 2] = ["FieldInfo", "BallPrediction"];
4750
pub const FROZEN_NEEDS_PY: [&'static str; 3] = ["GamePacket", "BallInfo", "CollisionShape"];
4851
pub const UNIONS: [&'static str; 4] = [
4952
"PlayerClass",

codegen/structs.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub struct StructBindGenerator {
5454
is_all_base_types: bool,
5555
is_frozen: bool,
5656
frozen_needs_py: bool,
57+
is_semi_frozen: bool,
5758
}
5859

5960
macro_rules! write_str {
@@ -77,7 +78,8 @@ impl StructBindGenerator {
7778
types: Vec<CustomType>,
7879
) -> Option<Self> {
7980
let is_frozen = PythonBindType::FROZEN_TYPES.contains(&struct_name.as_str());
80-
let frozen_needs_py = PythonBindType::FROZEN_NEEDS_PY.contains(&struct_name.as_str());
81+
let frozen_needs_py = is_frozen && PythonBindType::FROZEN_NEEDS_PY.contains(&struct_name.as_str());
82+
let is_semi_frozen = !is_frozen && PythonBindType::SEMI_FROZEN_TYPES.contains(&struct_name.as_str());
8183

8284
let is_all_base_types = types
8385
.iter()
@@ -120,6 +122,7 @@ impl StructBindGenerator {
120122
is_all_base_types,
121123
is_frozen,
122124
frozen_needs_py,
125+
is_semi_frozen,
123126
})
124127
}
125128

@@ -726,6 +729,8 @@ impl Generator for StructBindGenerator {
726729
self,
727730
if self.is_frozen {
728731
"#[pyclass(module = \"rlbot_flatbuffers\", subclass, get_all, frozen)]"
732+
} else if self.is_semi_frozen {
733+
"#[pyclass(module = \"rlbot_flatbuffers\", subclass, get_all)]"
729734
} else if self.types.is_empty() {
730735
"#[pyclass(module = \"rlbot_flatbuffers\", subclass, frozen)]"
731736
} else {

pybench.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,37 @@ def test_gtp():
3535

3636

3737
def test_ballpred():
38-
print("Testing BallPrediction")
38+
print("Testing 10s BallPrediction")
3939

4040
times = []
4141

4242
ballPred = flat.BallPrediction([flat.PredictionSlice(1) for _ in range(120 * 10)])
4343

4444
print(len(ballPred.pack()))
4545

46-
for _ in range(100_000):
46+
for _ in range(40_000):
47+
start = time_ns()
48+
49+
packed = ballPred.pack()
50+
flat.BallPrediction.unpack(packed)
51+
52+
times.append(time_ns() - start)
53+
54+
print(f"Total time: {sum(times) / 1_000_000_000:.3f}s")
55+
avg_time_ns = sum(times) / len(times)
56+
print(f"Average time per: {avg_time_ns / 1000:.1f}us")
57+
print(f"Minimum time per: {min(times) / 1000:.1f}us")
58+
59+
print()
60+
print("Testing 6s BallPrediction")
61+
62+
times = []
63+
64+
ballPred = flat.BallPrediction([flat.PredictionSlice(1) for _ in range(120 * 6)])
65+
66+
print(len(ballPred.pack()))
67+
68+
for _ in range(40_000):
4769
start = time_ns()
4870

4971
packed = ballPred.pack()
@@ -72,6 +94,8 @@ def find_slice_at_time(ball_prediction: flat.BallPrediction, game_time: float):
7294

7395

7496
def test_loop():
97+
print("Testing access times")
98+
7599
ballPred = flat.BallPrediction([flat.PredictionSlice(1) for _ in range(120 * 6)])
76100

77101
start = time_ns()
@@ -94,6 +118,7 @@ def test_loop():
94118

95119
times.append(time_ns() - start)
96120

121+
print()
97122
print(f"Total time: {sum(times) / 1_000_000_000:.3f}s")
98123
avg_time_ns = sum(times) / len(times)
99124
print(f"Average time per: {avg_time_ns / 1000:.1f}us")
@@ -107,6 +132,7 @@ def test_loop():
107132

108133
times.append(time_ns() - start)
109134

135+
print()
110136
print(f"Total time: {sum(times) / 1_000_000_000:.3f}s")
111137
avg_time_ns = sum(times) / len(times)
112138
print(f"Average time per: {avg_time_ns / 1000:.1f}us")
@@ -120,15 +146,16 @@ def test_loop():
120146

121147
times.append(time_ns() - start)
122148

149+
print()
123150
print(f"Total time: {sum(times) / 1_000_000_000:.3f}s")
124151
avg_time_ns = sum(times) / len(times)
125152
print(f"Average time per: {avg_time_ns / 1000:.1f}us")
126153
print(f"Minimum time per: {min(times) / 1000:.1f}us")
127154

128155

129156
if __name__ == "__main__":
130-
test_gtp()
131-
print()
157+
# test_gtp()
158+
# print()
132159
test_ballpred()
133160
print()
134161
test_loop()

0 commit comments

Comments
 (0)