Skip to content

armgit / burr

Public Imported from GitHub
0

Build applications that make decisions (chatbots, agents, simulations, etc...). Monitor, trace, persist, and execute on your own infrastructure.

Makes state copy/write more efficient

elijahbenizzya0908095e445
diff --git a/burr/core/state.py b/burr/core/state.py
index e8cf03f4..5b073af6 100644
--- a/burr/core/state.py
+++ b/burr/core/state.py
@@ -284,10 +284,16 @@ class State(Mapping, Generic[StateType]):
def apply_operation(self, operation: StateDelta) -> "State[StateType]":
"""Applies a given operation to the state, returning a new state"""
- # Moved to copy.copy instead of copy.deepcopy
- # TODO -- just copy the ones that have changed
- # And if they can't be copied then we use the same ones...
new_state = copy.copy(self._state) # TODO -- restrict to just the read keys
+ for field in operation.reads():
+ # This ensures we only copy the fields that are read by value
+ # and copy the others by value
+ # TODO -- make this more efficient when we have immutable transactions
+ # with event-based history: https://github.com/DAGWorks-Inc/burr/issues/33
+ if field in new_state:
+ # currently the reads() includes optional fields
+ # We should clean that up, but this is an internal API so not worried now
+ new_state[field] = copy.deepcopy(new_state[field])
operation.validate(new_state)
operation.apply_mutate(
new_state
diff --git a/tests/core/test_state.py b/tests/core/test_state.py
index 01a871f3..5933f3b5 100644
--- a/tests/core/test_state.py
+++ b/tests/core/test_state.py
@@ -107,7 +107,7 @@ def test_state_wipe_keep():
def test_state_append_validate_failure():
state = State({"foo": "bar"})
with pytest.raises(ValueError, match="non-appendable"):
- state.append(foo="baz", bar="qux")
+ state.append(foo="baz")
def test_state_extend_validate_failure():