Skip to content

Use bytearrays for building up bytes for I/O. #245

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

Merged
merged 1 commit into from
Dec 28, 2021

Conversation

alexmv
Copy link
Contributor

@alexmv alexmv commented Dec 27, 2021

Bytes are immutable in python, which means that repeatedly appending
to them is quadratic in time. That is, we would like and expect
that deleting 10k objects 10 times would take about as long as
deleting 100k, but because we repeatedly concatenate to a bytes object
in the loop, they are not:

timeit.timeit(lambda: server.delete_multi(["foo"]*10000),number=10)
# 0.5087270829999966
timeit.timeit(lambda: server.delete_multi(["foo"]*100000),number=1)
# 10.650619775999985

Switch to using bytearray in all places which handle variable
numbers of bytes. After this change:

timeit.timeit(lambda: server.delete_multi(["foo"]*10000),number=10)
# 0.1197161969999998
timeit.timeit(lambda: server.delete_multi(["foo"]*100000),number=1)
# 0.1269589350000011

This change is Reviewable

Bytes are immutable in python, which means that repeatedly appending
to them is _quadratic_ in time.  That is, we would like and expect
that deleting 10k objects 10 times would take about as long as
deleting 100k, but because we repeatedly concatenate to a bytes object
in the loop, they are not:

    timeit.timeit(lambda: server.delete_multi(["foo"]*10000),number=10)
    # 0.5087270829999966
    timeit.timeit(lambda: server.delete_multi(["foo"]*100000),number=1)
    # 10.650619775999985

Switch to using `bytearray` in all places which handle variable
numbers of bytes.  After this change:

    timeit.timeit(lambda: server.delete_multi(["foo"]*10000),number=10)
    # 0.1197161969999998
    timeit.timeit(lambda: server.delete_multi(["foo"]*100000),number=1)
    # 0.1269589350000011
Copy link
Owner

@jaysonsantos jaysonsantos left a comment

Choose a reason for hiding this comment

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

Reviewed all commit messages.
Reviewable status: 0 of 1 files reviewed, all discussions resolved (waiting on @jaysonsantos)

@jaysonsantos
Copy link
Owner

thank you very much!

@jaysonsantos jaysonsantos merged commit 36770d2 into jaysonsantos:master Dec 28, 2021
jaysonsantos pushed a commit that referenced this pull request Dec 28, 2021
Bytes are immutable in python, which means that repeatedly appending
to them is _quadratic_ in time.  That is, we would like and expect
that deleting 10k objects 10 times would take about as long as
deleting 100k, but because we repeatedly concatenate to a bytes object
in the loop, they are not:

    timeit.timeit(lambda: server.delete_multi(["foo"]*10000),number=10)
    # 0.5087270829999966
    timeit.timeit(lambda: server.delete_multi(["foo"]*100000),number=1)
    # 10.650619775999985

Switch to using `bytearray` in all places which handle variable
numbers of bytes.  After this change:

    timeit.timeit(lambda: server.delete_multi(["foo"]*10000),number=10)
    # 0.1197161969999998
    timeit.timeit(lambda: server.delete_multi(["foo"]*100000),number=1)
    # 0.1269589350000011
@alexmv alexmv deleted the bytearray branch December 28, 2021 19:19
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.

2 participants