Skip to content

Reduce Metal backend memory use - #94

Merged
elcritch merged 1 commit into
mainfrom
optimize-metal-memory
Sep 23, 2026
Merged

elcritch merged 1 commit into
mainfrom
optimize-metal-memory

Conversation

@elcritch

Copy link
Copy Markdown
Owner

Summary

  • Allocate backdrop textures and CPU rect-mask arrays only when needed. Release backdrop textures after three consecutive frames without blur, and release mismatched optional textures on resize.
  • Drop the CPU copy of static indices after uploading them to Metal. Read screenshots directly into the Pixie image buffer and swap BGRA channels in place.
  • Add a Metal regression test and a reproducible memory benchmark.

Memory profile

Release ARC build on Apple M3 Pro, macOS 15.7.9. Three runs per scenario; values are medians. Workload: 1024×768 frame, 1024² atlas, 1024 max quads.

Scenario Before After
Plain frame, Metal allocated memory 11.28 MiB 8.25 MiB
Three frames after blur stops, Metal allocated memory 14.31 MiB 8.25 MiB
Full screenshot, peak process RSS 22.66 MiB 19.47 MiB

An active blur frame remains at 14.31 MiB in both versions. The benchmark uses MTLDevice.currentAllocatedSize and /usr/bin/time -l; its source is in tests/bench_metal_memory.nim.

Tests

  • atlas-run tests — 46/46 passed.
  • Metal regression covers lazy allocation, idle release, resize/recreation, and screenshot channel order.

Threading and GC

No thread or GC configuration changes. The three-frame idle window matches the existing maximum number of in-flight Metal frames.

@elcritch
elcritch merged commit ec4049a into main Sep 23, 2026
8 checks passed
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