Skip to content

Conversation

@Schamper
Copy link
Member

Closes #113

Some micro benchmarks:

from dissect.cstruct import cstruct

cdef = """
#define X 512

enum MyEnum {
    A,
    B,
    C
};

struct test {
    uint32 a;
};
"""
cs = cstruct()
cs.load(cdef)

Before:

In: %timeit getattr(t.cs, "X")
58.1 ns ± 1.04 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

In: %timeit getattr(t.cs, "MyEnum")
227 ns ± 0.912 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

In: %timeit getattr(t.cs, "test")
219 ns ± 1.47 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

After:

In: %timeit getattr(t.cs, "X")
31.2 ns ± 0.0331 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

In: %timeit getattr(t.cs, "MyEnum")
33.1 ns ± 0.0884 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

In: %timeit getattr(t.cs, "test")
31.3 ns ± 0.0571 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

@Schamper Schamper force-pushed the type-as-attr branch 2 times, most recently from 1558ae7 to a7a1441 Compare July 3, 2025 10:10
@codecov
Copy link

codecov bot commented Jul 3, 2025

Codecov Report

❌ Patch coverage is 82.35294% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.91%. Comparing base (194b1b5) to head (c9c24e2).

Files with missing lines Patch % Lines
dissect/cstruct/tools/stubgen.py 68.00% 8 Missing ⚠️
dissect/cstruct/cstruct.py 93.10% 2 Missing ⚠️
dissect/cstruct/parser.py 83.33% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #114      +/-   ##
==========================================
- Coverage   93.01%   92.91%   -0.11%     
==========================================
  Files          21       21              
  Lines        2435     2470      +35     
==========================================
+ Hits         2265     2295      +30     
- Misses        170      175       +5     
Flag Coverage Δ
unittests 92.91% <82.35%> (-0.11%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Miauwkeru
Copy link
Contributor

Some initial remarks with these changes, some projects fail to build:

return name

def _add_attr(self, name: str, value: Any, replace: bool = False) -> None:
if not replace and (name in self.__dict__ and self.__dict__[name] != value):
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if not replace and (name in self.__dict__ and self.__dict__[name] != value):
if not replace and (self.__dict__.get(name) != value):

Or is the overhead of the function call higher?

if name in empty_cs.consts:
continue
body.append(textwrap.indent(f"{name}: Literal[{value!r}] = ...", prefix=indent))

Copy link
Contributor

Choose a reason for hiding this comment

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

There is an issue on L174/L185 where it now generates structures multiple times due to looking into typedefs instead of types

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.

Eagerly resolve types and set as attributes on the cstruct object

3 participants