Skip to content

Comments

Optimize object_patch memory churn#6204

Merged
FeodorFitsner merged 1 commit intomainfrom
mem-fix-2
Feb 22, 2026
Merged

Optimize object_patch memory churn#6204
FeodorFitsner merged 1 commit intomainfrom
mem-fix-2

Conversation

@FeodorFitsner
Copy link
Contributor

@FeodorFitsner FeodorFitsner commented Feb 22, 2026

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:

  • Share a single _control_setattr implementation across control classes and mark configured classes to avoid per-instance monkeypatching.
  • Optimize index storage in DiffBuilder with typed buckets and a safer fallback format to reduce hashing overhead and TypeError handling.
  • Introduce helper methods to update list and dict snapshots in place to preserve references and avoid unnecessary allocations during diffs.

Tests:

  • Add a memory churn regression test that ensures object_patch steady-state memory growth remains bounded under repeated diffs.

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.
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

We've reviewed this pull request using the Sourcery rules engine

@cloudflare-workers-and-pages
Copy link

Deploying flet-docs with  Cloudflare Pages  Cloudflare Pages

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

View logs

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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_setattr function 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_snapshot and _update_dict_snapshot to 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.

@FeodorFitsner FeodorFitsner merged commit 67a9763 into main Feb 22, 2026
59 of 108 checks passed
@FeodorFitsner FeodorFitsner deleted the mem-fix-2 branch February 22, 2026 20:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant