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

perf: optimize get_filestorage_size #293

Merged
merged 2 commits into from Feb 5, 2024

Conversation

uncle-lv
Copy link
Contributor

@uncle-lv uncle-lv commented Feb 5, 2024

The old get_filestorage_size get the size by len(file.read()) which triggers a read operation and consumes additional memory.
Old version:

def get_filestorage_size(file: FileStorage) -> int:
    size = len(file.read())
    file.stream.seek(0)
    return size

The new get_filestorage_size get the size by file.stream.getbuffer().nbytes which returns the number of bytes of the data directly without any extra overhead.
New version:

def get_filestorage_size(file: FileStorage) -> int:
    size: int = file.stream.getbuffer().nbytes  # type: ignore
    return size

@sloria
Copy link
Member

sloria commented Feb 5, 2024

Thanks!

@sloria sloria enabled auto-merge (squash) February 5, 2024 17:30
@sloria sloria merged commit dc40974 into marshmallow-code:dev Feb 5, 2024
7 checks passed
@uncle-lv uncle-lv deleted the perf-get-file-size branch February 18, 2024 12:21
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.

None yet

2 participants