Browse Source

Significantly better memory performance (Thanks @drslump!)

Added __slots__ to RulePtr and Token, resulting in significantly lower memory consumption.

As suggested by @drslump.
tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.6.0
Erez Shinan 6 years ago
parent
commit
ba0dc789a3
2 changed files with 4 additions and 0 deletions
  1. +2
    -0
      lark/lexer.py
  2. +2
    -0
      lark/parsers/grammar_analysis.py

+ 2
- 0
lark/lexer.py View File

@@ -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_


+ 2
- 0
lark/parsers/grammar_analysis.py View File

@@ -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)


Loading…
Cancel
Save