Commit b2afb116 authored by Stefan Behnel's avatar Stefan Behnel

Reduce lexicon build time some more by compiling Transitions.py.

parent 0cf1419b
...@@ -5,12 +5,18 @@ This version represents state sets directly as dicts for speed. ...@@ -5,12 +5,18 @@ This version represents state sets directly as dicts for speed.
""" """
from __future__ import absolute_import from __future__ import absolute_import
import cython
cython.declare(maxint=cython.long)
try: try:
from sys import maxsize as maxint from sys import maxsize as maxint
except ImportError: except ImportError:
from sys import maxint from sys import maxint
@cython.final
@cython.cclass
class TransitionMap(object): class TransitionMap(object):
""" """
A TransitionMap maps an input event to a set of states. A TransitionMap maps an input event to a set of states.
...@@ -39,23 +45,22 @@ class TransitionMap(object): ...@@ -39,23 +45,22 @@ class TransitionMap(object):
kept separately in a dictionary. kept separately in a dictionary.
""" """
map = None # The list of codes and states cython.declare(map=list, special=dict)
special = None # Mapping for special events
def __init__(self, map=None, special=None): def __init__(self, map=None, special=None):
if not map: if not map:
map = [-maxint, {}, maxint] map = [-maxint, {}, maxint]
if not special: if not special:
special = {} special = {}
self.map = map self.map = map # The list of codes and states
self.special = special self.special = special # Mapping for special events
def add(self, event, new_state, @cython.locals(i=cython.Py_ssize_t, j=cython.Py_ssize_t, map=list)
TupleType=tuple): def add(self, event, new_state):
""" """
Add transition to |new_state| on |event|. Add transition to |new_state| on |event|.
""" """
if type(event) is TupleType: if type(event) is tuple:
code0, code1 = event code0, code1 = event
i = self.split(code0) i = self.split(code0)
j = self.split(code1) j = self.split(code1)
...@@ -66,12 +71,12 @@ class TransitionMap(object): ...@@ -66,12 +71,12 @@ class TransitionMap(object):
else: else:
self.get_special(event)[new_state] = 1 self.get_special(event)[new_state] = 1
def add_set(self, event, new_set, @cython.locals(i=cython.Py_ssize_t, j=cython.Py_ssize_t, map=list)
TupleType=tuple): def add_set(self, event, new_set):
""" """
Add transitions to the states in |new_set| on |event|. Add transitions to the states in |new_set| on |event|.
""" """
if type(event) is TupleType: if type(event) is tuple:
code0, code1 = event code0, code1 = event
i = self.split(code0) i = self.split(code0)
j = self.split(code1) j = self.split(code1)
...@@ -88,8 +93,8 @@ class TransitionMap(object): ...@@ -88,8 +93,8 @@ class TransitionMap(object):
""" """
return self.special.get('') return self.special.get('')
def iteritems(self, @cython.locals(map=list, i=cython.Py_ssize_t, n=cython.Py_ssize_t, else_set=cython.bint)
len=len): def iteritems(self):
""" """
Return the mapping as an iterable of ((code1, code2), state_set) and Return the mapping as an iterable of ((code1, code2), state_set) and
(special_event, state_set) pairs. (special_event, state_set) pairs.
...@@ -116,8 +121,9 @@ class TransitionMap(object): ...@@ -116,8 +121,9 @@ class TransitionMap(object):
# ------------------- Private methods -------------------- # ------------------- Private methods --------------------
def split(self, code, @cython.ccall
len=len, maxint=maxint): @cython.locals(map=list, lo=cython.Py_ssize_t, mid=cython.Py_ssize_t, hi=cython.Py_ssize_t, code=cython.long)
def split(self, code):
""" """
Search the list for the position of the split point for |code|, Search the list for the position of the split point for |code|,
inserting a new split point if necessary. Returns index |i| such inserting a new split point if necessary. Returns index |i| such
......
...@@ -85,6 +85,7 @@ def compile_cython_modules(profile=False, compile_more=False, cython_with_refnan ...@@ -85,6 +85,7 @@ def compile_cython_modules(profile=False, compile_more=False, cython_with_refnan
"Cython.Plex.Scanners", "Cython.Plex.Scanners",
"Cython.Plex.Actions", "Cython.Plex.Actions",
"Cython.Plex.Machines", "Cython.Plex.Machines",
"Cython.Plex.Transitions",
"Cython.Compiler.Scanning", "Cython.Compiler.Scanning",
"Cython.Compiler.Visitor", "Cython.Compiler.Visitor",
"Cython.Compiler.FlowControl", "Cython.Compiler.FlowControl",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment