Skip to content

perf: accelerate C906 WebSocket masking - #497

Merged
dormancygrace merged 2 commits into
mainfrom
codex/c906-websocket-mask
Oct 27, 2026
Merged

dormancygrace merged 2 commits into
mainfrom
codex/c906-websocket-mask

Conversation

@dormancygrace

Copy link
Copy Markdown
Owner

Browser-originated WebSocket frames use Gorilla's generic word-at-a-time masking loop. Add a C906 path that keeps 0–7 bytes in Go, handles 8–127 bytes with scalar assembly, and uses XTheadVector from 128 bytes while preserving phase and exact buffer bounds.

The server builder stages the pinned Gorilla v1.5.3 package under its output and uses a private modfile. The shared module cache stays intact; the build verifies the linked assembly and records its source hashes. Runtime checks require XTheadVector, enabled vector state and fast misaligned accesses, with an explicit generic control.

Validation on SG2002/C906, Ethernet and video enabled, three processes with three repetitions and medians:

  • Actual package masking: 21.6–32.2% less CPU when streaming 1400 B–1 MiB buffers; larger gains for repeated cached buffers.
  • In-memory ReadMessage, including allocations and GC: 1.96–9.37% less process CPU for 16 B–256 KiB payloads; every case improved in all three processes. This is not a network/USB throughput or FPS claim.
  • Full Gorilla suite passed on the device in automatic and generic modes, including 104,184 comparison cases, 2,272 guard-page cases, thread stress and a rejected corruption control.
  • Production server cross-build, linked vector-instruction check, host/default and purego Gorilla tests, storage/stream/ws tests, RISC-V vet and staging tests passed.

CI checks the generated instructions, module staging, fallback tests and target test compilation. The test-only CONNECT fixture uses a real tunnel for Go 1.27, and prepared-message tests retain their intended deterministic seeds; no assertions were removed.

Methodology, process medians and qualification hashes are in firmware/cpu/go-websocket-c906/. Memory clearing remains a separate investigation and is not changed by this PR.

@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Build outputs

Compared with the build of main at 5964727.

1 changed, 0 added, 0 removed of 570 outputs.

directory files
changed server server/NanoKVM-Server
Hashes: outputs-sha256 artifact of this run.

@dormancygrace dormancygrace left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

The assembly looks correct.

What I checked:

  • Every th.* WORD decodes to the instruction in its comment, and the generator's --check passes.
  • Key rotation by pos&3 is right, including for pos == 0 and for fragmented payloads.
  • The tail vsetvli never reads past the slice.
  • mask_other.go is the exact complement of the riscv64 build tag.
  • On the host, go test for the module passes in default and purego modes, riscv64 go vet and go test -c are clean, and tests/test-websocket-c906.py passes.

I could not run the device tests (XTheadVector).

The two comments below are about build maintenance.

@dormancygrace dormancygrace left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Reviewed masking dispatch, key rotation, slice bounds, scalar/vector tails and runtime gating, plus the staging/build changes. Fixed the absolute local replacement paths and added a binary reproducibility check covering Gorilla and the Pion forks in two output roots. Also excluded the source-pinned Gorilla fork from automatic version edits. Host/race/purego tests and vet pass; RISC-V tests compile and generated instructions match GNU binutils. The staged server passes storage/stream/ws race tests. The source/cache remain unchanged by preparation. Leaving the PR open because the build-layout correction is substantive; no new device measurements were performed for this revision.

@dormancygrace dormancygrace left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Rechecked the optimized masking dispatch and private build staging on current main, including the newly merged HTTP/browser ISO readers. Staging and two-root byte-for-byte binary reproducibility pass. Gorilla host race/purego tests (with its documented randseednop=0 fixture setting), staged-server storage/stream/ws race tests and RISC-V test compilation pass. Existing review fixes and the final-head CI remain green. No new hardware performance result is claimed.

@dormancygrace
dormancygrace merged commit c3eff76 into main Oct 27, 2026
16 checks passed
@dormancygrace
dormancygrace deleted the codex/c906-websocket-mask branch October 27, 2026 01:16
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