<commit_before>(ns cljs.lean-map.bench
  (:require [cljs.lean-map.core :as lean]))

(def println print)

(set! *print-fn* js/print)

(def small-map-size 100)
(def small-map-sample 2000)

(def medium-map-size 4000)
(def medium-map-sample 50)

(def large-map-size 10000)
(def large-map-sample 20)

(def size->sample
  {small-map-size small-map-sample
   medium-map-size medium-map-sample
   large-map-size large-map-sample})

(declare test-keys)

(defn sized-map [m n]
  (loop [m m i 0]
    (if (< i n)
      (recur (assoc m (get test-keys i) i) (inc i))
      m)))

(defmulti map-bench
  (fn [benchmark _ _] benchmark))

(defmethod map-bench :assoc [_ m _]
  (println "Assoc")
  (doseq [[size sample] size->sample]
    (simple-benchmark [] (sized-map m size) sample)))

(defmethod map-bench :dissoc [_ _ {:keys [small-map medium-map large-map]}]
  (println "Dissoc")
  (let [maps [small-map medium-map large-map]
        samples [small-map-sample medium-map-sample large-map-sample]]
    (doseq [[m sample] (partition 2 (interleave maps samples))]
     (simple-benchmark
       []
       (loop [m m i (count m)]
         (when (> i 0)
           (recur (dissoc m (get test-keys i)) (dec i))))
       sample))))

(defmethod map-bench :dissoc-fail [_ _ {:keys [small-map medium-map large-map]}]
  (println "Dissoc Fail")
  (let [maps [small-map medium-map large-map]
        samples [small-map-sample medium-map-sample large-map-sample]]
    (doseq [[m sample] (partition 2 (interleave maps samples))]
      (simple-benchmark
        []
        (loop [m m i (count m)]
          (when (> i 0)
            (recur (dissoc m i) (dec i))))
        sample))))

(defmethod map-bench :hash [_ _ {:keys [small-map medium-map large-map]}]
  (println "Hash")
  (let [maps [small-map medium-map large-map]
        samples [small-map-sample medium-map-sample large-map-sample]]
    (doseq [[m sample] (partition 2 (interleave maps samples))]
      (simple-benchmark [] (hash-unordered-coll m) sample))))

(defmethod map-bench :equals [_ _ {:keys [small-map medium-map large-map]}]
  (println "Equals")
  (let [maps [small-map medium-map large-map]
        samples [small-map-sample medium-map-sample large-map-sample]]
    (doseq [[m sample] (partition 2 (interleave maps samples))]
      (let [cm (clone m)]
        (simple-benchmark [] (= m cm) sample)))))

(defmethod map-bench :equals-fail [_ _ {:keys [small-map medium-map large-map]}]
  (println "Equals fail")
  (let [maps [small-map medium-map large-map]
        samples [small-map-sample medium-map-sample large-map-sample]]
    (doseq [[m sample] (partition 2 (interleave maps samples))]
      (let [neq-m (assoc m (js-obj) 0)]
        (simple-benchmark [] (= m neq-m) sample)))))

(defmethod map-bench :sequence [_ _ {:keys [small-map medium-map large-map]}]
  (println "sequence")
  (let [maps [small-map medium-map large-map]
        samples [small-map-sample medium-map-sample large-map-sample]]
    (doseq [[m sample] (partition 2 (interleave maps samples))]
      (simple-benchmark [] (doseq [[k v] m] [v k]) sample))))

(defmethod map-bench :reduce [_ _ {:keys [small-map medium-map large-map]}]
  (println "Reduce")
  (let [maps [small-map medium-map large-map]
        samples [small-map-sample medium-map-sample large-map-sample]]
    (doseq [[m sample] (partition 2 (interleave maps samples))]
      (simple-benchmark [] (reduce (fn [sum [k v]] (+ 1 sum)) 0 m) sample))))

(defn run-benchmarks [benchmarks empty-map data]
  (println "Running Benchmarks")
  (doseq [benchmark benchmarks]
    (map-bench benchmark empty-map data)))

(def lem (.-EMPTY PersistentHashMap))
(def cem (.-EMPTY cljs.core/PersistentHashMap))

(def test-keys (mapv (fn [i]
                       (let [nk (keyword (str "key" i))]
                         (hash nk)
                         nk))<commit_msg>Correct benchmark PersistentHashMap to use lean map<commit_after>(ns cljs.lean-map.bench
  (:require [cljs.lean-map.core :as lean]))

(def println print)

(set! *print-fn* js/print)

(def small-map-size 100)
(def small-map-sample 2000)

(def medium-map-size 4000)
(def medium-map-sample 50)

(def large-map-size 10000)
(def large-map-sample 20)

(def size->sample
  {small-map-size small-map-sample
   medium-map-size medium-map-sample
   large-map-size large-map-sample})

(declare test-keys)

(defn sized-map [m n]
  (loop [m m i 0]
    (if (< i n)
      (recur (assoc m (get test-keys i) i) (inc i))
      m)))

(defmulti map-bench
  (fn [benchmark _ _] benchmark))

(defmethod map-bench :assoc [_ m _]
  (println "Assoc")
  (doseq [[size sample] size->sample]
    (simple-benchmark [] (sized-map m size) sample)))

(defmethod map-bench :dissoc [_ _ {:keys [small-map medium-map large-map]}]
  (println "Dissoc")
  (let [maps [small-map medium-map large-map]
        samples [small-map-sample medium-map-sample large-map-sample]]
    (doseq [[m sample] (partition 2 (interleave maps samples))]
     (simple-benchmark
       []
       (loop [m m i (count m)]
         (when (> i 0)
           (recur (dissoc m (get test-keys i)) (dec i))))
       sample))))

(defmethod map-bench :dissoc-fail [_ _ {:keys [small-map medium-map large-map]}]
  (println "Dissoc Fail")
  (let [maps [small-map medium-map large-map]
        samples [small-map-sample medium-map-sample large-map-sample]]
    (doseq [[m sample] (partition 2 (interleave maps samples))]
      (simple-benchmark
        []
        (loop [m m i (count m)]
          (when (> i 0)
            (recur (dissoc m i) (dec i))))
        sample))))

(defmethod map-bench :hash [_ _ {:keys [small-map medium-map large-map]}]
  (println "Hash")
  (let [maps [small-map medium-map large-map]
        samples [small-map-sample medium-map-sample large-map-sample]]
    (doseq [[m sample] (partition 2 (interleave maps samples))]
      (simple-benchmark [] (hash-unordered-coll m) sample))))

(defmethod map-bench :equals [_ _ {:keys [small-map medium-map large-map]}]
  (println "Equals")
  (let [maps [small-map medium-map large-map]
        samples [small-map-sample medium-map-sample large-map-sample]]
    (doseq [[m sample] (partition 2 (interleave maps samples))]
      (let [cm (clone m)]
        (simple-benchmark [] (= m cm) sample)))))

(defmethod map-bench :equals-fail [_ _ {:keys [small-map medium-map large-map]}]
  (println "Equals fail")
  (let [maps [small-map medium-map large-map]
        samples [small-map-sample medium-map-sample large-map-sample]]
    (doseq [[m sample] (partition 2 (interleave maps samples))]
      (let [neq-m (assoc m (js-obj) 0)]
        (simple-benchmark [] (= m neq-m) sample)))))

(defmethod map-bench :sequence [_ _ {:keys [small-map medium-map large-map]}]
  (println "sequence")
  (let [maps [small-map medium-map large-map]
        samples [small-map-sample medium-map-sample large-map-sample]]
    (doseq [[m sample] (partition 2 (interleave maps samples))]
      (simple-benchmark [] (doseq [[k v] m] [v k]) sample))))

(defmethod map-bench :reduce [_ _ {:keys [small-map medium-map large-map]}]
  (println "Reduce")
  (let [maps [small-map medium-map large-map]
        samples [small-map-sample medium-map-sample large-map-sample]]
    (doseq [[m sample] (partition 2 (interleave maps samples))]
      (simple-benchmark [] (reduce (fn [sum [k v]] (+ 1 sum)) 0 m) sample))))

(defn run-benchmarks [benchmarks empty-map data]
  (println "Running Benchmarks")
  (doseq [benchmark benchmarks]
    (map-bench benchmark empty-map data)))

(def lem (.-EMPTY lean/PersistentHashMap))
(def cem (.-EMPTY cljs.core/PersistentHashMap))

(def test-keys (mapv (fn [i]
                       (let [nk (keyword (str "key" i))]
                         (hash nk)
                         nk))
{'fraction_non_alphanumeric': 0.15904082955281917, 'fraction_numerical': 0.008554763447828904, 'mean_word_length': 2.9855371900826446, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1989781', 'n_tokens_mistral': 2753, 'n_tokens_neox': 2609, 'n_words': 765}