Repository navigation
Description
I have observed that the rendering with Parameters.NUMBER_OF_THREADS=1 works in my case much faster that the default option with Parameters.NUMBER_OF_THREADS=Runtime.getRuntime().availableProcessors() + 1. Hillshading is switched of for these tests.
To be able to compare things I added in the DatabaseRenderer.executeJob at the beginning and the end one line with a log statement.
As a test I just started the app with the germany map, with an initial Position: latLong=latitude=49.180251, longitude=8.430373, zoomLevel=9.
With multithreading (Parameters.NUMBER_OF_THREADS=9) the result is (where d is the duration of the job in ms):
2024-12-27 12:57:40.449 7107-7227 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-1 tileX=269 tileY=176
2024-12-27 12:57:40.450 7107-7228 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-2 tileX=267 tileY=175
2024-12-27 12:57:40.451 7107-7229 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-3 tileX=268 tileY=175
2024-12-27 12:57:40.452 7107-7230 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-4 tileX=267 tileY=174
2024-12-27 12:57:40.452 7107-7231 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-5 tileX=268 tileY=174
2024-12-27 12:57:40.452 7107-7232 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-6 tileX=267 tileY=176
2024-12-27 12:57:40.452 7107-7234 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-7 tileX=268 tileY=176
2024-12-27 12:57:40.452 7107-7235 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-8 tileX=266 tileY=175
2024-12-27 12:57:40.453 7107-7237 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-9 tileX=269 tileY=175
2024-12-27 12:57:54.992 7107-7232 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-6 tileX=267 tileY=176 d=14540
2024-12-27 12:57:54.993 7107-7232 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-6 tileX=266 tileY=174
2024-12-27 12:57:55.000 7107-7235 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-8 tileX=266 tileY=175 d=14548
2024-12-27 12:57:55.001 7107-7235 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-8 tileX=269 tileY=174
2024-12-27 12:57:55.215 7107-7237 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-9 tileX=269 tileY=175 d=14762
2024-12-27 12:57:55.215 7107-7237 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-9 tileX=266 tileY=176
2024-12-27 12:57:55.241 7107-7230 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-4 tileX=267 tileY=174 d=14790
2024-12-27 12:57:55.241 7107-7230 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-4 tileX=267 tileY=173
2024-12-27 12:57:55.382 7107-7234 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-7 tileX=268 tileY=176 d=14930
2024-12-27 12:57:55.382 7107-7234 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-7 tileX=268 tileY=173
2024-12-27 12:57:55.592 7107-7228 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-2 tileX=267 tileY=175 d=15142
2024-12-27 12:57:55.593 7107-7228 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-2 tileX=266 tileY=173
2024-12-27 12:57:56.348 7107-7227 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-1 tileX=269 tileY=176 d=15899
2024-12-27 12:57:56.349 7107-7227 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-1 tileX=269 tileY=173
2024-12-27 12:57:56.675 7107-7229 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-3 tileX=268 tileY=175 d=16224
2024-12-27 12:57:56.781 7107-7231 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-5 tileX=268 tileY=174 d=16329
2024-12-27 12:57:57.052 7107-7232 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-6 tileX=266 tileY=174 d=2058
2024-12-27 12:57:57.121 7107-7235 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-8 tileX=269 tileY=174 d=2120
2024-12-27 12:57:58.112 7107-7237 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-9 tileX=266 tileY=176 d=2897
2024-12-27 12:57:58.223 7107-7228 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-2 tileX=266 tileY=173 d=2630
2024-12-27 12:57:58.344 7107-7227 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-1 tileX=269 tileY=173 d=1995
2024-12-27 12:57:58.514 7107-7230 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-4 tileX=267 tileY=173 d=3273
2024-12-27 12:57:58.543 7107-7234 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-7 tileX=268 tileY=173 d=3161
Exactly same scenario, same position, but with Parameters.NUMBER_OF_THREADS=1:
2024-12-27 12:59:21.155 7467-7524 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-1 tileX=269 tileY=176
2024-12-27 12:59:21.770 7467-7524 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-1 tileX=269 tileY=176 d=614
2024-12-27 12:59:21.771 7467-7524 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-1 tileX=267 tileY=175
2024-12-27 12:59:22.120 7467-7524 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-1 tileX=267 tileY=175 d=349
2024-12-27 12:59:22.121 7467-7524 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-1 tileX=268 tileY=175
2024-12-27 12:59:22.404 7467-7524 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-1 tileX=268 tileY=175 d=283
2024-12-27 12:59:22.404 7467-7524 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-1 tileX=267 tileY=174
2024-12-27 12:59:22.615 7467-7524 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-1 tileX=267 tileY=174 d=211
2024-12-27 12:59:22.615 7467-7524 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-1 tileX=268 tileY=174
2024-12-27 12:59:22.901 7467-7524 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-1 tileX=268 tileY=174 d=286
2024-12-27 12:59:22.901 7467-7524 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-1 tileX=267 tileY=176
2024-12-27 12:59:23.119 7467-7524 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-1 tileX=267 tileY=176 d=218
2024-12-27 12:59:23.119 7467-7524 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-1 tileX=268 tileY=176
2024-12-27 12:59:23.355 7467-7524 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-1 tileX=268 tileY=176 d=236
2024-12-27 12:59:23.356 7467-7524 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-1 tileX=266 tileY=175
2024-12-27 12:59:23.572 7467-7524 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-1 tileX=266 tileY=175 d=215
2024-12-27 12:59:23.572 7467-7524 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-1 tileX=269 tileY=175
2024-12-27 12:59:23.801 7467-7524 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-1 tileX=269 tileY=175 d=229
2024-12-27 12:59:23.802 7467-7524 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-1 tileX=266 tileY=174
2024-12-27 12:59:23.976 7467-7524 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-1 tileX=266 tileY=174 d=174
2024-12-27 12:59:23.977 7467-7524 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-1 tileX=269 tileY=174
2024-12-27 12:59:24.151 7467-7524 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-1 tileX=269 tileY=174 d=174
2024-12-27 12:59:24.151 7467-7524 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-1 tileX=266 tileY=176
2024-12-27 12:59:24.334 7467-7524 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-1 tileX=266 tileY=176 d=183
2024-12-27 12:59:24.335 7467-7524 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-1 tileX=267 tileY=173
2024-12-27 12:59:24.607 7467-7524 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-1 tileX=267 tileY=173 d=272
2024-12-27 12:59:24.608 7467-7524 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-1 tileX=268 tileY=173
2024-12-27 12:59:24.910 7467-7524 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-1 tileX=268 tileY=173 d=302
2024-12-27 12:59:24.911 7467-7524 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-1 tileX=266 tileY=173
2024-12-27 12:59:25.105 7467-7524 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-1 tileX=266 tileY=173 d=195
2024-12-27 12:59:25.106 7467-7524 DatabaseRenderer mg.mgmap I Thread Job started: pool-5-thread-1 tileX=269 tileY=173
2024-12-27 12:59:25.268 7467-7524 DatabaseRenderer mg.mgmap I Thread Job finished: pool-5-thread-1 tileX=269 tileY=173 d=162
From my perspective the synchronized methods matchWay and MatchNode slow down the rendering in multi thread environment.
Activity
devemux86 commentedon Dec 27, 2024
This is strange, we have never seen multi threading slower than 1 thread.
Have you checked with Mapsforge samples app?
Can you display some videos where the difference appears visually?
devemux86 commentedon Dec 27, 2024
Note that you can set the threads parameter as you wish depending on the available processor cores.
The default is just a suggestion.
mg4gh commentedon Dec 27, 2024
This video shows the app startup with 9 threads - it takes 14 seconds until the first tile is rendered:
https://github.com/user-attachments/assets/46215037-25d6-41be-84b4-76f8a1057a97
This second video shows the same with 1 thread - after about 1 second the first tile is rendered:
https://github.com/user-attachments/assets/838d0b9f-e0da-4d47-801f-bdcf4e2e2a83
mg4gh commentedon Dec 27, 2024
Sublimis commentedon Dec 27, 2024
Will issue a PR for this shortly so we all can do testing. Seems like synchronizations in RenderTheme are unnecessarily broad.
mg4gh commentedon Dec 27, 2024
Exactly, I think it is sufficient to have the maps in RenderTheme synchronized. This speeds up drastically.
devemux86 commentedon Dec 27, 2024
General note:
Everyone please make the tests with Mapsforge samples, so that we can reproduce them.
Sublimis commentedon Dec 27, 2024
Please test with this #1599
devemux86 commentedon Dec 27, 2024
I merged the pull request in branch issue_1599 for testing.
@mg4gh Can you test it?
mg4gh commentedon Dec 27, 2024
If tested the changes from @Sublimis - and the result looks good.
Concerning the implementation
I wonder, whether it would be sufficient to synchronize here the get access to the wayMatchingCache . If so, then it would be sufficient to use Collections.synchronizedMap when creating the cache.
Sublimis commentedon Dec 27, 2024
If the solution from #1599 works properly, then the
Collections.synchronizedMapshould also work properly. Will update the PR now so you can test.Sublimis commentedon Dec 27, 2024
However, perhaps we should pay more attention to the renderCallback which now loses some of the synchronization in these cases, perhaps this could be problematic?
devemux86 commentedon Dec 27, 2024
@Sublimis Do you mean with the current pull request implementation?
Or the suggestion above?
devemux86 commentedon Dec 27, 2024
I try to reproduce the reported difference by @mg4gh, but without success.
(even with the
masterbranch)@Sublimis What is your experience with Mapsforge multithreaded rendering?
Do you use it and how? Do you see such difference?
Sublimis commentedon Dec 27, 2024
For both. When you look at the code for
matchWay()above,renderContextandwayparameters are okay, they are instances perRendererJobso do not need to be synchronized. But therenderCallbackis usually aStandardRendererimplementing theRenderCallbackinterface, without synchronization.This doesn't seem to need synchronization either. However, can you 100% confirm such a claim?
37 remaining items
mg4gh commentedon Dec 29, 2024
Now making matchNode and matchWay again synchronized I got this result (filtered for DatabaseRenderer and MapWorkerPool):
1599_multi_threading_synchronized_methods.log
mg4gh commentedon Dec 29, 2024
So bottom line there is a huge difference between the old and the current implementation.
mg4gh commentedon Dec 29, 2024
Even if two threads are iterate on the same RenderInstruction Array - this is not necessarily a problem. Depends on what they do inside the renderNode (or renderWay) method.
Sublimis commentedon Dec 29, 2024
Ok, some cacophony here. This is exactly why we synchronized all
RenderInstruction-s, please see the code in #1599 .mg4gh commentedon Dec 29, 2024
Ok, that's fine for me.
Still the question about the RenderContext - from my perspective ArryList of label and inner way lists need also to be synchronized.
Sublimis commentedon Dec 29, 2024
Maybe, but they've never been synchronized before, and we also didn't do so when we reviewed this recently, because every job/thread gets its own
RenderContextso no concurrency:mapsforge/mapsforge-map/src/main/java/org/mapsforge/map/layer/renderer/DatabaseRenderer.java
Lines 81 to 91 in 30c514a
mg4gh commentedon Dec 29, 2024
ok, if each Thread has its own context, then this seem not necessary.
devemux86 commentedon Dec 30, 2024
This should be fixed by #1599.
devemux86 commentedon Dec 30, 2024
@Sublimis
@mg4gh probably means the
Parameters.NUMBER_OF_THREADSvalue.devemux86 commentedon Dec 30, 2024
@mg4gh After merging pull request #1599, have you compared single and multi threading?
Which one works best in your tests?
mg4gh commentedon Dec 30, 2024
I have compared Parameters.NUMBER_OF_THREADS = 1 (single thread) with Parameters.NUMBER_OF_THREADS = 9 (multi threaded) on a device with 8 processors. I did not compare with other values.
devemux86 commentedon Dec 30, 2024
@mg4gh You mentioned above these results about the improvement in multi threading.
How is single thread now in your tests compared to multi threads?