Skip to content

Commit 06bf6ad

Browse files
committed
feat: cache code objects in global registry
1 parent 41e0c06 commit 06bf6ad

File tree

7 files changed

+212
-20
lines changed

7 files changed

+212
-20
lines changed

.agents/tasks/2025/08/21-0939-codetype-interface

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,5 @@ Also please add usage examples to the design documentation
3838
Implement the CodeObjectWrapper as designed. Update the Tracer trait as well as the callback_xxx functions accordingly. Write a comprehensive unit tests for CodeObjectWrapper.
3939
--- FOLLOW UP TASK ---
4040
There is an issue in the current implementation. We don't use caching effectively, since we create a new CodeObjectWrapper at each callback_xxx call. We need a global cache, probably keyed by the code object id. Propose design changes and update the design documents. Don't implement the changes themselves before I approve them.
41+
--- FOLLOW UP TASK ---
42+
Implement the global code object registry.

codetracer-python-recorder/Cargo.lock

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

codetracer-python-recorder/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pyo3 = { version = "0.25.1" }
1919
runtime_tracing = "0.14.0"
2020
bitflags = "2.4"
2121
once_cell = "1.19"
22+
dashmap = "5.5"
2223

2324
[dev-dependencies]
2425
pyo3 = { version = "0.25.1", features = ["auto-initialize"] }

codetracer-python-recorder/src/code_object.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use once_cell::sync::OnceCell;
22
use pyo3::prelude::*;
33
use pyo3::types::PyCode;
4+
use dashmap::DashMap;
5+
use std::sync::Arc;
46

57
/// A wrapper around Python `code` objects providing cached access to
68
/// common attributes and line information.
@@ -128,3 +130,35 @@ impl CodeObjectWrapper {
128130
}
129131
}
130132
}
133+
134+
/// Global registry caching `CodeObjectWrapper` instances by code object id.
135+
#[derive(Default)]
136+
pub struct CodeObjectRegistry {
137+
map: DashMap<usize, Arc<CodeObjectWrapper>>,
138+
}
139+
140+
impl CodeObjectRegistry {
141+
/// Retrieve the wrapper for `code`, inserting a new one if needed.
142+
pub fn get_or_insert(
143+
&self,
144+
py: Python<'_>,
145+
code: &Bound<'_, PyCode>,
146+
) -> Arc<CodeObjectWrapper> {
147+
let id = code.as_ptr() as usize;
148+
self.map
149+
.entry(id)
150+
.or_insert_with(|| Arc::new(CodeObjectWrapper::new(py, code)))
151+
.clone() //AI? Why do we need to clone here?
152+
}
153+
154+
/// Remove the wrapper for a given code id, if present.
155+
pub fn remove(&self, id: usize) {
156+
self.map.remove(&id);
157+
}
158+
159+
/// Clear all cached wrappers.
160+
pub fn clear(&self) {
161+
self.map.clear();
162+
}
163+
}
164+

codetracer-python-recorder/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use pyo3::prelude::*;
55

66
pub mod code_object;
77
pub mod tracer;
8-
pub use crate::code_object::CodeObjectWrapper;
8+
pub use crate::code_object::{CodeObjectRegistry, CodeObjectWrapper};
99
pub use crate::tracer::{install_tracer, uninstall_tracer, EventSet, Tracer};
1010

1111
/// Global flag tracking whether tracing is active.

0 commit comments

Comments
 (0)