Skip to content

Optimize GenTerra - #47

Open
tehtelev wants to merge 1 commit into
anegostudios:masterfrom
tehtelev:patch-8
Open

Optimize GenTerra#47
tehtelev wants to merge 1 commit into
anegostudios:masterfrom
tehtelev:patch-8

Conversation

@tehtelev

@tehtelev tehtelev commented Jul 1, 2026

Copy link
Copy Markdown

Summary

Multithreaded terrain generation optimization: False Sharing fix

  • Added thread-local buffers for layerFullySolid and layerFullyEmpty. Added LocalLayerFullySolid and LocalLayerFullyEmpty arrays to the ThreadLocalTempData struct; these are initialized once per thread and reused.

  • Reworked Parallel.For with explicit local state. Instead of directly writing to shared global arrays within the loop body, each thread now works with its own local copy. Overloads with localInit (initialization of local buffers) and localFinally (merging results) are used.

  • Thread-safe merging under lock. In localFinally, results from local buffers are applied to global arrays using a lock, ensuring correct merging of "not completely full/empty" flags.

  • Initializing Arrays with Array.Fill. Replacing Boolean array filling loops with Array.Fill improves readability and slightly reduces overhead.

Performance numbers

Testing on 10,200 generation column chunks:

  • Before: total cycle execution time 170...228 sec
  • After: total cycle execution time 138...143 sec
  • Average execution time of GenTerra.generate: decreased from 24...29 seconds to 20...21 seconds.

Before
2026-07-01_16-19-33
2026-07-01_16-20-58
2026-07-01_16-21-25

After
2026-07-01_15-36-29
2026-07-01_15-37-42
2026-07-01_15-38-57

@tehtelev

tehtelev commented Jul 1, 2026

Copy link
Copy Markdown
Author

Also sent to Stratum

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