Optimize object_patch memory churn#6204
Merged
FeodorFitsner merged 1 commit intomainfrom Feb 22, 2026
Merged
Conversation
Reduce memory churn and redundant per-instance monkeypatching in object_patch.py. Add a shared _control_setattr and install it once on control classes (and set a __flet_control_cls__ marker) instead of assigning __setattr__ on every instance. Introduce typed index storage and a safer fallback format to avoid expensive hashing and TypeError paths. Add helper methods to update list/dict snapshots in-place (preserving references) rather than replacing them. Update uses throughout DiffBuilder to use these helpers. Add a new test (test_object_diff_memory_churn.py) that verifies steady-state memory growth remains bounded under repeated diffs.
Deploying flet-docs with
|
| Latest commit: |
608e0b7
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://a9864f54.flet-docs.pages.dev |
| Branch Preview URL: | https://mem-fix-2.flet-docs.pages.dev |
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request optimizes memory usage in the object diffing system by reducing unnecessary allocations during repeated diff operations. The main changes focus on: (1) sharing a single __setattr__ implementation across all control class instances instead of creating per-instance closures, (2) optimizing index storage with typed buckets to reduce hashing overhead, and (3) updating collection snapshots in-place to preserve object references rather than creating new copies.
Changes:
- Introduced shared
_control_setattrfunction with class-level assignment to replace per-instance monkeypatching - Refactored index storage to use type-bucketed dictionaries for better performance and reduced TypeErrors
- Added helper methods
_update_list_snapshotand_update_dict_snapshotto update snapshots in-place - Added comprehensive memory churn regression test
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| sdk/python/packages/flet/tests/test_object_diff_memory_churn.py | New test that verifies steady-state memory growth remains bounded under repeated diff operations |
| sdk/python/packages/flet/src/flet/controls/object_patch.py | Core optimizations: shared setattr, typed index storage, in-place snapshot updates |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reduce memory churn and redundant per-instance monkeypatching in object_patch.py. Add a shared _control_setattr and install it once on control classes (and set a flet_control_cls marker) instead of assigning setattr on every instance. Introduce typed index storage and a safer fallback format to avoid expensive hashing and TypeError paths. Add helper methods to update list/dict snapshots in-place (preserving references) rather than replacing them. Update uses throughout DiffBuilder to use these helpers. Add a new test (test_object_diff_memory_churn.py) that verifies steady-state memory growth remains bounded under repeated diffs.
Summary by Sourcery
Reduce memory churn and stabilize memory usage in object patching and diff building by reusing control setattr logic, optimizing index storage, and updating collection snapshots in place.
Enhancements:
Tests: