Skip to content

Commit

Permalink
refactor previous commit
Browse files Browse the repository at this point in the history
as [at]kayoub5 correctly pointed out, `inspect.signature()` is pretty
slow and this is a relatively "hot" code path, which lead to a
performance regression of about 30% when listing one of my bigger PDX
files.

as [at]kayoub5 suggested, `inspect.signature()` can be replaced by
`dataclasses.fields()`. This keeps the performance `list` for above
file virtually unchanged. Cool!

Signed-off-by: Andreas Lauser <andreas.lauser@mbition.io>
Signed-off-by: Alexander Walz <alexander.walz@mbition.io>
  • Loading branch information
andlaus committed Oct 20, 2023
1 parent 9cb2bce commit 3788d66
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions odxtools/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: MIT
import dataclasses
import re
from inspect import signature
from typing import Any, Dict, Optional
from xml.etree import ElementTree

Expand Down Expand Up @@ -32,10 +32,12 @@ def create_description_from_et(et_element: Optional[ElementTree.Element],) -> Op
def dataclass_fields_asdict(obj: Any) -> Dict[str, Any]:
"""Extract all attributes from a dataclass object that are fields.
This makes hierarchies of dataclasses possible while initializing
the base class using common code.
This is a non-recursive version of `dataclasses.asdict()`. Its
purpose is to make hierarchies of dataclasses possible while
initializing the base class using common code.
"""
return {x: getattr(obj, x) for x in signature(type(obj)).parameters}
return {x.name: getattr(obj, x.name) for x in dataclasses.fields(obj)}


# ISO 22901 section 7.1.1
Expand Down

0 comments on commit 3788d66

Please sign in to comment.