Skip to content

Improve speed of hasattr on IDS structures - #140

Merged
olivhoenen merged 1 commit into
iterorganization:developfrom
maarten-ic:performance/hasattr
Aug 6, 2026
Merged

Improve speed of hasattr on IDS structures#140
olivhoenen merged 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.

@olivhoenen olivhoenen left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@olivhoenen
olivhoenen merged commit 4390fae into iterorganization:develop Aug 6, 2026
9 checks passed
@maarten-ic
maarten-ic deleted the performance/hasattr branch August 6, 2026 09:24
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.

2 participants