Skip to content

Every environment report marks the whole tree, so a window drag runs a full resolve and emit on each engine pass #178

Closed

Description

@houko

Context

Tree.SetEnvironment (core/tree.go:711-723) sets the environment's signals in one batch, marks layout when the safe area or the header heights changed, and then calls t.markAll() unconditionally (core/tree.go:722, under the comment "Images pick their scales, screens their headers from it."). markAll sets t.full (core/tree.go:854-857), which commitRound turns into fullRound (core/commit.go:196). resolve then forces every node (core/commit.go:307) and marks it (:330), and emit compares every view (self := n.dirty || t.fullRound, core/commit.go:673). Layout is not redone: relayoutAll runs only on a pixel ratio change (core/commit.go:202-207).

Marking everything on an environment change is documented: "What every node reads (the environment, the layout direction, the font scale, an accessory's ID) marks them all." (docs/architecture/core.md:85). Reporting the window frame through the environment is documented too: "Frame reports the frame SetWindowFrame takes, through the environment, so an app reads it as an Env signal." (docs/architecture/services.md:250).

The cost comes from the trigger being every report, including reports where only fields that no node reads changed:

  • macOS observes NSWindowDidMoveNotification (internal/platform/macos/macos.go:1020), and refresh sends SetEnvironment whenever env.Equal fails (macos.go:1004-1007). WindowFrame is part of that environment (macos.go:965), so every move is a change.
  • A window-controls transition calls refresh() on each frame it moves the traffic lights (internal/platform/macos/desktop.go:148-152), and TitleBar's left edge follows the zoom button's frame (desktop.go:363-369), so each frame is a change.
  • Windows sends SetEnvironment on wmMove whenever the frame changed (internal/platform/windows/windows.go:2402-2405).

In core, windowFrame, maximized, lifecycle and titleBar are set in Env.set (core/env.go:146-149, :173) and read only through their Env accessors, which already track them per field; nothing in resolve or emit reads them. Class variants follow the environment through tracked reads as well (Tree.classEnv, core/classes.go:144-149). The engine merges queued environment messages for the same surface (internal/engine/engine.go:583), so this is one full resolve and emit per engine pass during a drag, not one per move message.

Why it matters

Dragging a window or running a controls transition costs a walk of the whole tree on the engine goroutine every pass, although nothing any view shows changed. That goes against the rule in the same paragraph of core.md:85, "A commit costs what changed, not what the tree is". docs/architecture/performance.md:52 shows the scale of the difference for 300 static rows: 4 µs per marked commit against 572 µs when commits walked the whole tree. That older figure includes layout, which a full round here skips, so a full resolve and emit costs less, but it still grows with the tree instead of with what changed.

Proposal

Keep the documented rule (what every node reads marks them all) and keep WindowFrame, Maximized, Lifecycle and TitleBar as Env signals.

  1. In SetEnvironment, compare the previous report (t.reported, already stored at core/tree.go:712) with the new one, and call markAll only when a field that resolve or emit reads directly changed: the pixel ratio and font scale (which commitRound already checks), the header heights, and whatever image variant selection reads. Keep markLayout for the safe area and headers as today. Fields read only through Env accessors propagate through their signals.
  2. Optionally, carry window frame, maximized and lifecycle changes as messages of their own that set the same signals, so a move never travels the full environment path.

Acceptance

  • A SetEnvironment that changes only WindowFrame, Maximized, Lifecycle or TitleBar commits without a full round; a test asserts that, and a benchmark of window moves over a large tree shows a cost that does not grow with the tree.
  • Changes to pixel ratio, font scale, header heights, dark mode, RTL and the other fields views depend on still update every affected node (the existing environment tests stay green).

Activity

  1. self-assigned this
    on Nov 20, 2026
  2. houko commented on Nov 20, 2026

    @houko
    CollaboratorAuthor

    I implemented the environment diff in PR #1000. Tree.SetEnvironment now keeps WindowFrame, Maximized, Lifecycle, and TitleBar changes on the signal-driven incremental path, while changes to fields the core reads directly still request a full resolve. Regression tests cover both paths. go tool nsgo test -run 'TestWindowEnvironment' -timeout 3m and go tool nsgo check -jobs 2 pass.

    PR: #1000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions