Skip to content

Fix multi-model performance review findings#19

Draft
mrecachinas wants to merge 5 commits intospectriclabs:mainfrom
mrecachinas:fix/performance-review-findings
Draft

Fix multi-model performance review findings#19
mrecachinas wants to merge 5 commits intospectriclabs:mainfrom
mrecachinas:fix/performance-review-findings

Conversation

@mrecachinas
Copy link

@mrecachinas mrecachinas commented Mar 10, 2026

Performance optimizations from multi-model review

Addresses 17 findings from a multi-model performance review across the rendering hot paths.

Allocation reduction:

  • Pool pixx/pixy trace buffers on Mx object (grow-only)
  • Guard SDS 1D buffer allocation with size check
  • Pre-allocate typed arrays for highlight fill
  • Cache ImageData objects, reuse when dimensions match

Hot-path improvements:

  • Remove console.log from mx.trace() fill path
  • Hoist Math.exp(-decay) outside vmovmax loop
  • Pass existing views instead of creating new PointArray wrappers; use subarray() over slice()
  • Hoist clipping object and use shared DASHED_STYLE constant

Algorithmic improvements:

  • Replace zbuf slice/push with in-place copyWithin+set for 2D scrolling
  • Cache prep() bounds for reuse in get_pan_bounds()
  • Nested y/x loops in scaleImage and create_image (eliminates per-pixel modulo/division)

Caching & event dispatch:

  • Replace deprecated createEvent/initEvent with CustomEvent
  • Cache measureText results for legend labels
  • getCachedCtx() helper via WeakMap to avoid repeated getContext("2d")

Level-of-detail rendering:

  • Min-max decimation in Layer1D.draw() — reduces points to ≤2×screenWidth when data far exceeds pixel resolution, preserving peaks/valleys. Controlled by layer.decimate (default: true).

6 files changed (m.js, mx.js, sigplot.js, sigplot.layer1d.js, sigplot.layer1dSDS.js, sigplot.layer2d.js).

mrecachinas and others added 5 commits March 8, 2026 13:31
- F3: Remove console.log from mx.trace() highlight fill hot path
- F4: Hoist Math.exp(-decay) computation outside vmovmax loop
- F8: Pass existing xpoint/ypoint views instead of creating new PointArray wrappers
- F8: Use subarray() instead of slice() for maxhold data (avoids copy)
- F9: Hoist clipping object {tL, tE} outside the per-point loop
- F15: Use shared DASHED_STYLE constant instead of per-call allocation

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- F1: Pool pixx/pixy Int32Array buffers on Mx object in mx.trace(), grow-only
- F5: Guard SDS 1D buffer allocation with size check to avoid per-frame alloc
- F10: Pre-allocate typed arrays for highlight fill instead of push()-based growth
- F13: Cache ImageData objects in both renderImage paths, reuse when dimensions match

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- F7: Replace deprecated createEvent/initEvent with CustomEvent; add suppress_lyrdraw opt-out flag
- F14: Cache measureText results for legend labels, invalidated on layer name changes
- F16: Add getCachedCtx() helper using WeakMap to avoid repeated getContext('2d') lookups

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- F2: Replace zbuf slice/push churn with in-place copyWithin+set for 2D scrolling modes
- F6: Cache prep() bounds from draw() for reuse in get_pan_bounds(), avoiding double computation
- F11: Replace flat loop with nested y/x loops in scaleImage, eliminating modulo/division per pixel
- F12: Hoist origin/direction checks outside pixel loop in create_image, use nested loops

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Implement min-max decimation in Layer1D.draw() that reduces point count
to at most 2*screenWidth when data significantly exceeds pixel resolution.
Preserves visual peaks/valleys while skipping redundant sub-pixel points.

- Only activates for line-only rendering (not symbols) when pts > 2*screenWidth
- Full data still used for bounds calculation, maxhold, and pan operations
- Controlled by layer.decimate flag (defaults to true)
- Reuses decimation buffers across frames to minimize GC pressure

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@mrecachinas mrecachinas changed the title Fix multi-model review findings Fix multi-model performance review findings Mar 10, 2026
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