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

Use __slots__ to reduce Statement memory usage #382

Merged
merged 2 commits into from Apr 10, 2018

Conversation

glennmatthews
Copy link
Contributor

Use __slots__ Python language feature to reduce the memory overhead of each Statement object. Also serves as a handy way to list all of the many possible Statement instance attributes in one place. In my testing this can reduce pyang's memory overhead by about 25%.

Memory usage of my app using baseline pyang without this change:

(Pdb) from guppy import hpy
(Pdb) hpy().heap()
Partition of a set of 1569817 objects. Total size = 390492928 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0  99535   6 156793960  40 156793960  40 dict of pyang.statements.Statement
     1 665307  42 76017560  19 232811520  60 list
     2  69021   4 37975224  10 270786744  69 dict (no owner)
     3  99555   6 27875400   7 298662144  76 dict of pyang.error.Position
     4  21854   1 23291824   6 321953968  82 collections.OrderedDict
     5  96418   6 15708544   4 337662512  86 unicode
     6 114464   7 11252864   3 348915376  89 str
     7  99555   6  6371520   2 355286896  91 pyang.error.Position
     8  99535   6  6370240   2 361657136  93 pyang.statements.Statement
     9  21854   1  6119120   2 367776256  94 dict of collections.OrderedDict

With this change:

(Pdb) from guppy import hpy
(Pdb) hpy().heap()
Partition of a set of 1564999 objects. Total size = 309499680 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0 665306  43 76018000  25  76018000  25 list
     1  99535   6 55739600  18 131757600  43 pyang.statements.Statement
     2  69020   4 37975328  12 169732928  55 dict (no owner)
     3  99555   6 27875400   9 197608328  64 dict of pyang.error.Position
     4  94177   6 26369560   9 223977888  72 dict of pyang.statements.Statement
     5  21854   1 23291824   8 247269712  80 collections.OrderedDict
     6  96418   6 15708544   5 262978256  85 unicode
     7 114900   7 11293768   4 274272024  89 str
     8  99555   6  6371520   2 280643544  91 pyang.error.Position
     9  21854   1  6119120   2 286762664  93 dict of collections.OrderedDict

@glennmatthews
Copy link
Contributor Author

Adding __slots__ to Position as well also takes it out of the top list:

Partition of a set of 1465357 objects. Total size = 283202912 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0 665306  45 76018064  27  76018064  27 list
     1  99535   7 55739600  20 131757664  47 pyang.statements.Statement
     2  69020   5 37974560  13 169732224  60 dict (no owner)
     3  94177   6 26369560   9 196101784  69 dict of pyang.statements.Statement
     4  21854   1 23291824   8 219393608  77 collections.OrderedDict
     5  96416   7 15708304   6 235101912  83 unicode
     6 114859   8 11291352   4 246393264  87 str
     7  99555   7  7964400   3 254357664  90 pyang.error.Position
     8  21854   1  6119120   2 260476784  92 dict of collections.OrderedDict
     9  65195   4  6029160   2 266505944  94 tuple

@glennmatthews
Copy link
Contributor Author

"make test" is passing - the Travis failure is the following, which I don't understand:

$ git ls-files . --exclude-standard --others | tee "$TMP"
.pytest_cache/v/cache/nodeids

The command "git ls-files . --exclude-standard --others | tee "$TMP"" exited with 0.

$ if test -s "$TMP"; then false; else true; fi

The command "if test -s "$TMP"; then false; else true; fi" exited with 1.

Any assistance would be appreciated.

@glennmatthews
Copy link
Contributor Author

Possibly failure may be due to pytest-dev/pytest#3286.

@mbj4668
Copy link
Owner

mbj4668 commented Apr 3, 2018

Thanks for this PR! I'll play with this locally before merging.

@mbj4668 mbj4668 merged commit 1ed835d into mbj4668:master Apr 10, 2018
@glennmatthews glennmatthews deleted the memory-slimming branch April 10, 2018 13:13
@glennmatthews
Copy link
Contributor Author

Thank you Martin!

@mbj4668 mbj4668 mentioned this pull request Dec 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants