GithubHelp home page GithubHelp logo

fast-case about clj-fast HOT 8 OPEN

joinr avatar joinr commented on June 11, 2024
fast-case

from clj-fast.

Comments (8)

bsless avatar bsless commented on June 11, 2024

Thinking about this and #9 , what do you think will be the effects of missed branch predictions on this code? Can we engineer a test case where branch prediction isn't possible to test its effects?

from clj-fast.

joinr avatar joinr commented on June 11, 2024

I'm no expert on that, let alone knowing how engineer good degenerate cases. I guess you would have random input over the cases....no idea beyond that.

from clj-fast.

bsless avatar bsless commented on June 11, 2024

I ran the following interesting test:

(defn foo [k]
  (fast-case k :a 0 :b 1 :c 2 :d 3 :e 4 :f 5 :g 6 :h 7 :i 9 :j 10 :k 11 :l 12 :m 13 :n 14 :o 15 :p 16 :q 17 :r 18 :s 19 :none))

(defn bar [k]
  (case k :a 0 :b 1 :c 2 :d 3 :e 4 :f 5 :g 6 :h 7 :i 9 :j 10 :k 11 :l
             12 :m 13 :n 14 :o 15 :p 16 :q 17 :r 18 :s 19 :none))

(def ks [:a :b :c :d :e :f :g :h :i :j :k :l :m :n :o :p :q :r :s :none])

(def lots (repeatedly 10000 #(rand-nth ks)))

(do
  (c/bench (doseq [k lots] 0))
  (c/bench (doseq [k lots] (foo k)))
  (c/bench (doseq [k lots] (bar k))))

Overhead: 571.822473 µs
Fast case: 654.098578 µs, minus overhead 85
Core case: 740.666878 µs, minus overhead 170
=> fast case is 2x faster, amortized .

from clj-fast.

joinr avatar joinr commented on June 11, 2024

Nice. I tend to use case quit a bit, as does some clojure internals like defrecord, so this seems generally useful.

from clj-fast.

bsless avatar bsless commented on June 11, 2024

I wander how critical it actually is. While identity case checks are 2x faster we're still talking about 4 vs 8 ns in the worst case scenario, not to mention I did not measure these influences in a wider context of a running application. Perhaps cache misses will degrade other parts' characteristics, so I'm still wary.
It would be interesting to get a profile of a typical application and how many times it cases on keywords.

from clj-fast.

joinr avatar joinr commented on June 11, 2024

If you're using keyword access for records, it will impact your performance. Ended up being a enough in the IFCPC optimization case (where I first ran into this, and defrecord's weaker performance compared to arraymaps..) that I found out about this. If you're not using idiomatic paths that leverage case on hot paths, it may not matter then. That can probably be applied to most of these optimizations though (e.g. context and profiling matter).

from clj-fast.

bsless avatar bsless commented on June 11, 2024

Is there a need to introduce the binding to ge and type hint it as an Object? I fail to see the ratilnale behind it. Am I missing anything?

from clj-fast.

joinr avatar joinr commented on June 11, 2024

That came from the original source in clojure.core/case. Unsure.

from clj-fast.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.