Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use a ConcurrentHashMap for BiomeManager #1895

Closed
wants to merge 1 commit into from

Conversation

tahmid-23
Copy link
Contributor

synchronized getByXXX methods make biome loading quite slow when loading instances concurrently

@Dovias
Copy link

Dovias commented Aug 30, 2023

I'm pretty sure fastutil Int2ObjectOpenHashmap should be left there and instead of concurrent maps, to improve this, we should use ReentrantReadWriteLock to improve the concurrent performance when retrieving a biome (which synchronized keyword does not allow to read from multiple threads concurrently). I might be wrong, though.

@Steanky
Copy link
Contributor

Steanky commented Aug 31, 2023

I'm pretty sure fastutil Int2ObjectOpenHashmap should be left there and instead of concurrent maps, to improve this, we should use ReentrantReadWriteLock to improve the concurrent performance when retrieving a biome (which synchronized keyword does not allow to read from multiple threads concurrently). I might be wrong, though.

If I recall correctly, during benchmarking me and @tahmid-23 discovered that a ConcurrentHashMap performs better than an RWL-gated Int2ObjectOpenHashmap, probably due to less overhead (RWLs actually incur a fair bit of overhead when reading, even if they do allow concurrent access).

The best option here, IMO, is to use copy-on-write semantics for the maps. This incurs a high overhead for writes (which are fully synchronized, unlike ConcurrentHashMap), but I think most usage patterns will feature very infrequent writes. Reads have no synchronization overhead and can use a fastutil map -- this makes them quite fast.

You can see such an implementation here.

@tahmid-23
Copy link
Contributor Author

I'm pretty sure fastutil Int2ObjectOpenHashmap should be left there and instead of concurrent maps, to improve this, we should use ReentrantReadWriteLock to improve the concurrent performance when retrieving a biome (which synchronized keyword does not allow to read from multiple threads concurrently). I might be wrong, though.

If I recall correctly, during benchmarking me and @tahmid-23 discovered that a ConcurrentHashMap performs better than an RWL-gated Int2ObjectOpenHashmap, probably due to less overhead (RWLs actually incur a fair bit of overhead when reading, even if they do allow concurrent access).

The best option here, IMO, is to use copy-on-write semantics for the maps. This incurs a high overhead for writes (which are fully synchronized, unlike ConcurrentHashMap), but I think most usage patterns will feature very infrequent writes. Reads have no synchronization overhead and can use a fastutil map -- this makes them quite fast.

You can see such an implementation here.

but its uglier

@mworzala
Copy link
Member

mworzala commented Nov 2, 2023

This will come with #1937 (it was merged into minestom-ce here: hollow-cube#39). I am in agreement with @tahmid-23 that the proposed implementation by @Steanky is super ugly. If you have solid benchmarks and/or use cases where you can demonstrate that the performance difference is relevant, I am interested.

@mworzala mworzala closed this Nov 2, 2023
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.

None yet

4 participants