Git Product home page Git Product logo

Comments (11)

Animenosekai avatar Animenosekai commented on May 26, 2024 1

I took a look at the different implementations and I think that it could be possible to adapt pympler's asizeof implementation for Python >=3.2.

Here is my slightly modified implementation :
https://gist.github.com/Animenosekai/4e5a3a980e7ed2e542003a58a54ede96

from translate.

ZhymabekRoman avatar ZhymabekRoman commented on May 26, 2024 1

Maybe implement asynchronous checking and clearing the cache using ThreadPoolExecutor? This should speed up the caching process

from translate.

ZhymabekRoman avatar ZhymabekRoman commented on May 26, 2024

There is a small problem - it seems to me that it works a little slow, I still have no idea how to speed up

from translate.

Animenosekai avatar Animenosekai commented on May 26, 2024

I just tested the different implementations and yes our implementation is quite slow, pympler's one is a bit faster but sys.getsizeof is definitely the fastest (which is normal considering that there is no recursion).

from translate.

Animenosekai avatar Animenosekai commented on May 26, 2024

Or maybe we could check if the object is a native object, in which case we would check with sys.getsizeof and we would use other implementations for more complex objects

from translate.

Animenosekai avatar Animenosekai commented on May 26, 2024

Maybe implement asynchronous checking and clearing the cache using ThreadPoolExecutor? This should speed up the caching process

Well I tried to implement it too in 58521f1 but seems to also be slow

I'll need to figure out why because it seems odd...

from translate.

ZhymabekRoman avatar ZhymabekRoman commented on May 26, 2024

So I tried to optimise the LRU cache, and it seems I have it. Some numbers:

raw dictionary implementation code:
import os

if __name__ == "__main__":
    a = {}
    for i in range(1_000):
        print(i)
        a[i] = os.urandom(200_000_0)

Also in the test, I changed the old LRU implementation to use os.urandom instead of just putting numbers.


Just raw dictionary time:

python3 just_dict.py  0.03s user 8.59s system 99% cpu **8.643 total**

Old LRU cache implementation time:

python3 lru_old.py  48.94s user 8.59s system 86% cpu **1:06.65 total**

New LRU cache implementation time:

python3 lru.py  0.50s user 7.64s system 99% cpu **8.201 total**

time shows that the new LRU implementation is like a zero-overhead. I think that's a good result. In the new implementation, instead of recomputing the cache size every time, it simply takes the object size into a separate class property, and uses these values to free the cache in the future.

New LRU implementation code:
https://gist.github.com/ZhymabekRoman/43ee515959de024416e29b8dd97e4d96

from translate.

Animenosekai avatar Animenosekai commented on May 26, 2024

Ohhh I understand how you did that. Really cool numbers!

Did you try on python <3.7 to see if it was working as well ?

from translate.

ZhymabekRoman avatar ZhymabekRoman commented on May 26, 2024

Did you try on python <3.7 to see if it was working as well ?

Hmm, no. I'll try to install debian stretch on my machine and test it there with Python 3.5.

from translate.

ZhymabekRoman avatar ZhymabekRoman commented on May 26, 2024

Yes, it works great! Some debugging shows that all the logic works as expected.

from translate.

ZhymabekRoman avatar ZhymabekRoman commented on May 26, 2024

I think this issue can be closed, since we have merged into next branch

from translate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.