From ba0dc789a32fe21bb731132cec9a12b8ae6cb036 Mon Sep 17 00:00:00 2001 From: Erez Shinan Date: Sat, 7 Apr 2018 12:21:51 +0300 Subject: [PATCH] Significantly better memory performance (Thanks @drslump!) Added __slots__ to RulePtr and Token, resulting in significantly lower memory consumption. As suggested by @drslump. --- lark/lexer.py | 2 ++ lark/parsers/grammar_analysis.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/lark/lexer.py b/lark/lexer.py index bcf09f1..0a46ee1 100644 --- a/lark/lexer.py +++ b/lark/lexer.py @@ -25,6 +25,8 @@ class UnexpectedInput(LexError): self.considered_rules = considered_rules class Token(Str): + __slots__ = ('type', 'pos_in_stream', 'value', 'line', 'column', 'end_line', 'end_column') + def __new__(cls, type_, value, pos_in_stream=None, line=None, column=None): self = super(Token, cls).__new__(cls, value) self.type = type_ diff --git a/lark/parsers/grammar_analysis.py b/lark/parsers/grammar_analysis.py index 9ad49ce..5cbd697 100644 --- a/lark/parsers/grammar_analysis.py +++ b/lark/parsers/grammar_analysis.py @@ -5,6 +5,8 @@ from ..grammar import Rule class RulePtr(object): + __slots__ = ('rule', 'index') + def __init__(self, rule, index): assert isinstance(rule, Rule) assert index <= len(rule.expansion)