Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- GET performance: a snapshot cursor makes forward-sequential reads O(k) (only new patches applied) instead of O(N); backward jumps degrade gracefully to O(N + k×P).
- Retroactive `set_input` (out-of-order patches) is supported with automatic snapshot invalidation.
- No change to the public API (`set_input`, `get_array`, `Variable.as_of`).
- Fix quadratic reconstruction cost in `as_of` forward simulations: when the new patch is appended at the end (forward-sequential SET), the snapshot is updated instead of discarded so the next GET does not reconstruct from base through all patches; retroactive writes still invalidate correctly.

#### Technical changes

Expand Down
18 changes: 18 additions & 0 deletions openfisca_core/holders/holder.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ def get_array(self, period):
value = self._memory_storage.get(period)
if value is not None:
return value
if self._as_of:
value = self._get_as_of(period)
if value is not None:
return value
if self._disk_storage:
return self._disk_storage.get(period)
return None
Expand Down Expand Up @@ -430,11 +434,25 @@ def _set(self, period, value) -> None:
>= self.simulation.memory_config.max_memory_occupation_pc
)

if self._as_of:
# Reference sharing: reuse existing array object when value unchanged,
# otherwise store a read-only defensive copy so that callers cannot
# corrupt stored data by mutating their original array in-place.
prev = self._get_as_of(period)
if prev is not None and numpy.array_equal(value, prev):
value = prev # prev is already read-only
else:
value = value.copy()
value.flags.writeable = False

if should_store_on_disk:
self._disk_storage.put(value, period)
else:
self._memory_storage.put(value, period)

if self._as_of:
self._register_instant(period)

def put_in_cache(self, value, period) -> None:
if self._do_not_store:
return
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@

dev_requirements = [
"black >=24.8.0, <25.0",
"pytest-benchmark >=4.0.0, <5.0",
"codespell >=2.3.0, <3.0",
"colorama >=0.4.4, <0.5",
"darglint >=1.8.1, <2.0",
Expand Down
Loading