Skip to content

Commit 15d0790

Browse files
committed
Update v3/docs/opt-trace-format.md
1 parent dab9d55 commit 15d0790

1 file changed

Lines changed: 108 additions & 0 deletions

File tree

‎v3/docs/opt-trace-format.md‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,4 +375,112 @@ This execution point object is getting kinda big:
375375
}
376376
```
377377

378+
Note that in `globals`, `x` refers to heap object 1, `y` to heap object 2, and `z` to 3. If you then look at `heap`,
379+
you'll see that objects 1, 2, and 3 map to the corresponding list, tuple, and dict, respectively.
380+
381+
Look at the comments at the top of `pg_encoder.py` to learn the encoding format for various Python data types:
382+
383+
https://github.com/pgbovine/OnlinePythonTutor/blob/master/v3/pg_encoder.py
384+
385+
## Heap-to-Heap References
386+
387+
In the above example, the heap objects contained only primitives (numbers and strings), which can be directly
388+
encoded within those objects' representations in `heap`.
389+
390+
But heap objects can themselves point to other heap objects. Let's look at the following example:
391+
392+
```python
393+
c = (1, (2, None))
394+
d = (1, c)
395+
```
396+
397+
<a href="http://pythontutor.com/visualize.html#code=c+%3D+(1,+(2,+None))%0Ad+%3D+(1,+c)&mode=display&cumulative=false&py=2&curInstr=2">Jump to the end</a> of execution and notice that:
398+
399+
- `c` points to a tuple
400+
- the second element of that tuple points to another tuple
401+
- `d` points to a tuple whose second element points to what `c` points to
402+
403+
Let's look at the execution point object that corresponds to this visualization:
404+
405+
```javascript
406+
{
407+
"ordered_globals": [
408+
"c",
409+
"d"
410+
],
411+
"stdout": "",
412+
"func_name": "<module>",
413+
"stack_to_render": [],
414+
"globals": {
415+
"c": [
416+
"REF",
417+
1
418+
],
419+
"d": [
420+
"REF",
421+
3
422+
]
423+
},
424+
"heap": {
425+
"1": [
426+
"TUPLE",
427+
1,
428+
[
429+
"REF",
430+
2
431+
]
432+
],
433+
"2": [
434+
"TUPLE",
435+
2,
436+
null
437+
],
438+
"3": [
439+
"TUPLE",
440+
1,
441+
[
442+
"REF",
443+
1
444+
]
445+
]
446+
},
447+
"line": 2,
448+
"event": "return"
449+
}
450+
```
451+
452+
What's going on here? Let's start with `globals` again. `c` points to heap object 1 (`["REF", 1]`), and `d` points
453+
to heap object 3 (`["REF", 3]`).
454+
455+
Let's now look at `heap`. Heap object 1 is:
456+
457+
```javascript
458+
["TUPLE", 1, ["REF", 2]]
459+
```
460+
461+
What does this mean? It means that it represents a tuple whose first element is the number `1` and whose
462+
second element is a reference (pointer) to heap object 2.
463+
464+
Ok let's look at heap object 2:
465+
466+
```javascript
467+
["TUPLE", 2, null]
468+
```
469+
470+
which corresponds to the Python tuple object `(2, None)`.
471+
472+
Finally, heap object 3 (which `d` points to) is:
473+
474+
```javascript
475+
["TUPLE", 1, ["REF", 1]]
476+
```
477+
478+
which corresponds to the Python object `(1, c)`.
479+
480+
The ability to put "REF" objects inside of heap objects enables an arbitrary object graph to be
481+
represented in the execution trace.
482+
483+
378484
## Function Stack Frames
485+
486+
(TODO: WRITE ME!)

0 commit comments

Comments
 (0)