Use _ slots _ in Python to cache resources to save memory overhead

Source: Internet
Author: User
This article mainly introduces how to use _ slots _ in Python to save memory overhead by caching resources, and the sample code is very simple, for more information, see how the Python web server of Oyster.com uses a huge Python dicts (hash table) to cache a large amount of static resources. Recently, in the Image class, we used only one line of _ slots _ code to save more than 2 GB of service processes (4 in total) occupied by 6 GB of memory.

This is one of the servers before and after the deployment code:

Alloc has approximately 1 million instances similar to the following class:

Class Image (object ):
Def _ init _ (self, id, caption, url ):
Self. id = id
Self. caption = caption
Self. url = url
Self. _ setup ()

#... Other methods...

By default, Python uses a dict to store the attributes of an object instance. This is generally good and flexible, and you can set new attributes at will during running.

However, for some small classes with fixed attributes that should be known before "compilation", this dict is a bit wasteful of memory. And when you multiply this small waste by 1 million, it will be quite different. In Python, you can set _ slots __in class, which is a list containing these fixed attribute names. In this way, Python will no longer use dict, and only allocate space for these attributes.

Class Image (object ):
_ Slots _ = ['id', 'Caption ', 'URL']

Def _ init _ (self, id, caption, url ):
Self. id = id
Self. caption = caption
Self. url = url
Self. _ setup ()

#... Other methods...

You can also use collections. namedtuple, which allows access to parameters, but only occupies the space of a tuple. This is similar to _ slots. But I always think it is strange to inherit a namedtuple class. In addition, if you need custom initialization, you should reload _ new _ instead of _ init __.

Warning:Do not rush to this optimization and use it in all places. This approach is not conducive to code maintenance, and it will be effective only when you have thousands of instances.

Note:

Webreac: In my opinion, the _ slots _ keyword is not only speed optimization (note: memory optimization should be applied here), but also a reliable "document" for class field names. This facilitates code maintenance. Why do you think it is not good?

Ben Hoyt (author): An interesting saying: I'm not sure whether to use _ slots _ as a document. But it is indeed a good note. The reason I used to say this is that you need to define the field name twice (not DRY enough ). Namedtuple is similar.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.