Advertisement
Guest User

Strange slowness for keyword-lookup in records

a guest
Sep 27th, 2013
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. user> (use 'criterium.core)
  2. nil
  3. user> (defrecord Foo [a b])
  4. user.Foo
  5. user> (def r (Foo. 1 2))
  6. #'user/r
  7. user> (:a r)
  8. 1
  9. user> (:a r 3)
  10. 1
  11. user> (bench (:a r))
  12. Evaluation count : 5918775180 in 60 samples of 98646253 calls.
  13.              Execution time mean : 8.016731 ns
  14.              ...
  15. user> (bench (:a r 3))
  16.              Execution time mean : 32.933088 ns                 ;; <= 4X higher than with the not-found arg!
  17.              ...
  18. user> (.valAt ^clojure.lang.ILookup r :a)
  19. 1
  20. user> (.valAt ^clojure.lang.ILookup r :a 3)
  21. 1
  22. user> (bench (.valAt ^clojure.lang.ILookup r :a))
  23.              Execution time mean : 7.979451 ns
  24.              ...
  25. user> (bench (.valAt ^clojure.lang.ILookup r :a 3))
  26.              Execution time mean : 7.368049 ns                   ;; <= No performance difference here
  27.              ...
  28.  
  29.  
  30. ;; And yet this is the implementation of Keyword.invoke:
  31. final public Object invoke(Object obj) {
  32.     if(obj instanceof ILookup)
  33.         return ((ILookup)obj).valAt(this);
  34.     return RT.get(obj, this);
  35. }
  36.  
  37. final public Object invoke(Object obj, Object notFound) {
  38.     if(obj instanceof ILookup)
  39.         return ((ILookup)obj).valAt(this,notFound);
  40.     return RT.get(obj, this, notFound);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement