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

Evaluate usage of mmap #33

Open
mkrd opened this issue Nov 14, 2022 · 1 comment
Open

Evaluate usage of mmap #33

mkrd opened this issue Nov 14, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@mkrd
Copy link
Owner

mkrd commented Nov 14, 2022

mmap could greatly improve the performance of .at(…, key=…).session()

@mkrd
Copy link
Owner Author

mkrd commented Nov 15, 2022

import time
import mmap


def naive_insert_at_beginning():
    t1 = time.monotonic()
    with open("ddb_storage/users copy.json", "r+b") as f:
        data = f.read()
        f.seek(0)
        f.write(b"aaaa" + data)
        f.flush()

    t2 = time.monotonic()
    print(f"Time taken: {(t2 - t1) * 1000}ms")


# naive_insert_at_beginning()


def mmap_insert_at_beginning():
    t1 = time.monotonic()
    append_str = b"dddd"
    with open("ddb_storage/users copy.json", "a+b") as f:
        f.seek(0, 2)
        f.write(append_str)
        f.flush()

        with mmap.mmap(f.fileno(), 0, flags=mmap.MAP_PRIVATE, ) as m:
            m.move(0, 4, len(m) - 4)
            m.seek(0)
            m.write(append_str)
            m.flush()



    t2 = time.monotonic()
    print(f"Time taken: {(t2 - t1) * 1000}ms")


mmap_insert_at_beginning()

@mkrd mkrd added the enhancement New feature or request label Nov 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant