Skip to content

Instantly share code, notes, and snippets.

@mfikes
Created March 24, 2015 02:24
Show Gist options
  • Save mfikes/2f6007ee95ad47133a38 to your computer and use it in GitHub Desktop.
Save mfikes/2f6007ee95ad47133a38 to your computer and use it in GitHub Desktop.
Source map optimization

If you can presume that cljs.core is fixed (AOT, etc), then its associated source map reading can be heavily optimized.

This simple optimization "permanently memoizes" its source map, once read. It makes stacktrace printing instantaneous for simple projects, once the first has been printed.

(defn- read-source-map-impl
  [f]
  (let [smf (io/file (str f ".map"))]
    (when (.exists smf)
      (sm/decode (json/read-str (slurp smf) :key-fn keyword)))))

(def core-cljs-source-map (atom nil))

(defn read-source-map
  "Return the source map for the JavaScript source file."
  [f]
  (if (.endsWith (str f) "/cljs/core.js")
    (swap! core-cljs-source-map (fn [sm] (or sm (read-source-map-impl f))))
    (read-source-map-impl f)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment