mkrd / DictDataBase

A python NoSQL dictionary database, with concurrent access and ACID compliance
MIT License
234 stars 11 forks source link

Evaluate usage of mmap #33

Open mkrd opened 1 year ago

mkrd commented 1 year ago

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

mkrd commented 1 year ago
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()