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

Optional caching mechanism for table.lookup() #350

Open
simonw opened this issue Dec 6, 2021 · 3 comments
Open

Optional caching mechanism for table.lookup() #350

simonw opened this issue Dec 6, 2021 · 3 comments
Labels

Comments

@simonw
Copy link
Owner

simonw commented Dec 6, 2021

Inspired by work on git-history where I used this pattern:

    column_name_to_id = {}

    def column_id(column):
        if column not in column_name_to_id:
            id = db["columns"].lookup(
                {"namespace": namespace_id, "name": column},
                foreign_keys=(("namespace", "namespaces", "id"),),
            )
            column_name_to_id[column] = id
        return column_name_to_id[column]

If you're going to be doing a large number of table.lookup(...) calls and you know that no other script will be modifying the database at the same time you can presumably get a big speedup using a Python in-memory cache - maybe even a LRU one to avoid memory bloat.

@simonw simonw added the enhancement New feature or request label Dec 6, 2021
@simonw
Copy link
Owner Author

simonw commented Dec 6, 2021

API could be this:

id = db["columns"].lookup(
    {"namespace": namespace_id, "name": column},
    cache=True
)

This could default to a 100 item LRU cache. You could perhaps modify that with cache_size=500 or with cache_size=None to disable the size limit on that cache.

@simonw
Copy link
Owner Author

simonw commented Dec 6, 2021

Should I implement this remember to apply the optimization in git-history.

@simonw
Copy link
Owner Author

simonw commented Dec 6, 2021

Would be interesting to micro-benchmark this to get an idea for how much of a performance boost it is, since the indexed SQLite lookups used by table.lookup() should be really fast already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant