{"action":"opened","issue":{"url":"https://api.github.com/repos/cerner/clara-rules/issues/383","repository_url":"https://api.github.com/repos/cerner/clara-rules","labels_url":"https://api.github.com/repos/cerner/clara-rules/issues/383/labels{/name}","comments_url":"https://api.github.com/repos/cerner/clara-rules/issues/383/comments","events_url":"https://api.github.com/repos/cerner/clara-rules/issues/383/events","html_url":"https://github.com/cerner/clara-rules/issues/383","id":296533243,"number":383,"title":"Remove unused let-bindings in compiled RHS action functions","user":{"login":"WilliamParker","id":5178123,"avatar_url":"https://avatars0.githubusercontent.com/u/5178123?v=4","gravatar_id":"","url":"https://api.github.com/users/WilliamParker","html_url":"https://github.com/WilliamParker","followers_url":"https://api.github.com/users/WilliamParker/followers","following_url":"https://api.github.com/users/WilliamParker/following{/other_user}","gists_url":"https://api.github.com/users/WilliamParker/gists{/gist_id}","starred_url":"https://api.github.com/users/WilliamParker/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/WilliamParker/subscriptions","organizations_url":"https://api.github.com/users/WilliamParker/orgs","repos_url":"https://api.github.com/users/WilliamParker/repos","events_url":"https://api.github.com/users/WilliamParker/events{/privacy}","received_events_url":"https://api.github.com/users/WilliamParker/received_events","type":"User","site_admin":false},"labels":[{"id":525460927,"url":"https://api.github.com/repos/cerner/clara-rules/labels/performance-optimization","name":"performance-optimization","color":"fbca04","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-02-12T21:38:05Z","updated_at":"2018-02-12T21:38:05Z","closed_at":null,"author_association":"MEMBER","body":"Take the following rule:\r\n\r\n````\r\n(defrule cold-rule\r\n        [ColdAndWindy (= ?temperature temperature) (= ?windspeed windspeed)]\r\n        =>\r\n        (insert! (->Cold ?temperature)))\r\n````\r\n\r\nWhen we create a session with this rule and inspect the compiled RHS function, noting that the the unevaluated expression is [added as metadata to the ProductionNode](https://github.com/cerner/clara-rules/blob/0.17.0/src/main/clojure/clara/rules/compiler.clj#L1403) for [later use in durability](https://github.com/cerner/clara-rules/blob/0.17.0/src/main/clojure/clara/rules/durability.clj#L57)  :\r\n````\r\n(-> (mk-session [cold-rule]) .rulebase :alpha-roots (get ColdAndWindy) first :children first :children first meta)\r\n````\r\n\r\nwe retrieve the following:\r\n\r\n\r\n````\r\n{:action-expr\r\n (clojure.core/fn\r\n  [?__token__ ?__env__]\r\n  (clojure.core/let\r\n   [?temperature\r\n    (get-in ?__token__ [:bindings :?temperature])\r\n    ?windspeed\r\n    (get-in ?__token__ [:bindings :?windspeed])]\r\n   (do (insert! (->Cold ?temperature)))))}\r\n````\r\n\r\nNote that a let-binding for ?windspeed is created even though it is not used in the user's RHS code.  It does not appear that [the code which creates these RHS let-binding](https://github.com/cerner/clara-rules/blob/0.17.0/src/main/clojure/clara/rules/compiler.clj#L389) makes any attempt to determine what bindings are used in the RHS.  As a result unnecessary bindings are created, consuming time to create at runtime and increasing the amount of Clojure code evaluated at session creation time, thus likely increasing total session compilation time.\r\n\r\nIn order to fix this we can apply a similar fix to what was done in https://github.com/cerner/clara-rules/pull/180 for let bindings of fields that aren't actually used in constraints.  We'd basically just walk the user's RHS code and detect what bindings are present in it.  Since the bindings have to be literals in the code (rather than in outside user functions called by the RHS, where they aren't resolvable by Clojure's compiler) this can be done safely."}}
{"actor":{"display_login":"WilliamParker"}}