folium icon indicating copy to clipboard operation
folium copied to clipboard

Performance hit when FeatureGroup is created with show=False.

Open spacediver99 opened this issue 3 years ago • 6 comments

Thanks for the great library - fun to use, and beautiful!

Not sure whether this was best put in the bug or feature request category, so to be safe I chose the latter.

I've run into the following performance issue:

The following code renders the map very fast:

import folium

m = folium.Map(location=[35.11567262307692,-89.97423444615382], zoom_start=12, tiles='Stamen Terrain')

for i in range(200):
    feature_group = folium.FeatureGroup(i, show=True)
    feature_group.add_to(m)

folium.LayerControl().add_to(m)
m

However, if show is set to False, it takes much longer to render.

import folium

m = folium.Map(location=[35.11567262307692,-89.97423444615382], zoom_start=12, tiles='Stamen Terrain')

for i in range(200):
    feature_group = folium.FeatureGroup(i, show=False)
    feature_group.add_to(m)

folium.LayerControl().add_to(m)
m

The problem is, I don't want these layers shown by default. So I'm wondering if there's a way to render the map, and then automatically uncheck the layers (I know that in these examples, the layers are "empty", but that is just to illustrate that the performance issue has nothing to do with the "content" of the layers).

Failing that, are there any other ways to deal with this performance issue?

Thanks very much for any guidance!

spacediver99 avatar Feb 20 '22 18:02 spacediver99

That's interesting. I can confirm this happens for me as well.

Know that show=False means we deactivate the layer after creating it. In other words, the layer will be created and shown, then removed again. I guess that's maybe causing the performance hit?

If we wanted to do this differently, it would mean we'd have to update the templates of all our classes! We'd have to change the line .addTo({{ this._parent.get_name() }}) into something like if (show) {.addTo({{ this._parent.get_name() }})}. That's a lot of changes, so that's why we opted to remove them after creation.

I'll label this as a bug, since it's an unintended effect.

Conengmo avatar Nov 17 '22 15:11 Conengmo

Thanks for the reply!

Given that it seems to take significantly longer to deactivate a set of layers than it is to create them, I wonder if there's any room for speeding up the deactivation part. I tried digging through the python code a while back, but was unable to find the relevant sections - perhaps this is because the issue is in the leaflet library? (I have no experience with JavaScript)

spacediver99 avatar Nov 19 '22 16:11 spacediver99

Removing the layers with show=False happens here: https://github.com/python-visualization/folium/blob/2e86b480bb77ce4b83f3a761233ec9dc0cceafec/folium/map.py#L137-L139

Conengmo avatar Nov 21 '22 15:11 Conengmo

Thanks, that's good to know :)

I'm unfamiliar with web templating so unsure how to test this code. Thinking about adding some perf_counter() timers around various parts of the code to see where bottleneck is. I may try that in the render method (see snippet below), , but not sure if possible to do in the LayerControl _template method you highlighted. But I can't imagine how the snippet below would be a bottleneck though, as it just seems to be iterating through and assigning dict values, and the get_name() method (which seems to be inherited from branca's element class) also looks inexpensive.

https://github.com/python-visualization/folium/blob/2e86b480bb77ce4b83f3a761233ec9dc0cceafec/folium/map.py#L159-L172

spacediver99 avatar Nov 22 '22 01:11 spacediver99

Note that the performance hit occurs in Javascript in your browser, so no need to profile the folium Python code. Instead, look at the generated html + JS and do the profiling in your browser. I assume when .remove() is called on all those feature groups, that's what makes it slow. That happens in JS in the browser. Not really something folium can do anything about, other than what I mentioned before in https://github.com/python-visualization/folium/issues/1573#issuecomment-1318848725.

Conengmo avatar Nov 22 '22 12:11 Conengmo

Thanks for clarification. I'll reply to this thread if I learn anything.

spacediver99 avatar Nov 22 '22 23:11 spacediver99

Since https://github.com/python-visualization/folium/pull/1690 was merged this should no longer be an issue.

Conengmo avatar May 18 '23 08:05 Conengmo