Permalink
Browse files

Parser optimization: compare Id instances by object identity.

Id.__eq__ was near the top of the profile, so we don't want it to be
implemented in Python.

Also:

- Count generated code correctly now that we've moved things.
  • Loading branch information...
Andy Chu
Andy Chu committed Dec 20, 2017
1 parent 2882a57 commit 18a17edf57bcd58756c594226be9dd22a40cb3e3
Showing with 10 additions and 15 deletions.
  1. +5 −10 core/id_kind.py
  2. +2 −3 core/id_kind_test.py
  3. +2 −1 native/fastlex_test.py
  4. +1 −1 scripts/count.sh
View
@@ -24,7 +24,11 @@ def IdName(id_):
return _ID_NAMES[id_.enum_value]
# Save memory by keeping one instance.
# Keep one instance of each Id, to save memory and enable comparison by
# OBJECT IDENTITY.
# Do NOT create any any more instances of them! Always used IdInstance().
# TODO: Fold this into ASDL, which will enforce this?
_ID_INSTANCES = {} # int -> Id
@@ -44,15 +48,6 @@ class Id(object):
def __init__(self, enum_value):
self.enum_value = enum_value
def __eq__(self, other):
return self.enum_value == other.enum_value
def __ne__(self, other):
return self.enum_value != other.enum_value
def __hash__(self):
return hash(self.enum_value)
def __repr__(self):
return IdName(self)
View
@@ -61,9 +61,8 @@ def testTokens(self):
self.assertEqual(Kind.BoolBinary, LookupKind(t.id))
def testEquality(self):
# OK WTF!!!!
left = Id(198)
right = Id(198)
left = id_kind.IdInstance(198)
right = id_kind.IdInstance(198)
print(left, right)
print(left == right)
self.assertEqual(left, right)
View
@@ -11,6 +11,7 @@
import unittest
from core import id_kind
from core.id_kind import Id
from osh import ast_ as ast
@@ -21,7 +22,7 @@
def MatchToken(lex_mode, line, start_pos):
tok_type, end_pos = fastlex.MatchToken(lex_mode.enum_id, line, start_pos)
return Id(tok_type), end_pos
return id_kind.IdInstance(tok_type), end_pos
def TokenizeLineOuter(line):
View
@@ -49,7 +49,7 @@ all() {
echo
echo 'GENERATED CODE'
wc -l _devbuild/*.py _devbuild/gen/* | sort --numeric
wc -l _devbuild/gen/*.{py,h} | sort --numeric
echo
echo 'TOOLS'

0 comments on commit 18a17ed

Please sign in to comment.