Improving the performance of "Rule Expressions" #2801
Replies: 8 comments 4 replies
-
?? There is exactly one jython script thread per display pane so that scripts for multiple displays can run in parallel while having only one per display to avoid issues with 'widget' or 'pvs' getting clobbered. |
Beta Was this translation helpful? Give feedback.
-
This might be my fault, I was under the impression that in order to be allowed to access widget properties and modify them the scripts need to run on the UI thread too. |
Beta Was this translation helpful? Give feedback.
-
In BOY, scripts were initialized and executed on the UI thread, which caused 1..3 second freeze when opening a display that contains scripts. If you want to call JavaFX UI code from the script, you do need to use |
Beta Was this translation helpful? Give feedback.
-
@kasemir: Thanks for the correction. Are you aware of any performance problems with the execution of the scripts that rules generate? If so, would it make sense to write a interpreter in Java (avoiding the overhead of Jython) for "Rule Expressions" in order to achieve improved performance? |
Beta Was this translation helpful? Give feedback.
-
Based on my (not very recent) analysis the performance penalty is with the loading/reloading of the OPIs when rules are converted and compiled. As for using a cache for rules to avoid the conversion/compilation step... In my view that is a bandaid. Let's spend resources to find a better solution, even at the potential cost of dealing with backwards compatibility. I do not believe a cache would be super trivial, e.g. it would need to update or flush when user has edited an OPI. As for the size of a cache... I have seen cases where a single OPI contains several hundreds of rules. |
Beta Was this translation helpful? Give feedback.
-
I agree that ensuring that the values in the cache are not out-of-date could potentially be tricky. There are two separate "issues" with the current Rule Expressions, I think:
If we want to solve 1. and 2. by replacing Rule Expressions with Formula Expressions, I don't think we should introduce this change from one release to another, since it would break backwards compatibility. I think that we would need to manage the transition well, due to the number of OPIs in production that use Rule Expressions. If we proceed along this path, then I suggest that we allow for both Rule Expressions and Formula Expressions in Rules during a transition period, during which Rule Expressions are marked as being deprecated, and at the end of which Rule Expressions are finally removed from the code base. However, we should, I think, also keep in mind that we can fix 1. with no impact on 2. by writing an interpreter for Rule Expressions in Java. (Or, an alternative solution could perhaps be to introduce a cache of compiled objects.) I prefer this option since it preserves backwards compatibility. |
Beta Was this translation helpful? Give feedback.
-
Frankly, no. Just like scripts, rules should be the exception. I've not seen the performance of rules or scripts as a showstopper. If your displays rely on rules and scripts, consider switching to PyDM where every display is a custom python program. I wish you the best.
I think that's correct. Here's a screenshot of jProfiler after re-loading The context menu for getting to "Reload" and then disposing the existing display take a little time.
As for the Jython compilation, I don't remember the full detail, but some evidence is left in the So there might be ways to speed up the Jython startup. Replacing the current rule implementation with something else like a Java implementation runs the risk of breaking existing displays. I'm not sure it's worth it since again rules/scripts should be the exception. |
Beta Was this translation helpful? Give feedback.
-
I'd go as far as making the formula expressions a new, separate thing.
|
Beta Was this translation helpful? Give feedback.
-
I would like to discuss two proposals for improving the performance of "Rule Expressions" in user-specified "Rules".
Alternative 1: Replace "Rule Expressions" with "Formula Expressions" as implemented in
core-formula
.Existing work. On the branch
JavaRules
(see #2786), @shroffk has implemented a replacement of "Rule Expressions" (which onmaster
currently are implemented by a compilation into Python scripts which are subsequently evaluated using the Jython interpreter on the UI thread) with "Formula Expressions" (which are evaluated by an interpreter written in Java that is located incore-formula
). The original implementation is from 2019, and I have added some commits to this branch to make it compile when merged with the currentmaster
branch. However, the commits I have made were done without understanding the implementation, so they almost certainly break things.Advantages and disadvantages of replacing "Rule Expressions" with "Formula Expressions". Replacing the current implementation of "Rule Expressions" with "Formula Expressions" implemented in
core-formula
has several advantages:core-formula
is written in Java, and presumably provides significant performance benefits over the current implementation using Jython for two reasons:There are also disadvantages to a replacement of Rule Expressions with Formula Expressions:
pv0
,pv1
, ... . Since PV names tend to be long, formulas that contain PV names are difficult to read and modify.A further point that has to be taken into account if we were to replace Rule Expressions with Formula Expressions is:
Changes to be implemented under alternative 1. The following two changes would be implemented:
JavaRules
branch, but with a deterministic semantics as described in P1.Breaking the backwards compatibility is of course still a concern (D1). I think that during a transition period, we should allow both "Rule Expressions" and "Formula Expressions", and then remove "Rule Expressions" after the transition period. One idea could be that we add a button "Translate to Formula Expressions" to the "Rules"-dialog, with which a user can automatically convert at least a large subset of "Rule Expressions into "Formula Expressions". After the transition period, the functionality to run "Rule Expressions" would be removed. We should also display warnings in appropriate places, that warn the user that "Rule Expressions" are being deprecated.
Alternative 2: Improving the performance of the current implementation.
It seems plausible that the advantages A3.1 and (to some extent) A3.2 can be obtained by the following two changes, which do not incur the disadvantages D1 and D2 (but also does not result in the advantages A1 and A2):
These two changes have the property that they provide advantages without any disadvantages: the current implementation of Rules would presumably be sped up, without any other noticeable changes. At least conceptually, it seems likely that these two changes should be relatively easy to implement.
Relevant GitHub Issues and Pull-Request. The following two GitHub Issues and Pull-Request may be relevant for this discussion:
JavaRules
: Java rules #2786Beta Was this translation helpful? Give feedback.
All reactions