Skip to content

Commit

Permalink
Improve parse_static_options speed
Browse files Browse the repository at this point in the history
Comparing each element of the static options with each incoming element
is very inefficient. Since dictionaries are ordered this can be
simplified to a simple key value insert.

Prior to this change:
```
parse_static_options (/Users/mvandenb/src/galaxy/lib/galaxy/tool_util/parser/xml.py:1207)
function called 7183 times

         313317 function calls in 11.318 seconds

   Ordered by: cumulative time, internal time, call count

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     7183   11.281    0.002   11.318    0.002 xml.py:1207(parse_static_options)
    99675    0.029    0.000    0.033    0.000 __init__.py:1004(string_as_bool)
    99675    0.004    0.000    0.004    0.000 {method 'lower' of 'str' objects}
    99601    0.004    0.000    0.004    0.000 {method 'append' of 'list' objects}
     7183    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        0    0.000             0.000          profile:0(profiler)
```
after:
```
*** PROFILER RESULTS ***
parse_static_options (/Users/mvandenb/src/galaxy/lib/galaxy/tool_util/parser/xml.py:1207)
function called 7183 times

         220899 function calls in 0.260 seconds

   Ordered by: cumulative time, internal time, call count

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     7183    0.230    0.000    0.259    0.000 xml.py:1207(parse_static_options)
    99675    0.026    0.000    0.029    0.000 __init__.py:1004(string_as_bool)
    99675    0.004    0.000    0.004    0.000 {method 'lower' of 'str' objects}
     7183    0.000    0.000    0.000    0.000 {method 'values' of 'dict' objects}
     7183    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        0    0.000             0.000          profile:0(profiler)
```

This is most due to openms_idfilter, which has 2928 static options.
  • Loading branch information
mvdbeek committed Aug 17, 2023
1 parent 03ac08a commit b6fec9b
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions lib/galaxy/tool_util/parser/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -1214,21 +1214,16 @@ def parse_static_options(self):
>>> xis.parse_static_options()
[('a', 'a', True), ('b', 'b', False)]
"""
static_options = list()

deduplicated_static_options = {}

elem = self.input_elem
for option in elem.findall("option"):
value = option.get("value")
text = option.text or value
selected = string_as_bool(option.get("selected", False))
present = False
for i, o in enumerate(static_options):
if o[1] == value:
present = True
static_options[i] = (text, value, selected)
break
if not present:
static_options.append((text, value, selected))
return static_options
deduplicated_static_options[value] = (text, value, selected)
return list(deduplicated_static_options.values())

def parse_optional(self, default=None):
"""Return boolean indicating whether parameter is optional."""
Expand Down

0 comments on commit b6fec9b

Please sign in to comment.