Skip to content

perf: stop pinning a first commit's item buffer, and one style per node - #7

Merged
oneo-me merged 3 commits into
nsgo-dev:mainfrom
Heyana:perf/node-alloc
Oct 26, 2026
Merged

oneo-me merged 3 commits into
nsgo-dev:mainfrom
Heyana:perf/node-alloc

Conversation

@Heyana

@Heyana Heyana commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

Two allocations that a moving screen pays once and then keeps.

1. The first commit's item buffer stays the tree's capacity. Tree.items
is the queue a commit builds. The first commit sends every view, so the
slice grows to the whole tree — 14 892 items × 368 B for a 400-row table —
while a steady commit queues tens of items. It is reset with [:0], which
keeps the capacity, so the peak is pinned for the life of the tree. Give it
back once the queue is small again, with a floor and a ratio guard so it
does not thrash.

2. node.dyn held a Style for every node. It stored a full Style
(976 B) even for nodes without a StyleFunc, and its render effect
allocated a fresh one on every call. Keep a pointer, allocate it once, and
update it in place: unsafe.Sizeof(node) drops 5696 → 4728 B.

Neither changes the public API nor what a commit sends.

Numbers

macOS 26 (Apple M6, go1.27.1, 144 Hz). Same bench source (bench/quotes),
both binaries running at the same time so they see the same machine; CPU
from ps -o cputime, memory from footprint.

before after
40 rows, 560×1040 56.4 MB 55.6 MB
200 rows in a core.Scroll 106.2 MB 91.3 MB (−14%)
200 rows, CPU 860.8 ms/s 865.8 ms/s
frames 144 144

The 40-row case barely moves, as expected: it is dominated by the macOS
base and the window's backing store. The buffer fix scales with the tree,
which is where it matters.

Tests

go test ./core — 147 passing, 0.5 s.

Two new tests, each verified failing before the fix:

  • TestCommitBufferDropsTheFirstCommitPeak — reports
    the commit buffer kept its peak: cap 14892 -> 14892.
  • TestStyleFuncKeepsOneStyle — asserts one allocation per node across
    renders, and that a node without a StyleFunc keeps none.

I did not run the whole suite locally; go test ./... takes about ten
minutes here (examples/cmd), so only ./core was run.

Each node held a Style (976 B) for its StyleFunc result even when it had
none, and the render effect allocated a fresh one on every call. Keep a
pointer, allocate it once, and update it in place: node drops 5696 -> 4728
bytes, and a table of 200 rows saves ~5 MB.
The first commit sends every view, so its scratch slice grew to the whole
tree's worth of items (38k x 368 B for a 200-row table) and stayed the
tree's capacity for good, while a steady commit sends tens. Release it
once the queue is small again, keeping a floor so it does not thrash.
Both are memory regressions that no assertion of the rendered output
would catch:
- a first commit's peak stays the tree's item capacity (cap 14892 here),
- a StyleFunc allocates a 976 B Style per render effect when the node
  could keep one.

Verified as failing before the fixes: the buffer test reports
cap 14892 -> 14892, the style test does not compile against the old
value field.
@oneo-me

oneo-me commented Oct 26, 2026

Copy link
Copy Markdown
Collaborator

Thanks! Both fixes hold up, so I'm merging this.

  • Rebase: I rebased it onto main (6a6e094). Main had changed restyle into t.restyle(n), which now reports whether layout changed, and added the part states. I kept both and put the pointer change on top. go vet ./... and go test ./... pass.
  • Commit buffer: confirmed. Your test fails on main (cap 14684 -> 14684) and passes with the fix.
  • StyleFunc style: the memory saving is real; node goes from 6240 to 5224 B on current main. One correction: the old value field did not allocate on each render, because n.dyn = fn() just copies into the node. testing.AllocsPerRun shows 2 allocations per signal update both before and after. So the saving comes from nodes without a StyleFunc being smaller, and the pointer check in TestStyleFuncKeepsOneStyle guards the in-place update, not an allocation.
  • Possible follow-up: Merge with a zero Style returns the base unchanged. So restyle could start from n.base and merge only when n.dyn is set. Nodes without a StyleFunc would then skip both the 1 KB copy and the Merge call.

@oneo-me
oneo-me merged commit 323a48c into nsgo-dev:main Oct 26, 2026
@Heyana
Heyana deleted the perf/node-alloc branch October 28, 2026 09:17
togotop pushed a commit to togotop/nsgo that referenced this pull request Nov 16, 2026
A Windows download that carries no signature, or one from a certificate
SmartScreen does not know, warns the user before it opens. MyGo signs
Windows packages; NSGo wrote them unsigned, and Windows code signing was
a roadmap gap.

Add it here without the Windows SDK, so a Mac signs what it builds:

- internal package cmd/nsgo/authenticode signs a PE image: it parses the
  headers, computes the Authenticode digest (headers without the checksum
  and certificate table entry, every section in file order, the bytes
  between and after them, a short last block padded to 8 bytes), writes a
  WIN_CERTIFICATE holding a PKCS nsgo-dev#7 SignedData over SpcIndirectDataContent
  and recomputes the PE checksum. A re-signed image replaces the old
  signature rather than appending a second. The digest covers
  SpcIndirectDataContent's value, not its SEQUENCE header, as Authenticode
  readers check it.
- sign.certificate (a .pfx, NSGO_CERTIFICATE_PASSWORD) signs the
  executable, the command companion, the resources' PE images that carry
  no signature, and the installer. sign.sign_command signs them with a
  tool of its own, %1 standing for the path, for a hardware token or a
  cloud service. sign.timestamp_url, DigiCert's unless "none", asks an
  RFC 3161 server for a token, which the signer carries as its unsigned
  attribute so the signature outlives the certificate.
- The installer's uninstaller comes from makensis, so it signs through
  NSIS's !uninstfinalize running `nsgo sign-uninstaller` with the
  certificate in the environment; that needs NSIS 3.08, and without it
  the uninstaller stays unsigned.
- -certificate, -sign-command and -timestamp-url replace the three from
  the command line. build refuses two ways of signing at once, a
  certificate on a build that names no Windows target, and it refuses a
  certificate outside a release build.

Verified: the package's tests, including osslsigncode's verification of
an image signed here, and a real build of examples/hello whose exe, setup
and uninstaller each verify with osslsigncode.

The MSIX stays unsigned: AppxSignature.p7x is a package signature of its
own, recorded in the roadmap.
togotop pushed a commit to togotop/nsgo that referenced this pull request Nov 16, 2026
A Windows download that carries no signature, or one from a certificate
SmartScreen does not know, warns the user before it opens. MyGo signs
Windows packages; NSGo wrote them unsigned, and Windows code signing was
a roadmap gap.

Add it here without the Windows SDK, so a Mac signs what it builds:

- internal package cmd/nsgo/authenticode signs a PE image: it parses the
  headers, computes the Authenticode digest (headers without the checksum
  and certificate table entry, every section in file order, the bytes
  between and after them, a short last block padded to 8 bytes), writes a
  WIN_CERTIFICATE holding a PKCS nsgo-dev#7 SignedData over SpcIndirectDataContent
  and recomputes the PE checksum. A re-signed image replaces the old
  signature rather than appending a second. The digest covers
  SpcIndirectDataContent's value, not its SEQUENCE header, as Authenticode
  readers check it.
- sign.certificate (a .pfx, NSGO_CERTIFICATE_PASSWORD) signs the
  executable, the command companion, the resources' PE images that carry
  no signature, and the installer. sign.sign_command signs them with a
  tool of its own, %1 standing for the path, for a hardware token or a
  cloud service. sign.timestamp_url, DigiCert's unless "none", asks an
  RFC 3161 server for a token, which the signer carries as its unsigned
  attribute so the signature outlives the certificate.
- The installer's uninstaller comes from makensis, so it signs through
  NSIS's !uninstfinalize running `nsgo sign-uninstaller` with the
  certificate in the environment; that needs NSIS 3.08, and without it
  the uninstaller stays unsigned.
- -certificate, -sign-command and -timestamp-url replace the three from
  the command line. build refuses two ways of signing at once, a
  certificate on a build that names no Windows target, and it refuses a
  certificate outside a release build.

Verified: the package's tests, including osslsigncode's verification of
an image signed here, and a real build of examples/hello whose exe, setup
and uninstaller each verify with osslsigncode.

The MSIX stays unsigned: AppxSignature.p7x is a package signature of its
own, recorded in the roadmap.
houko pushed a commit to togotop/nsgo that referenced this pull request Nov 16, 2026
A Windows download that carries no signature, or one from a certificate
SmartScreen does not know, warns the user before it opens. MyGo signs
Windows packages; NSGo wrote them unsigned, and Windows code signing was
a roadmap gap.

Add it here without the Windows SDK, so a Mac signs what it builds:

- internal package cmd/nsgo/authenticode signs a PE image: it parses the
  headers, computes the Authenticode digest (headers without the checksum
  and certificate table entry, every section in file order, the bytes
  between and after them, a short last block padded to 8 bytes), writes a
  WIN_CERTIFICATE holding a PKCS nsgo-dev#7 SignedData over SpcIndirectDataContent
  and recomputes the PE checksum. A re-signed image replaces the old
  signature rather than appending a second. The digest covers
  SpcIndirectDataContent's value, not its SEQUENCE header, as Authenticode
  readers check it.
- sign.certificate (a .pfx, NSGO_CERTIFICATE_PASSWORD) signs the
  executable, the command companion, the resources' PE images that carry
  no signature, and the installer. sign.sign_command signs them with a
  tool of its own, %1 standing for the path, for a hardware token or a
  cloud service. sign.timestamp_url, DigiCert's unless "none", asks an
  RFC 3161 server for a token, which the signer carries as its unsigned
  attribute so the signature outlives the certificate.
- The installer's uninstaller comes from makensis, so it signs through
  NSIS's !uninstfinalize running `nsgo sign-uninstaller` with the
  certificate in the environment; that needs NSIS 3.08, and without it
  the uninstaller stays unsigned.
- -certificate, -sign-command and -timestamp-url replace the three from
  the command line. build refuses two ways of signing at once, a
  certificate on a build that names no Windows target, and it refuses a
  certificate outside a release build.

Verified: the package's tests, including osslsigncode's verification of
an image signed here, and a real build of examples/hello whose exe, setup
and uninstaller each verify with osslsigncode.

The MSIX stays unsigned: AppxSignature.p7x is a package signature of its
own, recorded in the roadmap.
houko pushed a commit that referenced this pull request Nov 16, 2026
A Windows download that carries no signature, or one from a certificate
SmartScreen does not know, warns the user before it opens. MyGo signs
Windows packages; NSGo wrote them unsigned, and Windows code signing was
a roadmap gap.

Add it here without the Windows SDK, so a Mac signs what it builds:

- internal package cmd/nsgo/authenticode signs a PE image: it parses the
  headers, computes the Authenticode digest (headers without the checksum
  and certificate table entry, every section in file order, the bytes
  between and after them, a short last block padded to 8 bytes), writes a
  WIN_CERTIFICATE holding a PKCS #7 SignedData over SpcIndirectDataContent
  and recomputes the PE checksum. A re-signed image replaces the old
  signature rather than appending a second. The digest covers
  SpcIndirectDataContent's value, not its SEQUENCE header, as Authenticode
  readers check it.
- sign.certificate (a .pfx, NSGO_CERTIFICATE_PASSWORD) signs the
  executable, the command companion, the resources' PE images that carry
  no signature, and the installer. sign.sign_command signs them with a
  tool of its own, %1 standing for the path, for a hardware token or a
  cloud service. sign.timestamp_url, DigiCert's unless "none", asks an
  RFC 3161 server for a token, which the signer carries as its unsigned
  attribute so the signature outlives the certificate.
- The installer's uninstaller comes from makensis, so it signs through
  NSIS's !uninstfinalize running `nsgo sign-uninstaller` with the
  certificate in the environment; that needs NSIS 3.08, and without it
  the uninstaller stays unsigned.
- -certificate, -sign-command and -timestamp-url replace the three from
  the command line. build refuses two ways of signing at once, a
  certificate on a build that names no Windows target, and it refuses a
  certificate outside a release build.

Verified: the package's tests, including osslsigncode's verification of
an image signed here, and a real build of examples/hello whose exe, setup
and uninstaller each verify with osslsigncode.

The MSIX stays unsigned: AppxSignature.p7x is a package signature of its
own, recorded in the roadmap.
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.

2 participants