Skip to content

Commit f760089

Browse files
authored
CodeObject wrapper and global CodeObject registry (#32)
Every event callback receives a _code argument which is of the Python type CodeObject. We created a CodeObjectWrapper type which wraps this object for two purposes: 1. To expose an easier to use interface which would focus on what we need to implement a tracer 2. To minimize performance hit by caching the code object data that we fetch from the Python object. A code object corresponds to the body of a function or a module. With our wrapper and registry we guarantee that each code object is processed at most once.
2 parents 0b1da45 + 2daafd1 commit f760089

File tree

9 files changed

+668
-201
lines changed

9 files changed

+668
-201
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Implement an interface for the CodeType object.
2+
3+
The Python Monitoring API passes a CodeObject to each event handler. In the current implemenation of our Tracer trait the event handlers have an argument `_code: &Bound<'_, PyAny>` which gives
4+
access to this object. This is not a good interface because the type is too generic. PyO3 has a PyCodeObject type, however it doesn't expose any public members because the type is unstable,
5+
so we cannot use that one.
6+
7+
We need to create our own type CodeObjectWrapper which can simplify access to the underlying code object type. Then we should use this type in the signature of the methods in the Tracer trait.
8+
The type should allow easy access to that functionality of the underlying code object which we will need to implement a recorder for a time-travel debugger.
9+
On the other hand it is important not to introduce any performance problems. Some ideas to think about:
10+
- Minimize the copying of values
11+
- Repeated computations on each event call could be memoized
12+
- Any other approach that minimizes the performance hit
13+
14+
Propose a design of the CodeObjectWrapper type. Write the design in design-docs/code-object.md. Do not actually implement the type for now, I need to confirm the design first.
15+
16+
Here's relevant information:
17+
* design-docs/design-001.md - shows how we write design documentation
18+
* https://docs.python.org/3/library/sys.monitoring.html - Documentation of the Python sys.montoring API
19+
* https://docs.python.org/3/reference/datamodel.html#code-objects - Description of Python Code Objects.
20+
21+
--- FOLLOW UP TASK ---
22+
Please address any inline comments on the diff, as well as any additional instructions below.
23+
24+
According to the PyO3 documentation it is preferred to use instead of Py<T>. Is it possible that the code object wrapper takes that into account? Here is relevant info:
25+
* https://pyo3.rs/v0.25.1/types.html
26+
* https://docs.rs/pyo3/0.25.1/pyo3/index.html
27+
28+
Also please add usage examples to the design documentation
29+
--- FOLLOW UP TASK ---
30+
Please address any inline comments on the diff, as well as any additional instructions below.
31+
32+
According to the PyO3 documentation it is preferred to use `Bound<'_, T>` instead of Py<T>. Is it possible that the code object wrapper takes that into account? Here is relevant info:
33+
* https://pyo3.rs/v0.25.1/types.html
34+
* https://docs.rs/pyo3/0.25.1/pyo3/index.html
35+
36+
Also please add usage examples to the design documentation
37+
--- FOLLOW UP TASK ---
38+
Implement the CodeObjectWrapper as designed. Update the Tracer trait as well as the callback_xxx functions accordingly. Write a comprehensive unit tests for CodeObjectWrapper.
39+
--- FOLLOW UP TASK ---
40+
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: 129 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ default = ["extension-module"]
1818
pyo3 = { version = "0.25.1" }
1919
runtime_tracing = "0.14.0"
2020
bitflags = "2.4"
21+
once_cell = "1.19"
22+
dashmap = "5.5"
2123

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

0 commit comments

Comments
 (0)