Context
pyrung.core.context
ScanContext - Batched write context for a single scan cycle.
Optimizes performance by batching all tag/memory updates within a scan, reducing object allocation from O(instructions) to O(1) per scan while preserving read-after-write visibility.
ScanContext
Batched write context for a single scan cycle.
Collects all tag and memory writes during a scan cycle, then commits them all at once to produce a new SystemState. Provides read-after-write visibility so subsequent instructions in the same scan see updated values.
Attributes:
| Name | Type | Description |
|---|---|---|
_state |
The original SystemState (immutable, not modified). |
|
_tags_evolver |
Pyrsistent evolver for final tag commit. |
|
_memory_evolver |
Pyrsistent evolver for final memory commit. |
|
_tags_pending |
dict[str, Any]
|
Fast lookup dict for pending tag writes. |
_memory_pending |
dict[str, Any]
|
Fast lookup dict for pending memory writes. |
original_state
property
Access to the original (unmodified) state.
Useful for operations that need to read original values, such as computing _prev:* for edge detection.
get_tag
Get a tag value, checking pending writes first.
Provides read-after-write visibility within the same scan cycle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The tag name to retrieve. |
required |
default
|
Any
|
Value to return if tag not found. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The tag value from pending writes, original state, or default. |
get_memory
Get a memory value, checking pending writes first.
Provides read-after-write visibility within the same scan cycle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The memory key to retrieve. |
required |
default
|
Any
|
Value to return if key not found. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The memory value from pending writes, original state, or default. |
set_tag
Set a tag value (batched, committed at end of scan).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The tag name to set. |
required |
value
|
Any
|
The value to set. |
required |
set_tags
Set multiple tag values (batched, committed at end of scan).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
updates
|
dict[str, Any]
|
Dict of tag names to values. |
required |
set_memory
Set a memory value (batched, committed at end of scan).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The memory key to set. |
required |
value
|
Any
|
The value to set. |
required |
set_memory_bulk
Set multiple memory values (batched, committed at end of scan).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
updates
|
dict[str, Any]
|
Dict of memory keys to values. |
required |
commit
Commit all pending changes and advance to next scan.
Creates a new SystemState with all batched tag and memory updates, then advances scan_id and timestamp.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dt
|
float
|
Time delta in seconds to add to timestamp. |
required |
Returns:
| Type | Description |
|---|---|
SystemState
|
New SystemState with all changes applied. |