Skip to content

Improve speed of hasattr on IDS structures - #140

Open
maarten-ic wants to merge 1 commit into
iterorganization:developfrom
maarten-ic:performance/hasattr
Open

Improve speed of hasattr on IDS structures#140
maarten-ic wants to merge 1 commit into
iterorganization:developfrom
maarten-ic:performance/hasattr

Conversation

@maarten-ic

Copy link
Copy Markdown
Collaborator

Replace _path (which constructs the path including AoS indices) with the static metadata.path_string when an attribute cannot be found.

Speeds up the following sample from 110ms to 0.5ms (200x gain):

import timeit
import imas

cp = imas.IDSFactory().core_profiles()
cp.profiles_1d.resize(1000)
def totime():
    hasattr(cp.profiles_1d[999], "xyz")
print(timeit.repeat(totime, number=1000))

Changed behaviour:
Before: AttributeError: IDS structure 'profiles_1d[999]' has no attribute 'xyz' After: AttributeError: IDS structure 'profiles_1d' has no attribute 'xyz'

Since the index of an AoS is not relevant for whether an attribute exists or not, I believe this is not a problem.

Replace `_path` (which constructs the path including AoS indices) with the static `metadata.path_string` when an attribute cannot be found.

Speeds up the following sample from 110ms to 0.5ms (200x gain):

```python
import timeit
import imas

cp = imas.IDSFactory().core_profiles()
cp.profiles_1d.resize(1000)
def totime():
    hasattr(cp.profiles_1d[999], "xyz")
print(timeit.repeat(totime, number=1000))
```

**Changed behaviour:**
Before: `AttributeError: IDS structure 'profiles_1d[999]' has no attribute 'xyz'`
After: `AttributeError: IDS structure 'profiles_1d' has no attribute 'xyz'`

Since the index of an AoS is not relevant for whether an attribute exists or not, I believe this is not a problem.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant