cognitect / transit-cljs

Transit for ClojureScript
http://transit-format.org
Apache License 2.0
323 stars 20 forks source link

Consider better support for DCE #34

Open rauhs opened 7 years ago

rauhs commented 7 years ago

Currently, because of the way the writer is set up, Google Closure will not DCE any CLJS core data structures that are otherwise unused by the developer.

Though, transit-js already support dynamically looking up an alias on the object itself. Thus by adding the alias to the prototype of the types, it'd enable proper DCE on lesser used types.

So by using, for instance:

(goog.object/set (.-prototype PersistentQueueSeq) "transitTag" List)
(goog.object/set (.-prototype PersistentQueue) "transitTag" List)
(goog.object/set (.-prototype PersistentTreeSet) "transitTag" PersistentHashSet)

And removing the entries from the map in transito.core/writer, we enable DCE without losing any functionality.

This also works fine with GCC's advanced minification.

Edit: Should probably not reuse the types since user's may provide customs handler for types in which case the alias could fail. We could introduce some simple dummy deftypes that can for sure handle the conversion and alias it to those instead.

So:

(deftype GenericListHandler [])
(deftype GenericSetHandler [])
(goog.object/set (.-prototype PersistentQueueSeq) "transitTag" GenericListHandler)
(goog.object/set (.-prototype PersistentQueue) "transitTag" GenericListHandler)
(goog.object/set (.-prototype PersistentTreeSet) "transitTag" GenericSetHandler)

;; Then in the writer:
(merge
           {GenericListHandler list-handler
            GenericSetHandler set-handler
          .....})
swannodette commented 7 years ago

Will think about it.