Skip to content

Use ffi.gc to handle object lifecycle #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2023
Merged

Use ffi.gc to handle object lifecycle #5

merged 1 commit into from
May 18, 2023

Conversation

bbayles
Copy link
Collaborator

@bbayles bbayles commented May 18, 2023

Re: this discussion, this PR re-works how the objects from the C library are managed.

Previously I was using __enter__ and __exit__ to ensure that ada_free got called. But CFFI provides ffi.gc, which can do this automatically:

ffi.gc(cdata, destructor, size=0): return a new cdata object that points to the same data. Later, when this new cdata object is garbage-collected, destructor(old_cdata_object) will be called

This allows us to avoid the overhead of __enter__ and __exit__. On a benchmark script this saved roughly 20% of execution time.

@bbayles bbayles merged commit eb8f596 into main May 18, 2023
@bbayles bbayles deleted the gc-destructor branch May 18, 2023 20:59
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