summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Behnel <stefan_ml@behnel.de>2020-05-29 12:42:13 +0200
committerStefan Behnel <stefan_ml@behnel.de>2020-05-29 13:03:49 +0200
commit447445a3f4c651c2881bc5b50b8d3a1b3fc6b7a6 (patch)
tree056618e85e0ab11c51a6b9fab90b69a110320620
parentaa4273dbf1f517f208878d206e211434fe72131d (diff)
downloadcython-447445a3f4c651c2881bc5b50b8d3a1b3fc6b7a6.tar.gz
Speed up Machines.py by compiling it.
See https://github.com/cython/cython/issues/3646 Supersedes https://github.com/cython/cython/pull/3649
-rw-r--r--Cython/Plex/Machines.pxd5
-rw-r--r--Cython/Plex/Machines.py32
-rwxr-xr-xsetup.py11
3 files changed, 26 insertions, 22 deletions
diff --git a/Cython/Plex/Machines.pxd b/Cython/Plex/Machines.pxd
new file mode 100644
index 000000000..55a4f7b95
--- /dev/null
+++ b/Cython/Plex/Machines.pxd
@@ -0,0 +1,5 @@
+cdef class FastMachine:
+ cdef readonly dict initial_states
+ cdef readonly dict new_state_template
+ cdef readonly list states
+ cdef readonly Py_ssize_t next_number
diff --git a/Cython/Plex/Machines.py b/Cython/Plex/Machines.py
index 2d19e8dfc..f3a27e574 100644
--- a/Cython/Plex/Machines.py
+++ b/Cython/Plex/Machines.py
@@ -5,6 +5,7 @@ Classes for building NFAs and DFAs
"""
from __future__ import absolute_import
+import cython
from .Transitions import TransitionMap
try:
@@ -12,10 +13,11 @@ try:
except ImportError:
from sys import maxint
-try:
- unichr
-except NameError:
- unichr = chr
+if not cython.compiled:
+ try:
+ unichr
+ except NameError:
+ unichr = chr
LOWEST_PRIORITY = -maxint
@@ -131,17 +133,13 @@ class FastMachine(object):
FastMachine is a deterministic machine represented in a way that
allows fast scanning.
"""
- initial_states = None # {state_name:state}
- states = None # [state] where state = {event:state, 'else':state, 'action':Action}
- next_number = 1 # for debugging
-
- new_state_template = {
- '': None, 'bol': None, 'eol': None, 'eof': None, 'else': None
- }
-
def __init__(self):
- self.initial_states = {}
- self.states = []
+ self.initial_states = {} # {state_name:state}
+ self.states = [] # [state] where state = {event:state, 'else':state, 'action':Action}
+ self.next_number = 1 # for debugging
+ self.new_state_template = {
+ '': None, 'bol': None, 'eol': None, 'eof': None, 'else': None
+ }
def __del__(self):
for state in self.states:
@@ -159,6 +157,7 @@ class FastMachine(object):
def make_initial_state(self, name, state):
self.initial_states[name] = state
+ @cython.locals(code0=cython.long, code1=cython.long, maxint=cython.long, state=dict)
def add_transitions(self, state, event, new_state, maxint=maxint):
if type(event) is tuple:
code0, code1 = event
@@ -210,9 +209,7 @@ class FastMachine(object):
if char_list:
ranges = self.chars_to_ranges(char_list)
ranges_to_state[ranges] = state
- ranges_list = ranges_to_state.keys()
- ranges_list.sort()
- for ranges in ranges_list:
+ for ranges in sorted(ranges_to_state):
key = self.ranges_to_string(ranges)
state = ranges_to_state[ranges]
file.write(" %s --> State %d\n" % (key, state['number']))
@@ -221,6 +218,7 @@ class FastMachine(object):
if state:
file.write(" %s --> State %d\n" % (key, state['number']))
+ @cython.locals(char_list=list, i=cython.Py_ssize_t, n=cython.Py_ssize_t, c1=cython.long, c2=cython.long)
def chars_to_ranges(self, char_list):
char_list.sort()
i = 0
diff --git a/setup.py b/setup.py
index 684bd64e6..4351db327 100755
--- a/setup.py
+++ b/setup.py
@@ -84,6 +84,7 @@ def compile_cython_modules(profile=False, compile_more=False, cython_with_refnan
compiled_modules = [
"Cython.Plex.Scanners",
"Cython.Plex.Actions",
+ "Cython.Plex.Machines",
"Cython.Compiler.Scanning",
"Cython.Compiler.Visitor",
"Cython.Compiler.FlowControl",
@@ -236,24 +237,24 @@ def run_build():
The Cython language makes writing C extensions for the Python language as
easy as Python itself. Cython is a source code translator based on Pyrex_,
but supports more cutting edge functionality and optimizations.
-
+
The Cython language is a superset of the Python language (almost all Python
code is also valid Cython code), but Cython additionally supports optional
static typing to natively call C functions, operate with C++ classes and
declare fast C types on variables and class attributes. This allows the
compiler to generate very efficient C code from Cython code.
-
+
This makes Cython the ideal language for writing glue code for external
C/C++ libraries, and for fast C modules that speed up the execution of
Python code.
-
+
Note that for one-time builds, e.g. for CI/testing, on platforms that are not
covered by one of the wheel packages provided on PyPI *and* the pure Python wheel
that we provide is not used, it is substantially faster than a full source build
to install an uncompiled (slower) version of Cython with::
-
+
pip install Cython --install-option="--no-cython-compile"
-
+
.. _Pyrex: https://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/
"""),
license='Apache',