Skip to content

Slow rendering with multi threading #1598

Closed

Description

@mg4gh

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

  1. devemux86 commented on Dec 27, 2024

    @devemux86
    Collaborator

    This is strange, we have never seen multi threading slower than 1 thread.
    Have you checked with Mapsforge samples app?

    From my perspective the synchronized methods matchWay and MatchNode slow down the rendering in multi thread environment.

    Can you display some videos where the difference appears visually?

  2. devemux86 commented on Dec 27, 2024

    @devemux86
    Collaborator

    Note that you can set the threads parameter as you wish depending on the available processor cores.
    The default is just a suggestion.

  3. mg4gh commented on Dec 27, 2024

    @mg4gh
    ContributorAuthor

    This is strange, we have never seen multi threading slower than 1 thread. Have you checked with Mapsforge samples app?

    From my perspective the synchronized methods matchWay and MatchNode slow down the rendering in multi thread environment.

    Can you display some videos where the difference appears visually?

    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

  4. mg4gh commented on Dec 27, 2024

    @mg4gh
    ContributorAuthor

    Note that you can set the threads parameter as you wish depending on the available processor cores. The default is just a suggestion.
    Yes, I know - I can change the number of threads in my app with an entry in a properties file.

  5. Sublimis commented on Dec 27, 2024

    @Sublimis
    Contributor

    Will issue a PR for this shortly so we all can do testing. Seems like synchronizations in RenderTheme are unnecessarily broad.

  6. mg4gh commented on Dec 27, 2024

    @mg4gh
    ContributorAuthor

    Will issue a PR for this shortly so we all can do testing. Seems like synchronizations in RenderTheme are unnecessarily broad.

    Exactly, I think it is sufficient to have the maps in RenderTheme synchronized. This speeds up drastically.

  7. devemux86 commented on Dec 27, 2024

    @devemux86
    Collaborator

    General note:

    Everyone please make the tests with Mapsforge samples, so that we can reproduce them.

  8. Sublimis commented on Dec 27, 2024

    @Sublimis
    Contributor

    Please test with this #1599

  9. devemux86 commented on Dec 27, 2024

    @devemux86
    Collaborator

    I merged the pull request in branch issue_1599 for testing.

    @mg4gh Can you test it?

  10. mg4gh commented on Dec 27, 2024

    @mg4gh
    ContributorAuthor

    If tested the changes from @Sublimis - and the result looks good.

    Concerning the implementation

    private void matchWay(RenderCallback renderCallback, final RenderContext renderContext, Closed closed, PolylineContainer way) {
        // check cached instructions
        int matchingCacheKey = computeMatchingCacheKey(way.getTags(), way.getUpperLeft().zoomLevel, closed);
    
        synchronized (this.wayMatchingCache) {
            RenderInstruction[] instructions = this.wayMatchingCache.get(matchingCacheKey);
            if (instructions != null) {
                //noinspection ForLoopReplaceableByForEach
                for (int i = 0; i < instructions.length; ++i) {
                    instructions[i].renderWay(renderCallback, renderContext, way);
                }
                return;
            }
        }
        ....
    

    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.

  11. Sublimis commented on Dec 27, 2024

    @Sublimis
    Contributor

    If the solution from #1599 works properly, then the Collections.synchronizedMap should also work properly. Will update the PR now so you can test.

  12. Sublimis commented on Dec 27, 2024

    @Sublimis
    Contributor

    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?

  13. devemux86 commented on Dec 27, 2024

    @devemux86
    Collaborator

    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?

    @Sublimis Do you mean with the current pull request implementation?
    Or the suggestion above?

  14. devemux86 commented on Dec 27, 2024

    @devemux86
    Collaborator

    I try to reproduce the reported difference by @mg4gh, but without success.
    (even with the master branch)

    @Sublimis What is your experience with Mapsforge multithreaded rendering?
    Do you use it and how? Do you see such difference?

  15. Sublimis commented on Dec 27, 2024

    @Sublimis
    Contributor

    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?

    @Sublimis Do you mean with the current pull request implementation? Or the suggestion above?

    For both. When you look at the code for matchWay() above, renderContext and way parameters are okay, they are instances per RendererJob so do not need to be synchronized. But the renderCallback is usually a StandardRenderer implementing the RenderCallback interface, without synchronization.

    This doesn't seem to need synchronization either. However, can you 100% confirm such a claim?

  16. 37 remaining items

  17. mg4gh commented on Dec 29, 2024

    @mg4gh
    ContributorAuthor

    Now making matchNode and matchWay again synchronized I got this result (filtered for DatabaseRenderer and MapWorkerPool):

    1599_multi_threading_synchronized_methods.log

  18. mg4gh commented on Dec 29, 2024

    @mg4gh
    ContributorAuthor

    So bottom line there is a huge difference between the old and the current implementation.

  19. mg4gh commented on Dec 29, 2024

    @mg4gh
    ContributorAuthor

    PR update. More synchronizations were needed in RenderInstruction-s and maybe in a few other places. To explain the reason, look at the following lines as an example. If two jobs/threads compute the same cache key, it could happen that they call the same render instructions at the same time.

        public void matchNode(RenderCallback renderCallback, final RenderContext renderContext, PointOfInterest poi) {
            // check cached instructions
            int matchingCacheKey = computeMatchingCacheKey(poi.tags, renderContext.rendererJob.tile.zoomLevel, Closed.NO);
    
    
            final RenderInstruction[] instructions;
            synchronized (this.poiMatchingCache) {
                instructions = this.poiMatchingCache.get(matchingCacheKey);
            }
            if (instructions != null) {
                //noinspection ForLoopReplaceableByForEach
                for (int i = 0; i < instructions.length; ++i) {
                    instructions[i].renderNode(renderCallback, renderContext, poi);
                }
                return;
            }
    
    
            // cache miss

    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.

  20. Sublimis commented on Dec 29, 2024

    @Sublimis
    Contributor

    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.

    Ok, some cacophony here. This is exactly why we synchronized all RenderInstruction-s, please see the code in #1599 .

  21. mg4gh commented on Dec 29, 2024

    @mg4gh
    ContributorAuthor

    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.

    All the RenderCallback calls end up in RenderContext in an addLabel or an add to the wayList. So from my perspective in the Constructor of RenderContext

        this.labels = Collections.synchronizedList( new LinkedList<>() );
    

    the label list should be synchronized and in the createWayList also the most inner way lists:

                innerWayList.add(Collections.synchronizedList( new ArrayList<ShapePaintContainer>(0) ));
    

    Both seem to have no negative impact on the performance.

  22. Sublimis commented on Dec 29, 2024

    @Sublimis
    Contributor

    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.

    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 RenderContext so no concurrency:

    /**
    * Called when a job needs to be executed.
    *
    * @param rendererJob the job that should be executed.
    */
    public TileBitmap executeJob(RendererJob rendererJob) {
    TileBitmap output = null;
    RenderContext renderContext = null;
    try {
    renderContext = new RenderContext(rendererJob, graphicFactory);

  23. mg4gh commented on Dec 29, 2024

    @mg4gh
    ContributorAuthor

    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.

    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 RenderContext so no concurrency:

    /**
    * Called when a job needs to be executed.
    *
    * @param rendererJob the job that should be executed.
    */
    public TileBitmap executeJob(RendererJob rendererJob) {
    TileBitmap output = null;
    RenderContext renderContext = null;
    try {
    renderContext = new RenderContext(rendererJob, graphicFactory);

    ok, if each Thread has its own context, then this seem not necessary.

  24. added this to the 0.23.0 milestone on Dec 30, 2024
  25. devemux86 commented on Dec 30, 2024

    @devemux86
    Collaborator

    This should be fixed by #1599.

  26. devemux86 commented on Dec 30, 2024

    @devemux86
    Collaborator

    @Sublimis

    when you say "single thread", what does that mean?

    @mg4gh probably means the Parameters.NUMBER_OF_THREADS value.

  27. devemux86 commented on Dec 30, 2024

    @devemux86
    Collaborator

    @mg4gh After merging pull request #1599, have you compared single and multi threading?
    Which one works best in your tests?

  28. mg4gh commented on Dec 30, 2024

    @mg4gh
    ContributorAuthor

    @mg4gh After merging pull request #1599, have you compared single and multi threading? Which one works best in your tests?

    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.

  29. devemux86 commented on Dec 30, 2024

    @devemux86
    Collaborator

    The original described problem (just startup and initial show of map) was with multithrading about 18s.
    With one single rendering thread it was about 4.1s.
    With current implementation it is about 2.6s.

    @mg4gh You mentioned above these results about the improvement in multi threading.

    How is single thread now in your tests compared to multi threads?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions