Skip to content

Commit

Permalink
Improve parse tree translation performance.
Browse files Browse the repository at this point in the history
Removed use of Python constructors due to high calling overheads. Instead, use pure __new__ object construction and assign class attributes manually.
Also caching more python objects to keep Python C api usage lower.
  • Loading branch information
amykyta3 committed Jan 13, 2020
1 parent 02ed9d9 commit 268d6ad
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 96 deletions.
9 changes: 7 additions & 2 deletions docs/example.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ sa_mygrammar.py

`src/spam/parser/sa_mygrammar.py <https://github.com/amykyta3/speedy-antlr-example/blob/master/src/spam/parser/sa_mygrammar.py>`_

TODO
This module provides the entry-point for the C++ based parser, as well as a
pure Python fall-back implementation. When calling the ``parse()`` function,
the fall-back implementation is automatically used if the C++ version failed to
install.


print_tree.py
Expand Down Expand Up @@ -89,7 +92,9 @@ setup.py

`setup.py <https://github.com/amykyta3/speedy-antlr-example/blob/master/setup.py>`_

TODO
This example setup script shows how to gracefully omit the C++ accelerator if
it fails to build. Recall from earlier, if the extension is not avialable, the
``parse()`` wrapper function will automatically choose the Python equivalent.


LICENSE-3RD-PARTY
Expand Down
8 changes: 1 addition & 7 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ This tool generates a Python extension that runs your parser using Antlr's C++
target, and then translates the parsed tree back into Python.

Performance is highly dependant on the complexity of the grammar. Depending on
the test input used, parse speed improved by 5x to 25x.
the test input used, parse speed can be improved by 5x to 25x.

Installing
----------
Expand All @@ -25,9 +25,3 @@ Install from `PyPi`_ using pip
python3 -m pip install speedy-antlr-tool
.. _PyPi: https://pypi.org/project/speedy-antlr-tool


How It Works
------------

TODO: Write this
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"jinja2",
],
classifiers=(
"Development Status :: 4 - Beta",
"Development Status :: 5 - Production/Stable",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.5",
Expand All @@ -47,4 +47,4 @@
"Source": "https://github.com/amykyta3/speedy-antlr-tool",
"Tracker": "https://github.com/amykyta3/speedy-antlr-tool/issues",
},
)
)
2 changes: 1 addition & 1 deletion speedy_antlr_tool/__about__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.0"
__version__ = "1.0.0"
3 changes: 3 additions & 0 deletions speedy_antlr_tool/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jinja2 as jj

from .extractor import extract
from .__about__ import __version__

def write_cpp_files(grammar_name:str, context_data:str, output_dir:str):
loader = jj.FileSystemLoader(os.path.join(os.path.dirname(__file__), "templates"))
Expand All @@ -15,6 +16,7 @@ def write_cpp_files(grammar_name:str, context_data:str, output_dir:str):
context = {
"grammar_name": grammar_name,
"context_data": context_data,
"__version__": __version__,
}

# Write out main module source
Expand Down Expand Up @@ -59,6 +61,7 @@ def write_py_files(grammar_name:str, context_data:str, output_dir:str):
context = {
"grammar_name": grammar_name,
"context_data": context_data,
"__version__": __version__,
}

# Write out python file
Expand Down
2 changes: 1 addition & 1 deletion speedy_antlr_tool/templates/sa_X.pyt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file was auto-generated by speedy-antlr-tool
# This file was auto-generated by speedy-antlr-tool v{{__version__}}
# https://github.com/amykyta3/speedy-antlr-tool

import sys
Expand Down
3 changes: 2 additions & 1 deletion speedy_antlr_tool/templates/sa_X_cpp_parser.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* This file was auto-generated by speedy-antlr-tool
* This file was auto-generated by speedy-antlr-tool v{{__version__}}
* https://github.com/amykyta3/speedy-antlr-tool
*/

Expand Down Expand Up @@ -150,3 +150,4 @@ PyInit_sa_{{grammar_name|lower}}_cpp_parser(void) {
PyObject *m = PyModule_Create(&module);
return m;
}

9 changes: 6 additions & 3 deletions speedy_antlr_tool/templates/sa_X_translator.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* This file was auto-generated by speedy-antlr-tool
* This file was auto-generated by speedy-antlr-tool v{{__version__}}
* https://github.com/amykyta3/speedy-antlr-tool
*/

Expand All @@ -11,6 +11,9 @@ SA_{{grammar_name}}Translator::SA_{{grammar_name}}Translator(speedy_antlr::Trans
}

SA_{{grammar_name}}Translator::~SA_{{grammar_name}}Translator() {
{%- for d in context_data if not d.is_label_parent %}
Py_XDECREF({{d.Rule_name}}Context_cls);
{%- endfor %}
}

{% for d in context_data if not d.is_label_parent %}
Expand All @@ -22,8 +25,8 @@ antlrcpp::Any SA_{{grammar_name}}Translator::visit{{d.Rule_name}}({{grammar_name
{%- endfor %}
};
{%- endif %}
PyObject *py_ctx = translator->convert_ctx(this, ctx, "{{d.ctx_classname}}"
{%- if d.is_label_ctx %}, "{{d.label_ctx_classname}}"{% else %}, nullptr{% endif %}
if(!{{d.Rule_name}}Context_cls) {{d.Rule_name}}Context_cls = PyObject_GetAttrString(translator->parser_cls, "{{d.Rule_name}}Context");
PyObject *py_ctx = translator->convert_ctx(this, ctx, {{d.Rule_name}}Context_cls
{%- if d.labels %}, labels, {{d.labels|length}}{% endif %});
return py_ctx;
}
Expand Down
7 changes: 6 additions & 1 deletion speedy_antlr_tool/templates/sa_X_translator.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* This file was auto-generated by speedy-antlr-tool
* This file was auto-generated by speedy-antlr-tool v{{__version__}}
* https://github.com/amykyta3/speedy-antlr-tool
*/

Expand All @@ -11,6 +11,11 @@
class SA_{{grammar_name}}Translator : public {{grammar_name}}BaseVisitor {
speedy_antlr::Translator *translator;

// Cached context classes
{%- for d in context_data if not d.is_label_parent %}
PyObject *{{d.Rule_name}}Context_cls = NULL;
{%- endfor %}

public:
SA_{{grammar_name}}Translator(speedy_antlr::Translator *translator);
~SA_{{grammar_name}}Translator();
Expand Down
Loading

0 comments on commit 268d6ad

Please sign in to comment.