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

Slow printing of layout if filtered legend contains a layer that has a symbology classified using a virtual attribute #57230

Open
2 tasks done
stafoo opened this issue Apr 23, 2024 · 10 comments
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! Map and Legend Related to map or legend rendering Print Layouts Related to QGIS Print Layouts, Atlas or Reporting frameworks Regression Something which used to work, but doesn't anymore

Comments

@stafoo
Copy link

stafoo commented Apr 23, 2024

What is the bug or the crash?

Filtered legends in layouts slow down printing process in QGIS 3.34.5 and 3.36.1.
It happens when one layer has a symbology classified using a virtual attribute (such as a symbol showing only elements that are selected via is_selected() expression, as described here: #57230 (comment))
This behavios was not observed in QGIS 3.28.10 with the same project.

In the video example below, activating the filter option in legend significantly slows down the print process:

  • with filtered legend: 30 seconds
  • without filtered legend: 2 seconds
Registra_2024_04_23_15_55_37_919_compresso.mp4

This becomes even more critical with multipage layouts and with projects containing several layers.
This problem does not seems to be related to atlas.

Steps to reproduce the issue

  1. create a layer with a virtual attribute (in my case I used is_selected() expression; 1 means the element is selected, 0 not selected)
  2. classify the layer symbology using the virtual attribute
  3. create a layout (with map + legend)
  4. activate the "filtered legend" option, then print the layout to pdf (very slow)
  5. de-activate the "filtered legend" option, print the layout to pdf again (this time you will notice how it is much faster)

Versions

Versione di QGIS | 3.34.5-Prizren
Revisione codice QGIS | 4b30849
Versione Qt | 5.15.3
Versione Python | 3.9.18
Versione GDAL/OGR | 3.8.4
Versione PROJ | 9.3.1
Versione database del Registro EPSG | v10.098 (2023-11-24)
Versione GEOS | 3.12.1-CAPI-1.18.1
Versione SQLite | 3.41.1
Versione PDAL | 2.6.0
Versione client PostgreSQL | 16.2
Versione SpatiaLite | 5.1.0
Versione QWT | 6.1.6
Versione QScintilla2 | 2.13.4
Versione SO | Windows 10 Version 2009

Plugins Python attivi
AnotherDXF2Shape | 1.3.1
contour | 2.0.13
felt | 2.0.1
LAStools | 2.1.0
mmqgis | 2021.9.10
nominatim_locator_filter | 0.3.2
OnlineRoutingMapper | 0.9
profiletool | 4.2.6
qfieldsync | v4.9.1
quick_map_services | 0.19.34
rivergis | 3.0
slyr_community | 5.0.0
StreetView | 3.2
db_manager | 0.1.20
grassprovider | 2.12.99
MetaSearch | 0.3.6
processing | 2.12.99

Supported QGIS version

  • I'm running a supported QGIS version according to the roadmap.

New profile

@stafoo stafoo added the Bug Either a bug report, or a bug fix. Let's hope for the latter! label Apr 23, 2024
@stafoo stafoo changed the title Slow print layout with filtered legend in QGIS 3.34.5 and QGIS 3.36.1 Slow printing of layout with filtered legend in QGIS 3.34.5 and QGIS 3.36.1 Apr 23, 2024
@stafoo
Copy link
Author

stafoo commented Apr 29, 2024

This problem is still present in QGIS 3.34.6-1

@stafoo
Copy link
Author

stafoo commented Apr 29, 2024

I investigated the error a bit.
It seems that the slowness happens only if the filtered legend contains a layer that has a symbology classified using a virtual attribute.

immagine

immagine

Making the attribute Selezionato a "real" attribute (and not a virtual one), the printing process is fast even if the legend is filtered.

@stafoo stafoo changed the title Slow printing of layout with filtered legend in QGIS 3.34.5 and QGIS 3.36.1 Slow printing of layout if filtered legend contains a layer that has a symbology classified using a virtual attribute Apr 29, 2024
@agiudiceandrea
Copy link
Member

@stafoo thanks for reporting. It would be useful if you provided a sample project and layet(s) with which the issue occurs.

@agiudiceandrea agiudiceandrea added the Print Layouts Related to QGIS Print Layouts, Atlas or Reporting frameworks label Apr 29, 2024
@stafoo
Copy link
Author

stafoo commented Apr 30, 2024

TestBug57230_project_gpkg.zip

Sample project attached. Steps to reproduce:

  1. open the project, select one element (or more) in layer (the virtual attribute "selected" is now updated with 'true' in the selected elements)
  2. open the print layout and export a pdf (slow print)
  3. uncheck the filter "Only show items inside linked maps" in the legend option, then print (fast print)
    This test project is made with QGIS 3.34.6

@agiudiceandrea agiudiceandrea added Map and Legend Related to map or legend rendering Regression Something which used to work, but doesn't anymore labels Apr 30, 2024
@agiudiceandrea
Copy link
Member

I can confirm the issue on Windows using either QGIS 3.36.2 or QGIS 3.34.6. The issue didn't occur using QGIS 3.28.15.

I also see a glitch in the legend's dimensions which incorrectly change when the "Only show items inside linked maps" checkbox is checked /unchecked
Video_2024-04-30_123124

@stafoo
Copy link
Author

stafoo commented May 8, 2024

Update on the bug.
The slow printing problem is present even if the Legend in the layout does not contain the "layer" object.
If you remove "layer" from the objects shown in the legend and you print the layout, it will still be slow (with filtered legend).
In addition, Layer must to be visible in the layout map to notice the slow printing.

  • removing the virtual attribute filled by the expression is_selected(), the printing process is always fast also with the "Only show items inside linked maps" option checked
  • with the virtual attribute filled by the expression is_selected(), the printing process is slow ONLY if the elements of the Layer are shown in the layout map; the "Only show items inside linked maps" option must be checked
  • with the virtual attribute filled by the expression is_selected(), the printing process is fast if the elements of the layer are NOT shown in the layout map, even if the "Only show items inside linked maps" option is checked

@elpaso elpaso self-assigned this May 29, 2024
@elpaso
Copy link
Contributor

elpaso commented May 29, 2024

I've spent quite some time trying to find a solution but I couldn't.

The issue comes from the hit test task QgsMapHitTestTask which is run in a separate thread by the task manager and gets locked on a semaphore here: https://github.com/qgis/QGIS/blob/master/src/core/expression/qgsexpressionutils.cpp#L179

this happens because the is_selected function like may others is invoked in the layer's thread which happens to be the main thread which is waiting for the hit test to finish.

The 30 seconds come from the if ( mNotFinishedMutex.tryLock( timeout ) ) so this is basically deadlock which eventually times out and by chance continues without errors.

@nyalldawson do you have any recommendation about how to handle this?
I thought about cloning the layers into the hit test thread but I'm not sure it is a viable solution.

@nyalldawson
Copy link
Collaborator

@elpaso

Hmm, very tricky

The only safe approach I can think would be to have some QgsPreparedExpressionData" which we generate in the main thread when creating that task. And then a bunch of plumbing to get that object passed to the expression at evaluation time (maybe attaching it to the expression context?). And then if the prepared expression data is available, we use it instead of calling the map layer function direct.
And then we prepopulate the expression data with values we need for functions like this, storing them in a variant map for thread safety.

@stafoo
Copy link
Author

stafoo commented Jun 26, 2024

@elpaso I have tested again this bug with this build: #57649 (comment)

As noted by @m-kuhn (#57649 (comment)) the expression is_selected still slows down the printing process.

My 2 cents

@stafoo
Copy link
Author

stafoo commented Aug 7, 2024

Bug confirmed with QGIS 3.34.9

@elpaso elpaso removed their assignment Jan 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! Map and Legend Related to map or legend rendering Print Layouts Related to QGIS Print Layouts, Atlas or Reporting frameworks Regression Something which used to work, but doesn't anymore
Projects
None yet
Development

No branches or pull requests

4 participants