https://kotlinlang.org logo
#mathematics
Title
# mathematics
a

altavir

07/29/2020, 10:03 AM
Here is the promised article by @Iaroslav Postovalov about AST-based optimization of kmath expressions during runtime: https://medium.com/@postovalovya/expression-optimization-with-jvm-bytecode-generation-b50304b96619
👍 4
i

Ilya Muradyan

07/29/2020, 10:43 AM
Looks great! Is it possible to get a working Class<*> instance by giving it an expression string? I'd like to try it out Some points: 1. You may try to generate "primitive" versions of Expression and Algebra (Instead of using generics) interfaces to avoid boxing issues and performance regression 2. Operations seem to fit enum type naturally. You won't need using string identifiers and map lookup, just define enum with appropriate methods Also, it would be great to see more detailed performance analysis, with different optimisations enabled and with different sizes of expressions.
a

altavir

07/29/2020, 10:44 AM
I believe @Iaroslav Postovalov did all that, he can address the question. There are benchmarks in the repository as well.
An important thing that I expected and @Iaroslav Postovalov confirmed is that JIT is almost as effective as byte-code generation when all types are known in advance and we do not generate nested functions.
i

Iaroslav Postovalov

07/29/2020, 10:53 AM
@Ilya Muradyan hello, we actually get class object and provide its instance to the user
@Ilya Muradyan boxing issue is partially optimized by inserting calls to primitive methods, not to bridge methods, so only the return value is being boxed. It's nice idea to create specialized methods for double/int/long
@Ilya Muradyan I'm not good in building complex performance comparisons and distributions.
@Ilya Muradyan string operations IDs are designed at core module. however, it's possible to replace methodNameAdapters with enum, thanks for advice.
i

Ilya Muradyan

07/29/2020, 11:06 AM
Thank you for answers! One more q: how many arguments map access operations will be performed for expression "x*x*x"? One or three?
a

altavir

07/29/2020, 11:08 AM
I believe that it will be a tree with 3 nodex, each of those use 2-argument primitive operation. In AST the tree will be traversed and flattened.
i

Iaroslav Postovalov

07/29/2020, 11:10 AM
I think, 3
Thanks for idea of optimization
@Ilya Muradyan but there is a possible problem. map can be mutable or return random values
I don't want to make complicated contract for input map
i

Ilya Muradyan

07/29/2020, 11:22 AM
Different values for the same key? This contract seems to be natural, but I agree, there could the cases
i

Iaroslav Postovalov

07/29/2020, 11:23 AM
Yes. Optimizations like this must be opt-in, and during profiling I didn't noticed any problems because of HashMap.get
a

altavir

07/29/2020, 11:25 AM
HashMap.get is super-optimized on JVM, so the access cost is close to the array access. It should be profiled of course.
i

Iaroslav Postovalov

07/29/2020, 11:27 AM
I'll review x*x*x expression.
a

altavir

07/29/2020, 11:45 AM
The MST interpretter actually allows caching the value of the variable. But I do not think it is relevant right now.
3 Views