Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-132108: Add Buffer Protocol support to int.from_bytes to improve performance#132109

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
cmaloney wants to merge4 commits intopython:main
base:main
Choose a base branch
Loading
fromcmaloney:int_from_byteslike
Open
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
gh-132108: Add Buffer Protocol support to int.from_bytes
Speed up conversion from `bytes-like` objects like `bytearray` whilekeeping conversion from `bytes` stable.On a `--with-lto --enable-optimizaitons` build on my 64 bit Linux box:new:from_bytes_flags: Mean +- std dev: 28.6 ns +- 0.5 nsbench_convert[bytes]: Mean +- std dev: 50.4 ns +- 1.4 nsbench_convert[bytearray]: Mean +- std dev: 51.3 ns +- 0.7 nsold:from_bytes_flags: Mean +- std dev: 28.1 ns +- 1.1 nsbench_convert[bytes]: Mean +- std dev: 50.3 ns +- 4.3 nsbench_convert[bytearray]: Mean +- std dev: 64.7 ns +- 0.9 nsBenchmark code:```pythonimport pyperfimport timedef from_bytes_flags(loops):    range_it = range(loops)    t0 = time.perf_counter()    for _ in range_it:        int.from_bytes(b'\x00\x10', byteorder='big')        int.from_bytes(b'\x00\x10', byteorder='little')        int.from_bytes(b'\xfc\x00', byteorder='big', signed=True)        int.from_bytes(b'\xfc\x00', byteorder='big', signed=False)        int.from_bytes([255, 0, 0], byteorder='big')    return time.perf_counter() - t0sample_bytes = [    b'',    b'\x00',    b'\x01',    b'\x7f',    b'\x80',    b'\xff',    b'\x01\x00',    b'\x7f\xff',    b'\x80\x00',    b'\xff\xff',    b'\x01\x00\x00',]sample_bytearray = [bytearray(v) for v in sample_bytes]def bench_convert(loops, values):    range_it = range(loops)    t0 = time.perf_counter()    for _ in range_it:        for val in values:            int.from_bytes(val)    return time.perf_counter() - t0runner = pyperf.Runner()runner.bench_time_func('from_bytes_flags', from_bytes_flags, inner_loops=10)runner.bench_time_func('bench_convert[bytes]', bench_convert, sample_bytes, inner_loops=10)runner.bench_time_func('bench_convert[bytearray]', bench_convert, sample_bytearray, inner_loops=10)```
  • Loading branch information
@cmaloney
cmaloney committedApr 5, 2025
commitd898d27dad53846a7943a818e4010a49a53c3895
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
Speed up :meth:`int.from_bytes` when passed a bytes-like object such as a
:class:`bytearray`.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Speed up:meth:`int.from_bytes` when passed a bytes-like object such as a
:class:`bytearray`.
Speed up:meth:`int.from_bytes` when passed a bytes-like object such as
:class:`bytes` and:class:`bytearray`.

Choose a reason for hiding this comment

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

I do not think that it affectsbytes.

29 changes: 21 additions & 8 deletionsObjects/longobject.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6439,6 +6439,7 @@ int_from_bytes_impl(PyTypeObject *type, PyObject *bytes_obj,
{
int little_endian;
PyObject *long_obj, *bytes;
Py_buffer view;

if (byteorder == NULL)
little_endian = 0;
Expand All@@ -6452,14 +6453,26 @@ int_from_bytes_impl(PyTypeObject *type, PyObject *bytes_obj,
return NULL;
}

bytes = PyObject_Bytes(bytes_obj);
if (bytes == NULL)
return NULL;

long_obj = _PyLong_FromByteArray(
(unsigned char *)PyBytes_AS_STRING(bytes), Py_SIZE(bytes),
little_endian, is_signed);
Py_DECREF(bytes);
/* Use buffer protocol to avoid copies. */
if (PyObject_CheckBuffer(bytes_obj)) {
if (PyObject_GetBuffer(bytes_obj, &view, PyBUF_SIMPLE) != 0) {
return NULL;
}
long_obj = _PyLong_FromByteArray(view.buf, view.len, little_endian,
is_signed);
PyBuffer_Release(&view);
}
else {
/* fallback: Construct a bytes then convert. */
bytes = PyObject_Bytes(bytes_obj);
if (bytes == NULL) {
return NULL;
}
long_obj = _PyLong_FromByteArray(
(unsigned char *)PyBytes_AS_STRING(bytes), Py_SIZE(bytes),
little_endian, is_signed);
Py_DECREF(bytes);
}

if (long_obj != NULL && type != &PyLong_Type) {
Py_SETREF(long_obj, PyObject_CallOneArg((PyObject *)type, long_obj));
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp