Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline locals that refer to constants #485

Closed
borkdude opened this issue Dec 21, 2020 · 1 comment
Closed

Inline locals that refer to constants #485

borkdude opened this issue Dec 21, 2020 · 1 comment

Comments

@borkdude
Copy link
Collaborator

borkdude commented Dec 21, 2020

E.g.:

(let [x 1] (+ x 1 2))

can be simplified to (+ 1 1 2)

We will have to benchmark if checking constants at analysis time outweighs the cost of not doing this and always looking up in the local environment for typical programs.

@borkdude
Copy link
Collaborator Author

borkdude commented Dec 21, 2020

A quick analysis shows that less than 1% of let binding vectors have some constant. It may not be worth the trouble.

;; clj -Sdeps "{:deps {borkdude/grasp {:git/url \"https://github.com/borkdude/grasp\" :sha \"6315cea8c0b6dafc7b1eef9ccd03d6d5590d2045\"}}}"

(require '[clojure.spec.alpha :as s]
         '[grasp.api :as g])

(defn constant? [x]
  (or (number? x) (string? x) (keyword? x) (boolean? x)))

(def lets (g/grasp "." (g/seq 'let
                              vector?
                              (s/* any?))))

(def with-constants (count (filter #(some constant? %)
                                   (map second lets))))

(def without-constants (count (filter #(not (some constant? %))
                                      (map second lets))))

(prn with-constants without-constants)
769 123615

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant