Last active
May 13, 2024 06:41
-
-
Save rindeal/583ce8f34d155c782c838ff2ec788940 to your computer and use it in GitHub Desktop.
Python 3 function to calculate optimal buffer size for any io.BufferedIOBase subclass, like io.BufferedReader and io.BufferedWriter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
=============================== | |
BufferedIOBase Buffer Size Calculator | |
=============================== | |
This Python script contains a function `buffio_get_buffsz` that calculates | |
the buffer size of a `BufferedIOBase` object. | |
Functionality | |
============= | |
The function `buffio_get_buffsz` takes a `BufferedIOBase` object as input and | |
returns the buffer size of the object. The buffer size is calculated by | |
rounding down the size of the object to the nearest power of 2. The size of | |
the object is obtained using the `sys.getsizeof()` function with a default | |
buffer size. | |
Usage | |
===== | |
Import the function into your Python script and pass a `BufferedIOBase` object | |
to it. The function will return the buffer size of the object. | |
.. code-block:: python | |
# Create a BufferedIOBase object | |
fileobj = io.BufferedReader(io.BytesIO(b"Hello World")) | |
# Get the buffer size | |
buffer_size = buffio_get_buffsz(fileobj) | |
print(f"The buffer size is {buffer_size}") | |
Source | |
====== | |
This function is based on a gist available at: | |
https://gist.github.com/rindeal/583ce8f34d155c782c838ff2ec788940 | |
""" | |
import io | |
import sys | |
def buffio_get_buffsz(fileobj: io.BufferedIOBase) -> int: | |
""" | |
Calculate the real buffer size of a BufferedIOBase object. | |
This function calculates the buffer size of a BufferedIOBase object by rounding down the size of the object to the nearest power of 2. | |
The size of the object is obtained using the sys.getsizeof() function with a default buffer size. | |
Source: | |
https://gist.github.com/rindeal/583ce8f34d155c782c838ff2ec788940 | |
Parameters: | |
fileobj (io.BufferedIOBase): The BufferedIOBase object for which the buffer size is to be calculated. | |
Returns: | |
int: The buffer size of the BufferedIOBase object. | |
""" | |
def round_down_to_pow2(n: int) -> int: | |
return 1 << (n.bit_length() - 1) if n != 0 else 0 | |
return round_down_to_pow2(sys.getsizeof(fileobj, io.DEFAULT_BUFFER_SIZE)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment