]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-144995: Optimize memoryview == memoryview (#144996)
authorVictor Stinner <vstinner@python.org>
Tue, 3 Mar 2026 11:15:32 +0000 (12:15 +0100)
committerGitHub <noreply@github.com>
Tue, 3 Mar 2026 11:15:32 +0000 (12:15 +0100)
commitc9d123482acebcf725c847bd6f8354bdd9314189
tree0c7d4688c5b40621ca234b7d4e05a5b567497ee4
parentf1de65b3669226d563802a32b78a2294e971151a
gh-144995: Optimize memoryview == memoryview (#144996)

Optimize memoryview comparison: a memoryview is equal to itself, there is no
need to compare values, except if it uses float format.

Benchmark comparing 1 MiB:

    from timeit import timeit
    with open("/dev/random", 'br') as fp:
        data = fp.read(2**20)
    view = memoryview(data)
    LOOPS = 1_000
    b = timeit('x == x', number=LOOPS, globals={'x': data})
    m = timeit('x == x', number=LOOPS, globals={'x': view})
    print("bytes %f seconds" % b)
    print("mview %f seconds" % m)
    print("=> %f time slower" % (m / b))

Result before the change:

    bytes 0.000026 seconds
    mview 1.445791 seconds
    => 55660.873940 time slower

Result after the change:

    bytes 0.000026 seconds
    mview 0.000028 seconds
    => 1.104382 time slower

This missed optimization was discovered by Pierre-Yves David
while working on Mercurial.

Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
Lib/test/test_memoryview.py
Misc/NEWS.d/next/Core_and_Builtins/2026-02-19-12-49-15.gh-issue-144995.Ob2oYJ.rst [new file with mode: 0644]
Objects/memoryobject.c