A way to cache resources with __slots__ in Python to save memory overhead _python

Source: Internet
Author: User

We have mentioned how Oyster.com's Python Web server leverages a huge Python dicts (hash table) to cache large amounts of static resources. We recently in the image class, with just one line of __slots__ code, so that each 6G memory footprint of the service process (a total of 4), save more than 2G.

This is a screenshot of one of the servers before and after the code is deployed:

We have alloc about 1 million examples 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 stores the properties of an object instance with a Dict. This is good in general, and very flexible, and even you can set new properties at random at run time.

However, for small classes that know that there are several fixed attributes before compiling, this dict is a bit of a waste of memory. And when you multiply this little waste by 1 million, that's a big difference. In Python, you can set __slots__ in class, which is a list containing these fixed property names. This way Python will no longer use dict and only allocate space for these properties.

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 occupies only one tuple space. This is similar to __slots__. But I always find it strange to inherit a Namedtuple class. In addition, if you need to customize the initialization, you should overload __new__ instead of __init__.

Warning: don't rush this optimization and use it all over the place. This is not good for code maintenance, and it only works when you have thousands of instances.

The author's comments about "not conducive to code maintenance" saying:

WEBREAC: I think the __slots__ keyword is not just speed optimization (note: This should be memory optimization), but also a reliable "document" for class field names. This facilitates code maintenance. Why do you think it's bad?

Ben Hoyt (author): Interesting argument--I'm not sure that __slots__ should be used as a document. But it's a good note. The reason I said that before is that you need to "define" the field name two times (not dry). 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.