Skip to content
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

Support for python's Buffer protocol #52

Open
mkzeender opened this issue Mar 5, 2024 · 2 comments
Open

Support for python's Buffer protocol #52

mkzeender opened this issue Mar 5, 2024 · 2 comments

Comments

@mkzeender
Copy link

The save-stating and screenshotting methods should be able to read from and write to any python object that exposes a buffer.

Writing the data directly into a buffer will greatly increase performance, because you won't have to allocate memory for a bytes object, nor will you have to copy the data twice.

The buffer protocol was made an official protocol as of python 3.11, and all major array-like objects support it (including bytes, bytearray, array.array, numpy.array, etc).

@Felk
Copy link
Owner

Felk commented Mar 10, 2024

Sounds like a great addition. I'll keep it in mind, thanks! Any concrete ideas how the API would look like? E.g. passing in the buffer to write to?

@mkzeender
Copy link
Author

I guess numpy-style would be, like,

from collections.abc import Buffer
@overload
def save_to_bytes(out:Buffer)->None:
   ...
def load_from_bytes(in_:Buffer):
   ... # bytes implements Buffer so this signature doesn't need to be overloaded
def mem_size() -> int: ...

For the savestates. Basically, just expanding the existing api to accept any buffer object.

As for the screenshot, I'm not entirely sure what's the best way. Maybe a way to set a global buffer that the frames are dumped into? That would probably suffice. And a way to get the dimensions of the image to set the size of the buffer.

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

No branches or pull requests

2 participants