Skip to content

Make JSONDataRecorder honour window_size - #4072

Merged
EwoutH merged 2 commits into
mesa:mainfrom
lahari936:recorders-honour-window-size
Nov 19, 2026
Merged

EwoutH merged 2 commits into
mesa:mainfrom
lahari936:recorders-honour-window-size

Conversation

@lahari936

@lahari936 lahari936 commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

  • This PR is a bug fix, not a new feature or enhancement.

Summary

DatasetConfig.window_size is documented as "Max snapshots to keep". DataRecorder applies it, but JSONDataRecorder ignores it: every snapshot is kept, so memory grows without a bound and save_to_json() writes the whole history.

Bug / Issue

There is no issue for this yet. Reproduction on main (e988375):

from mesa import Model
from mesa.experimental.data_collection import DataRecorder, DatasetConfig, JSONDataRecorder


class Clock(Model):
    def __init__(self, recorder_class):
        super().__init__()
        self.data_registry.track_model(self, "clock", fields=["time"])
        self.recorder = recorder_class(self, {"clock": DatasetConfig(window_size=2)})


for recorder_class in (DataRecorder, JSONDataRecorder):
    model = Clock(recorder_class)
    model.run_for(5)
    print(recorder_class.__name__, list(model.recorder.get_table_dataframe("clock")["time"]))

On main:

DataRecorder [4.0, 5.0]
JSONDataRecorder [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]

With this PR JSONDataRecorder also returns [4.0, 5.0].

Implementation

JSONDataRecorder keeps each dataset's snapshots in a deque(maxlen=window_size), as DataRecorder does, so the oldest snapshot is dropped automatically once the window is full (maxlen=None is unbounded). save_to_json() writes list(snapshots).

Testing

  • test_json_recorder_keeps_only_the_last_window_size_snapshots compares JSONDataRecorder with DataRecorder for the same config, and checks the saved file. It fails on main and passes here.
  • pytest tests/experimental/test_datarecorder.py: 86 passed.

Additional Notes

  • ParquetDataRecorder and SQLDataRecorder also ignore window_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.
  • Anyone who sets window_size on a JSON recorder and relied on getting everything back now gets only the window, which is what the setting says.

🤖 Generated with Claude Code

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]>
Copilot AI balanced review requested due to automatic review settings November 12, 2026 10:59

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@github-actions github-actions Bot added experimental Release notes label testing Release notes label labels Nov 12, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -2.9% [-3.6%, -2.1%] 🔵 -1.1% [-1.3%, -0.8%]
BoltzmannWealth large 🔵 -0.6% [-1.7%, +0.3%] 🔵 -3.0% [-5.0%, -1.0%]
Schelling small 🔵 -0.2% [-1.3%, +0.8%] 🔵 -0.7% [-1.5%, +0.1%]
Schelling large 🔵 +0.5% [-0.5%, +1.3%] 🔵 +1.7% [-0.8%, +3.7%]
WolfSheep small 🔵 +0.3% [-0.5%, +1.1%] 🔵 +3.2% [-0.9%, +7.1%]
WolfSheep large 🔵 -0.9% [-2.2%, +0.3%] 🔵 -3.0% [-4.3%, -1.7%]
SugarscapeG1mt small 🔵 -1.5% [-2.6%, -0.3%] 🔵 +0.5% [-1.1%, +2.1%]
SugarscapeG1mt large 🔵 -0.7% [-1.8%, +0.4%] 🔵 +0.3% [-1.3%, +1.9%]
BoidFlockers small 🔵 -1.2% [-1.9%, -0.7%] 🔵 -0.5% [-0.9%, +0.0%]
BoidFlockers large 🔵 -1.1% [-1.3%, -0.8%] 🔵 -0.5% [-0.6%, -0.4%]
MultiLevelAllianceModel small 🔵 +3.0% [+1.6%, +4.5%] 🔵 +0.3% [-0.5%, +0.9%]
MultiLevelAllianceModel large 🔵 +2.5% [+1.6%, +3.4%] 🔵 +4.1% [+2.5%, +5.7%]

@quaquel quaquel added the bug Release notes label label Nov 12, 2026

@EwoutH EwoutH left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@EwoutH
EwoutH requested a review from codebreaker32 November 14, 2026 20:07
@lahari936

Copy link
Copy Markdown
Contributor Author

thanks, that's much cleaner. switched to deque(maxlen=window_size) like DataRecorder, the trimming code is gone and save_to_json() dumps list(snapshots). pushed as 8601b72. the 86 recorder tests still pass and the reproducer gives [4.0, 5.0]. thanks for the timings too, i didn't know del list[0] got that slow on big windows. i also updated the implementation paragraph in the description.

@mesa-triage-bot

Copy link
Copy Markdown

Thanks @lahari936 for following up on the deque change.

@EwoutH, the latest diff implements your requested bounded deque and list(snapshots) serialization; all 16 checks pass. I recommend re-reviewing this focused fix. Please take another look at your outstanding changes request.

Automated note by an AI model; it can be wrong, and maintainers make the decisions. About this bot.

@EwoutH
EwoutH requested a review from quaquel November 19, 2026 07:31

@EwoutH EwoutH left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks good to me!

@quaquel or @codebreaker32, can either of you do the final review?

@codebreaker32

Copy link
Copy Markdown
Collaborator

Thanks, looks good to me.

One thing this brings up, not for this PR: should an empty snapshot count toward the window? DataRecorder skips a collection with no agents, the JSON recorder stores it, so the two now differ (same case as #4087).

My thinking when I wrote the window for DataRecorder was that it should hold the last N snapshots that actually have data. If empty ones count, a long empty stretch fills the window and the real data is gone, which defeats the point of having it however I can see the argument for otherwise. What do you think? @EwoutH @quaquel

@EwoutH

EwoutH commented Nov 19, 2026

Copy link
Copy Markdown
Member

@lahari936 thanks for the PR, merged!

@EwoutH
EwoutH merged commit 668c892 into mesa:main Nov 19, 2026
18 checks passed
@EwoutH

EwoutH commented Nov 19, 2026

Copy link
Copy Markdown
Member

@codebreaker32 thanks for the review!

should an empty snapshot count toward the window?

Good question. I can see upsides of both:

  • counting it makes it more predictable how far the window goes back or forward in time
  • not counting it makes you have more usable data and prevents accidental overwrites if a run takes longer than intended

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.

quaquel pushed a commit that referenced this pull request Nov 20, 2026
…#4157)

JSONDataRecorder.data now holds a bounded deque per dataset (#4072), while the test added in #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.
EwoutH pushed a commit to EwoutH/mesa that referenced this pull request Nov 22, 2026
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
EwoutH pushed a commit to EwoutH/mesa that referenced this pull request Nov 22, 2026
…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)
EwoutH added a commit to EwoutH/mesa that referenced this pull request Nov 22, 2026
Add mesa#3913, mesa#4061, mesa#4072, mesa#4105, mesa#4126, mesa#4136, mesa#4141, mesa#4157, mesa#4161 and the Starlette 1.x support backported from mesa#4007, and move the release date to 2026-11-22.

Assisted-by: Claude Opus 5.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label experimental Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants