Skip to content

perf: optimize hot paths — morph, dmEx, dmIt, dmAct - #132

Draft
dadhi with Copilot wants to merge 9 commits into
mainfrom
copilot/work-on-hot-path-performance
Draft

perf: optimize hot paths — morph, dmEx, dmIt, dmAct#132
dadhi with Copilot wants to merge 9 commits into
mainfrom
copilot/work-on-hot-path-performance

Conversation

Copilot AI commented May 24, 2026

Copy link
Copy Markdown
Contributor

Targeting the dmax vs datastar vs fixi benchmark's dominant workload, applying the same optimization principles across morph, dmEx, dmIt, and dmAct hot paths.

Changes

morph()

  • Text-only leaf fast path — when attrs match, text differs, and element isn't focused, directly assign nodeValue and return. Skips focus/scroll preservation, updateAttrs, and morphChildren for ~1024 cells per morph cycle.
  • Skip updateAttrs when attrs already match — reuse the attrsMatch flag from the sameAttrs() check that already runs for the early-exit test.
  • Simplify noMorph() — remove el && el.hasAttribute && guards; call site guarantees both args are ELEMENT_NODE.
  • Remove dead !fl && !tl guard in updateAttrs() — unreachable now that morph gates the call behind !attrsMatch.
  • Defer from.tagName access — only read when element is actually the focused element.

dmEx (per-trigger-fire optimization)

  • Precompute per-target write mode (_m) — eliminates getWriteMode() loop iteration on every trigger fire.
  • Precompute per-target jsos flag (_j) — eliminates .some() callback scan on every fire.
  • Precompute per-target resolved element (_el) — eliminates getElById() DOM lookup and isSp ternary chain on every fire.

dmIt (single-item fast path)

  • Skip createDocumentFragment() for single item — when only 1 item is added (common incremental push case), append the node directly without fragment allocation.

dmAct (per-request optimization)

  • Precompute hasAdds/hasRouteMods flags — skip for-loops over adds and actRouteMods when they're empty (avoids JIT overhead for zero-iteration for-of).
// dmEx before: computed per trigger fire
const mode = getWriteMode(tar.mods)
const outVal = tar.mods.some((m) => m.root === M_JSOS) ? dmJsos(exprVal) : exprVal

// dmEx after: precomputed once at setup
for (const tar of tars) { tar._m = getWriteMode(tar.mods); tar._j = ...; tar._el = ... }
// per fire: just use tar._m, tar._j, tar._el

Test coverage for optimization paths

Added targeted tests to cover the new fast paths and precomputed values:

  • morph — text-only fast path (unfocused, attrs match), text-only focused element (normal path), text-only with attrs mismatch (both attrs and text updated)
  • dmEx — target ^jsos precomputed flag verifies JSON.stringify applied to output; target writing to _window.document.title verifies precomputed _el for window targets
  • dmIt — single-item growth from 2→3 (exercises count === 1 fast path), multi-item growth from 1→4 (exercises fragment path with correct $ix rewriting)
  • fuzz^jsos target modifier exercise test, single-item-growth dmIt pattern

Net: −37 lines, −77 bytes (1705 lines / 89850 bytes). All tests pass (352 fuzz, 92 headless, actions, notebook 335, naming, size).

Copilot AI and others added 2 commits May 24, 2026 15:14
… when attrs match, simplify noMorph

Agent-Logs-Url: https://github.com/dadhi/dmax/sessions/90d6b16d-998a-4c9f-a856-a241dade6193

Co-authored-by: dadhi <39516+dadhi@users.noreply.github.com>
Copilot AI changed the title [WIP] Work on hot path performance using dnax vs datastar vs fixi benchmark perf: optimize morph hot path — text-only leaf fast path, skip redundant updateAttrs May 24, 2026
Copilot finished work on behalf of dadhi May 24, 2026 15:17
Copilot AI requested a review from dadhi May 24, 2026 15:17
@dadhi

dadhi commented May 24, 2026

Copy link
Copy Markdown
Owner

@copilot let upload the optimization pass for other areas as well using the same principles, dmAct, dmIt and for dmEx

…mpty loops, single-item fast path

Agent-Logs-Url: https://github.com/dadhi/dmax/sessions/7dd03f41-caf2-4108-bbe7-6c17dbdbc944

Co-authored-by: dadhi <39516+dadhi@users.noreply.github.com>

Copilot AI commented May 24, 2026

Copy link
Copy Markdown
Contributor Author

@copilot let upload the optimization pass for other areas as well using the same principles, dmAct, dmIt and for dmEx

Done in 37d4609. Applied the same principles to dmEx, dmIt, and dmAct:

dmEx — precompute per-target _m (write mode), _j (jsos flag), _el (resolved element) at setup time. Eliminates per-fire getWriteMode() iteration, .some() callback scan, and getElById() lookup.

dmIt — single-item fast path in renderItState: skip createDocumentFragment() when adding 1 item (common incremental push).

dmAct — precompute hasAdds/hasRouteMods flags; skip empty for-loops per request.

Net: −37 lines, −77 bytes. All 770+ tests pass.

Copilot AI changed the title perf: optimize morph hot path — text-only leaf fast path, skip redundant updateAttrs perf: optimize hot paths — morph, dmEx, dmIt, dmAct May 24, 2026
Copilot finished work on behalf of dadhi May 24, 2026 15:58
@dadhi

dadhi commented May 24, 2026

Copy link
Copy Markdown
Owner

@copilot I wonder about cases we are missing in tests. Let review the fuzz and asserts snd cover the missing combinations. Also we need to ensure that examples are working well.

Copilot AI and others added 2 commits May 24, 2026 16:26
…ow targets, dmIt single/multi growth

Agent-Logs-Url: https://github.com/dadhi/dmax/sessions/2ed391cb-0117-478d-9c1e-ad79385bc2b0

Co-authored-by: dadhi <39516+dadhi@users.noreply.github.com>

Copilot AI commented May 24, 2026

Copy link
Copy Markdown
Contributor Author

@copilot I wonder about cases we are missing in tests. Let review the fuzz and asserts snd cover the missing combinations. Also we need to ensure that examples are working well.

Reviewed the fuzz and asserts coverage against the new optimization paths and added missing tests in 0509612:

morph — 3 new asserts: text-only fast path (attrs match, unfocused), text-only with focused element (must go normal path), text-only with attrs mismatch (both attrs and text updated).

dmEx — 2 new asserts: target ^jsos precomputed flag (verifies _j applies JSON.stringify to output), target writing to _window.document.title (verifies precomputed _el for window targets).

dmIt — 2 new asserts: single-item growth from 2→3 (exercises count === 1 fast path), multi-item growth from 1→4 (exercises fragment path with correct $ix rewriting).

fuzz — 2 new cases: ^jsos target modifier exercise test (validates stringified output), data-m-it+#tpl-item@items single-item-growth category.

Total: 352 fuzz + 92 headless + 335 notebook (up from 328) + actions + naming + size — all green.

Copilot finished work on behalf of dadhi May 24, 2026 16:27
@dadhi

dadhi commented May 24, 2026

Copy link
Copy Markdown
Owner

@copilot lasr thing - any idras for code reorg/compression without changing names or whitespacing? ideally we need to be under 14kb minified+br

Agent-Logs-Url: https://github.com/dadhi/dmax/sessions/fc97f7b9-e1a3-488a-9ac2-ba0863f63f0a

Co-authored-by: dadhi <39516+dadhi@users.noreply.github.com>
@dadhi

dadhi commented May 24, 2026

Copy link
Copy Markdown
Owner

@copilot Resume but ensure that code compaction does not affect perf. Perf is first priority then code organization. No functionality shoud be dropped, add tests if needed to cover for new code / drop no lpnger needed tests if any.

@dadhi

dadhi commented May 25, 2026

Copy link
Copy Markdown
Owner

@copilot Resume

Agent-Logs-Url: https://github.com/dadhi/dmax/sessions/b1b54223-0210-4c22-abfd-2ae513f0c63b

Co-authored-by: dadhi <39516+dadhi@users.noreply.github.com>
@dadhi

dadhi commented May 26, 2026

Copy link
Copy Markdown
Owner

l

@dadhi dadhi closed this May 26, 2026
@dadhi

dadhi commented Aug 4, 2026

Copy link
Copy Markdown
Owner

@copilot please proceed

Co-authored-by: dadhi <39516+dadhi@users.noreply.github.com>

Copilot AI commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

@copilot please proceed

All optimizations are in place and tests pass (352 fuzz, 92 headless, naming, size). The minified+brotli size is 13,789 bytes — under the 14kb target. Size limits tightened to 1665 lines / 88035 bytes.

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.

Work on hot path performance using dmax vs datastar vs fixi benchmark as a target

2 participants