Clojure

LazySeq should utilize cached hash from its underlying seq.

Details

  • Type: Enhancement Enhancement
  • Status: Open Open
  • Priority: Minor Minor
  • Resolution: Unresolved
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Environment:
    1.6.0 master SNAPSHOT
  • Patch:
    Code
  • Approval:
    Triaged

Description

Even if underlying seq contains a cached hash, LazySeq computes it every time.

user=> *clojure-version*
{:major 1, :minor 7, :incremental 0, :qualifier "master", :interim true}
user=> (def a (range 100000))
#'user/a
user=> (time (hash a))
"Elapsed time: 21.812 msecs"
375952610
user=> (time (hash a))        ;; hash is cached
"Elapsed time: 0.036 msecs"
375952610
user=> (def b (seq a))
#'user/b
user=> (time (hash b))
"Elapsed time: 0.042 msecs"   ;; uses cached hash
375952610
user=> (def c (lazy-seq b))
#'user/c
user=> (time (hash c))        ;; SHOULD use underlying hash
"Elapsed time: 27.758 msecs"
375952610
user=> (time (hash c))        ;; SHOULD use underlying hash
"Elapsed time: 17.846 msecs"
375952610

Approach: If seq produced by LazySeq implementing IHashEq, use it to calculate the hasheq().
Patch: clj-1373.diff

Activity

Hide
Jozef Wagner added a comment -

Added patch which checks if underlying seq implements IHashEq and if yes, uses that hash instead of recomputing.

Show
Jozef Wagner added a comment - Added patch which checks if underlying seq implements IHashEq and if yes, uses that hash instead of recomputing.
Hide
Alex Miller added a comment -

In this patch, can you update the else case (the original code) to use s rather than this, so seq() is not re-called?

Show
Alex Miller added a comment - In this patch, can you update the else case (the original code) to use s rather than this, so seq() is not re-called?
Hide
Jozef Wagner added a comment -

Added patch [^clj-1373-2.diff] that reuses s for else case.

Show
Jozef Wagner added a comment - Added patch [^clj-1373-2.diff] that reuses s for else case.
Hide
Alex Miller added a comment -

The -2 patch doesn't compile so I guess that was a bad suggestion.

Show
Alex Miller added a comment - The -2 patch doesn't compile so I guess that was a bad suggestion.

People

Vote (1)
Watch (3)

Dates

  • Created:
    Updated: