Skip to content

Stop locking every RegionImage pixel write - #616

Merged
jpenilla merged 1 commit into
masterfrom
experiment/setpixel
Oct 16, 2026
Merged

jpenilla merged 1 commit into
masterfrom
experiment/setpixel

Conversation

@jpenilla

Copy link
Copy Markdown
Owner

RegionImage.setPixel was synchronized, so every pixel write from every scan worker took the same lock. The lock is now only taken to allocate the pixel array, using double-checked locking on a volatile field, and pixel writes are unsynchronized.

Two things make unsynchronized writes safe. Each chunk is scanned by only one worker, so no two workers write the same pixel. Saving only starts after every scan for the region has finished, which guarantees the save sees all the writes. Rendered tiles are byte-identical to master.

Performance

Render time and throughput, master vs this PR

Sampled render worker time and worker utilization, master vs this PR

Scan workers write distinct pixels of a region image, and saves only
start after every scan for the region has finished, so the lock in
setPixel was only needed to allocate the pixel array. Allocate it
with double-checked locking on a volatile field instead, and leave
pixel writes unsynchronized.
@jpenilla
jpenilla merged commit af22fd3 into master Oct 16, 2026
2 checks passed
@jpenilla
jpenilla deleted the experiment/setpixel branch October 16, 2026 06:27
jpenilla added a commit that referenced this pull request Oct 16, 2026
Scan workers write distinct pixels of a region image, and saves only
start after every scan for the region has finished, so the lock in
setPixel was only needed to allocate the pixel array. Allocate it
with double-checked locking on a volatile field instead, and leave
pixel writes unsynchronized.
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