Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Refactor Parser to allow for further optimization. One issue of the old version is its types have different sizes. That makes it hard to swap nodes that are different types. In the current version, even though the types still have different sizes, the malloc calls allocate enough memory so that we can cast from one type to another. Another issue is that there are a lot more AST node types in the old version than in the current versions. Many of them are introduced for optimization. In the current version, a lot of AST node types including subtraction have been removed. So
(- a b)
(i.e.,a-b
) now becomes(+ a (* -1 b))
. The seems less efficient. However this makes it much easier to optimize the AST because of the associative property of add. Note that after the AST is generated there is a compilation step that flattens the AST into a sequence of instructions. The removed types (e.g., sub) and special instructions are now added in the compilation step. The new version could be significantly faster for many non-simplifed expressions. For example,sin(x) + y**2*y*sin(x)
is about 2x faster, because the new version optimizes it to(1+y**2)*sin(x)
. However, there could be no performance improvement if the expression is already simplified.In addition to the refactor and optimization, we have also added a few new math functions, atan2, asinh, acosh, and atanh, for completeness.
I have tested the new version with thousands of expressions.
The new version will have roundoff errors with the old version.