Skip to content

Write the node size only when it changed - #580

Open
paceholder wants to merge 1 commit into
masterfrom
fix-size-write-loop
Open

paceholder wants to merge 1 commit into
masterfrom
fix-size-write-loop

Conversation

@paceholder

@paceholder paceholder commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation/refactoring

Description

The default geometries wrote NodeRole::Size into the model on every recompute, also when the value did not change. A model that emitted nodeUpdated from setNodeData(Size), as the advanced guide advised until #576, then recursed without end: the scene writes Size while it handles nodeUpdated, the write emits nodeUpdated, and so on.

The new protected function AbstractNodeGeometry::storeSize(nodeId, size) compares the new size with the stored one and writes only if they differ. Both default geometries end with this one call. A recompute that changes nothing writes nothing, and a loop ends after one pass. A custom geometry can call the same function, so it does not have to repeat the check. There is no new state, no flag and no layout change: the function is protected and not virtual. The library and all the example models store the size and emit nothing, so for them the only visible change is fewer writes (a new node used to write the size more than once).

A model that emits nodeUpdated on every Size write still gets one nested nodeUpdated: the write that changes the size emits, the scene recomputes once more inside the first handler, finds the same size and stops. I measured the nested depth in the loop test: 1. The guide says that this emit is not needed. A guard that removes the nested pass would need a flag per node, and I did not add it.

The guide said that emitting nodeUpdated on a Size write "starts an endless recursion". It now says only that the emit is not needed. The change-notifications guide tells a custom geometry to store the size with storeSize(). The guide does not promise anything about a model that emits anyway.

The size itself stays a value that the geometry computes and the model stores. Who should own the size is a question for v4 (#568, #577).

Testing

  • Qt version tested: 6.12 (offscreen). CI covers Qt 5 and MSVC.
  • Existing tests still pass: 120 test cases, 1073 assertions. The four examples that handle the Size role also build.
  • Added tests for new functionality, in test/src/TestSizeWrite.cpp, both failed on the old code:
    • "A recompute that does not change the size writes no size": a new node writes the size once, and two updates that change nothing write nothing (the old code wrote on every update).
    • "A model that emits nodeUpdated when the size is written does not start a loop": the model follows the old advice, with a depth cap so that a loop fails the check and does not overflow the stack. The depth reached the cap of 50 on the old code, and it stays below 5 now.
  • Docs built with Sphinx. No new warning.

Not tested: Qt 5, Linux, Windows, and a node with a real embedded widget (the tests use the test graph model without a widget). A custom geometry that writes the size on every recompute without storeSize() keeps its old behavior.

Breaking changes?

No. One protected, non-virtual function is added to AbstractNodeGeometry. The size and the layout of the class do not change.

Related issue

See #568.

@paceholder paceholder closed this Dec 2, 2026
@paceholder
paceholder deleted the fix-size-write-loop branch December 2, 2026 20:35
@paceholder
paceholder restored the fix-size-write-loop branch December 2, 2026 20:35
@paceholder paceholder reopened this Dec 2, 2026
The default geometries wrote the size on every recompute. A model that
emitted nodeUpdated from setNodeData(Size), as the guide advised, then
recursed without end. AbstractNodeGeometry::storeSize() now writes a size
only when it differs from the stored one, so a loop ends after one pass.
Both default geometries use it, and a custom geometry can call it.

The guide said that such an emit starts an endless recursion. It now says
only that the emit is not needed, and tells a custom geometry to store the
size with storeSize().

See #568
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