Use __slots__ for fast attribute access during parsing

This commit is contained in:
Massimiliano Culpo 2022-06-07 17:51:16 +02:00 committed by Todd Gamblin
parent 57e6452831
commit b6c8779772
2 changed files with 8 additions and 0 deletions

View File

@ -17,6 +17,8 @@
class Token(object):
"""Represents tokens; generated from input by lexer and fed to parse()."""
__slots__ = 'type', 'value', 'start', 'end'
def __init__(self, type, value="", start=0, end=0):
self.type = type
self.value = value
@ -39,6 +41,8 @@ def __eq__(self, other):
class Lexer(object):
"""Base class for Lexers that keep track of line numbers."""
__slots__ = 'scanner0', 'scanner1', 'mode', 'mode_switches_01', 'mode_switches_10'
def __init__(self, lexicon0, mode_switches_01=[], lexicon1=[], mode_switches_10=[]):
self.scanner0 = re.Scanner(lexicon0)
self.mode_switches_01 = mode_switches_01
@ -89,6 +93,8 @@ def lex(self, text):
class Parser(object):
"""Base class for simple recursive descent parsers."""
__slots__ = 'tokens', 'token', 'next', 'lexer', 'text'
def __init__(self, lexer):
self.tokens = iter([]) # iterators over tokens, handled in order.
self.token = Token(None) # last accepted token

View File

@ -4983,6 +4983,8 @@ def __init__(self):
class SpecParser(spack.parse.Parser):
"""Parses specs."""
__slots__ = 'previous', '_initial'
def __init__(self, initial_spec=None):
"""Construct a new SpecParser.