Decrease memory usage of pycparser by using __slots__ in AST nodes.
In general, there's no need to dynamically adjust the attributes of AST nodes.
So __slots__ seems suitable.
This reduces the memory usage of the test case reported in issue #72 from 21MB
to 17.5MB and should reduce the amount of space consumed by AST nodes in
general.
diff --git a/z.py b/z.py
index 9031e03..d881240 100644
--- a/z.py
+++ b/z.py
@@ -1,4 +1,5 @@
import sys
+from pycparser import parse_file
from pycparser.c_ast import *
from pycparser.c_parser import CParser, Coord, ParseError
from pycparser.c_lexer import CLexer
@@ -74,12 +75,33 @@
self.current_parent = oldparent
+def memprofile():
+ import resource
+ import tracemalloc
+
+ tracemalloc.start()
+
+ ast = parse_file('/tmp/197.c')
+
+ print('Memory usage: %s (kb)' %
+ resource.getrusage(resource.RUSAGE_SELF).ru_maxrss)
+
+ snapshot = tracemalloc.take_snapshot()
+ print("[ tracemalloc stats ]")
+ for stat in snapshot.statistics('lineno')[:20]:
+ print(stat)
+
+
if __name__ == "__main__":
source_code = r'''void foo() {
L"hi" L"there";
}
'''
- parser = CParser()
- ast = parser.parse(source_code, filename='zz')
- ast.show(showcoord=True, attrnames=True, nodenames=True)
+ memprofile()
+
+ #parser = CParser()
+ #ast = parser.parse(source_code, filename='zz')
+ #ast.show(showcoord=True, attrnames=True, nodenames=True)
+
+