Repository: penpot/penpot
File: frontend/src/app/main/data/event.cljs (ClojureScript)

Code before:
               :typographies (count (:typographies data))}))

          (save-file-data []
            (ptk/reify ::store-performance-info-save-file-data
              ptk/UpdateEvent
              (update [_ state]
                (let [file-id (get state :current-file-id)
                      file (get-in state [:files file-id])
                      file-size (count-shapes file)

                      libraries
                      (-> (refs/select-libraries (:files state) (:id file))
                          (update-vals (partial count-library-data (:files state))))

                      lib-sizes
                      (->> libraries
                           (vals)
                           (reduce (fn [acc {:keys [components colors typographies]}]
                                     (-> acc
                                         (update :components + components)
                                         (update :colors + colors)
                                         (update :typographies + typographies)))))]
                  (-> state
                      (assoc-in [:performance-info :file-size] file-size)
                      (assoc-in [:performance-info :library-sizes] lib-sizes)
                      (assoc-in [:performance-info :file-start-time] (js/performance.now)))))))]

    (ptk/reify ::store-performace-info
      ptk/UpdateEvent
      (update [_ state]
        (-> state
            micro-benchmark
            (assoc-in [:performance-info :app-start-time] (js/performance.now))))

      ptk/WatchEvent
      (watch [_ _ stream]
        (->> stream
             (rx/filter #(= (ptk/type %) :app.main.data.workspace/all-libraries-resolved))
             (rx/take 1)
             (rx/map save-file-data))))))

(defn initialize
  []
  (when (contains? cf/flags :audit-log)
    (ptk/reify ::initialize
      ptk/WatchEvent
      (watch [_ _ _]
        (rx/of (store-performace-info)))

      ptk/EffectEvent
      (effect [_ _ stream]

Review comment (performance):
The same here, replace multiple assoc-in with a single update + callback

```clojure
(update state :performance-info
        (fn [info]
          (-> info
              (assoc :file-size file-size)
              (assoc :library-sizes lib-sizes)
              (assoc :file-start-time (js/performance.now)))))
```

Code after:
                (let [data (dm/get-in files [id :data])]
                  {:components (count (:components data))
                   :colors (count (:colors data))
                   :typographies (count (:typographies data))}))]
        (let [file-id (get state :current-file-id)
              file (get-in state [:files file-id])
              file-size (count-shapes file)

              libraries
              (-> (refs/select-libraries (:files state) (:id file))
                  (d/update-vals (partial count-library-data (:files state))))

              lib-sizes
              (->> libraries
                   (reduce-kv
                    (fn [acc _ {:keys [components colors typographies]}]
                      (-> acc
                          (update :components + components)
                          (update :colors + colors)
                          (update :typographies + typographies)))
                    {}))]
          (update state :performance-info
                  (fn [info]
                    (-> info
                        (assoc :file-size file-size)
                        (assoc :library-sizes lib-sizes)
                        (assoc :file-start-time (perf/now))))))))))

(defn store-performace-info
  []
  (letfn [(micro-benchmark [state]
            (let [start (perf/now)]
              (loop [i micro-benchmark-iterations]
                (when-not (zero? i)
                  (* (math/sin i) (math/sqrt i))
                  (recur (dec i))))
              (let [end (perf/now)]
                (update state :performance-info assoc :bench-result (- end start)))))]

    (ptk/reify ::store-performace-info
      ptk/UpdateEvent
      (update [_ state]
        (-> state
            micro-benchmark
            (assoc-in [:performance-info :app-start-time] (perf/now))))

      ptk/WatchEvent
      (watch [_ _ stream]
        (->> stream
             (rx/filter (ptk/type? :app.main.data.workspace/all-libraries-resolved))
             (rx/take 1)