From 64992aa49cdedaffdb3a577e502816fe8c7e3f23 Mon Sep 17 00:00:00 2001 From: night199uk Date: Sat, 22 Dec 2018 08:11:51 +0100 Subject: [PATCH] Fix a performance regression on Rules related to empties --- lark/grammar.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lark/grammar.py b/lark/grammar.py index 21a62f1..53ce529 100644 --- a/lark/grammar.py +++ b/lark/grammar.py @@ -39,11 +39,14 @@ class Rule(object): origin : a symbol expansion : a list of symbols """ + __slots__ = ('origin', 'expansion', 'alias', 'options', '_hash') def __init__(self, origin, expansion, alias=None, options=None): self.origin = origin self.expansion = expansion self.alias = alias self.options = options + self._hash = hash((self.origin, tuple(self.expansion))) + def __str__(self): return '<%s : %s>' % (self.origin.name, ' '.join(x.name for x in self.expansion)) @@ -52,7 +55,8 @@ class Rule(object): return 'Rule(%r, %r, %r, %r)' % (self.origin, self.expansion, self.alias, self.options) def __hash__(self): - return hash((self.origin, tuple(self.expansion))) + return self._hash + def __eq__(self, other): if not isinstance(other, Rule): return False