Repository navigation
Make JSONDataRecorder honour window_size - #4072
Conversation
DatasetConfig.window_size is documented as the maximum number of snapshots to keep. DataRecorder applies it, but JSONDataRecorder kept every snapshot, so memory grew without bound and save_to_json wrote the full history. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
|
Performance benchmarks:
|
There was a problem hiding this comment.
Thanks, reproduced on main (JSON kept all 6 snapshots) and confirmed [4.0, 5.0] here, matching DataRecorder.
Could you use deque(maxlen=window_size) for the storage instead, like DataRecorder does? Then the trimming code isn't needed, and save_to_json() only needs list(snapshots):
- self.data[dataset_name] = []
+ self.data[dataset_name] = deque(maxlen=self.configs[dataset_name].window_size)
...
- window_size = self.configs[dataset_name].window_size
- if window_size and len(self.data[dataset_name]) > window_size:
- del self.data[dataset_name][0]
...
- json.dump(snapshots, f, indent=2, cls=NumpyJSONEncoder)
+ json.dump(list(snapshots), f, indent=2, cls=NumpyJSONEncoder)maxlen=None is unbounded, and window_size is already validated as > 0 or None. With this change on top of 347f2cc, the 86 recorder tests pass and the reproducer still gives [4.0, 5.0].
It's also cheaper per snapshot, because del list[0] shifts the whole list each time (append and trim only, timeit):
| window_size | list + del [0] |
deque(maxlen=…) |
|---|---|---|
| 10 | 81 ns | 44 ns |
| 1 000 | 330 ns | 44 ns |
| 100 000 | 27 600 ns | 70 ns |
@codebreaker32 do you agree?
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
|
thanks, that's much cleaner. switched to |
|
Thanks @lahari936 for following up on the deque change. @EwoutH, the latest diff implements your requested bounded deque and Automated note by an AI model; it can be wrong, and maintainers make the decisions. About this bot. |
EwoutH
left a comment
There was a problem hiding this comment.
Thanks, looks good to me!
@quaquel or @codebreaker32, can either of you do the final review?
|
Thanks, looks good to me. One thing this brings up, not for this PR: should an empty snapshot count toward the window? My thinking when I wrote the window for |
|
@lahari936 thanks for the PR, merged! |
|
@codebreaker32 thanks for the review!
Good question. I can see upsides of both:
I see a case for both. Not sure yet, I'm nudging towards the former (more explicit), but there's also a case for the latter. |
DatasetConfig.window_size is documented as the maximum number of snapshots to keep. DataRecorder applies it, but JSONDataRecorder kept every snapshot, so memory grew without bound and save_to_json wrote the full history. Adapted for 3.5.x: 3.5 recorders do not record when a run ends (mesa#4041 is 4.0-only), so after run_for(4) the window holds times 2.0 and 3.0; DataRecorder gives the same. The test's expected values are changed accordingly. (cherry picked from commit 668c892) Assisted-by: Claude Opus 5.5
…mesa#4157) JSONDataRecorder.data now holds a bounded deque per dataset (mesa#4072), while the test added in mesa#4061 compares it to a list, so main fails test_json_recorder_stores_a_plain_array_as_a_list. Convert to a list before comparing. (cherry picked from commit e89f241)
Pre-PR Checklist
Summary
DatasetConfig.window_sizeis documented as "Max snapshots to keep".DataRecorderapplies it, butJSONDataRecorderignores it: every snapshot is kept, so memory grows without a bound andsave_to_json()writes the whole history.Bug / Issue
There is no issue for this yet. Reproduction on
main(e988375):On
main:With this PR
JSONDataRecorderalso returns[4.0, 5.0].Implementation
JSONDataRecorderkeeps each dataset's snapshots in adeque(maxlen=window_size), asDataRecorderdoes, so the oldest snapshot is dropped automatically once the window is full (maxlen=Noneis unbounded).save_to_json()writeslist(snapshots).Testing
test_json_recorder_keeps_only_the_last_window_size_snapshotscomparesJSONDataRecorderwithDataRecorderfor the same config, and checks the saved file. It fails onmainand passes here.pytest tests/experimental/test_datarecorder.py: 86 passed.Additional Notes
ParquetDataRecorderandSQLDataRecorderalso ignorewindow_size(Parquet keeps all 6 rows in the example above). They store to a file or database rather than in memory, so a window there means deleting persisted rows. That is a design question, so I left it out of this PR.window_sizeon a JSON recorder and relied on getting everything back now gets only the window, which is what the setting says.🤖 Generated with Claude Code