Python3.10.5 Pattern Matching Excessive Memory Consumption

tom-p-reichel · GitHub

Crash report

While writing some code to match a particularly complicated AST, I accidentally wrote a function whose definition causes the Python 3.10.5 interpreter to rapidly consume memory. On my system, it consumed approximately 24 GBs of memory before being killed.

The function is as follows:

defdefines(self): matchself.ast: case [S('CoqAst'), [[S('v'), [_, _, [S('expr'), [S('VernacDefinition'), [S('NoDischarge'), S('Definition')], [[[S('v'), [S('Name'), [S('Id'), S(name)]]], _], []], [S('DefineBody'), [], [], [_, _], []]]]]], _]] | [S('CoqAst'), [[S('v'), [[S('control'), []], [S('attrs'), []], [S('expr'), [S('VernacStartTheoremProof'), S('Theorem'), [[[[[S('v'), [S('Id'), name]], _], []], _]]]]]], _]]: returnnameTo replicate, paste the above function into a python file and run it.

Error messages

OOM killing produced no output, save for Killed.

Your environment

CPython versions tested on: 3.10.5

Operating system and architecture: Tested on both Windows 10 and ArchLinux.