[ovs-dev] [PATCH] [python] Avoid alloc of an attr dict/row+column

Terry Wilson twilson at redhat.com
Tue Oct 12 19:13:31 UTC 2021

Python objects normally have a dictionary named __dict__ allocated
for handling dynamically assigned attributes. Depending on
architecture and Python version, that empty dict may be between
64 and 280 bytes.

Seeing as Atom and Datum objects do not need dynamic attribute
support and there can be millions of rows in a database, avoiding
this allocation with __slots__ can save 100s of MBs of memory per
Idl process.

Signed-off-by: Terry Wilson <twilson at redhat.com>
 python/ovs/db/data.py | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/python/ovs/db/data.py b/python/ovs/db/data.py
index 99bf80ed6..8db21b837 100644
--- a/python/ovs/db/data.py
+++ b/python/ovs/db/data.py
@@ -64,6 +64,8 @@ def returnUnchanged(x):
 class Atom(object):
+    __slots__ = ('value', 'type')
     def __init__(self, type_, value=None):
         self.type = type_
         if value is not None:
@@ -266,6 +268,8 @@ class Atom(object):
 class Datum(object):
+    __slots__ = ('type', 'values')
     def __init__(self, type_, values={}):
         self.type = type_
         self.values = values

More information about the dev mailing list