Uploaded image for project: 'ClojureScript'
  1. CLJS-2931

Collection hashers should use iterators

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Minor
    • Resolution: Unresolved
    • Affects versions: None
    • Fix versions: None
    • Labels:
    • Patch:
      Code

      Description

      In CLJS hash-ordered-coll and hash-unordered-coll currently consume their collections using seq instead of an iterator. Most core collection types have an efficient -iterator implementation which avoids the extra allocations inherent in seq.

      Using iterators instead shows a modest performance improvement with larger collections and more frequent iterations; benchmark-runner size (1000) and repetition count (100) shows no perceptible difference on my machine (Ubuntu 18.04 using "Intel(R) Core(TM) i7-4710MQ CPU @ 2.50GHz")

      cljs.benchmark-runner
      (ns cljs.benchmark-runner
        (:refer-clojure :exclude [println]))
      
      (def println print)
      
      (set! *print-fn* js/print)
      
      
      ;; Note that coll size and repetition count are both
      ;; 10x what benchmark-runner usually uses. 
      (def hash-coll-test
        (loop [i 0 r []]
          (if (< i 10000)
            (recur (inc i) (conj r (str "foo" i)))
            r)))
      (def hash-imap-test
        (loop [i 0 r {}]
          (if (< i 10000)
            (recur (inc i) (conj r [(keyword (str "foo" i)) i]))
            r)))
      (def hash-imap-int-test
        (loop [i 0 r {}]
          (if (< i 10000)
            (recur (inc i) (conj r [i i]))
            r)))
      
      ## benchmark run on Ubuntu 18.04 using "Intel(R) Core(TM) i7-4710MQ CPU @ 2.50GHz"
      ## benchmark results for master
      Benchmarking with V8
      [coll hash-coll-test], (hash-ordered-coll coll), 1000 runs, 1831 msecs
      [coll hash-imap-test], (hash-unordered-coll coll), 1000 runs, 6506 msecs
      [coll hash-imap-int-test], (hash-unordered-coll coll), 1000 runs, 8439 msecs
      
      ## benchmark results with this patch
      Benchmarking with V8
      [coll hash-coll-test], (hash-ordered-coll coll), 1000 runs, 1629 msecs
      [coll hash-imap-test], (hash-unordered-coll coll), 1000 runs, 5753 msecs
      [coll hash-imap-int-test], (hash-unordered-coll coll), 1000 runs, 6810 msecs
      

        Attachments

          Activity

            People

            • Assignee:
              dnolen David Nolen
              Reporter:
              favila Francis Avila
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated: