Clojure swing app startup time

2019-05-05 07:30发布

问题:

I just started making a GUI app using clojure and seesaw. It does little more that create a JFrame and a couple components. Here's the code. The main function does nothing but call start-gui and exit as soon as it returns.

(ns pause.gui
  (:use seesaw.core))

(native!)
; (javax.swing.UIManager/setLookAndFeel
;   "org.pushingpixels.substance.api.skin.SubstanceGraphiteLookAndFeel")

(def main-window
  (frame :title    "Pause"
         :on-close :exit))

(def sidebar (listbox :model []))
(def main-area (text :multi-line? true
                     :font "MONOSPACED-PLAIN-14"
                     :text "test"))

(def main-split
  (left-right-split (scrollable sidebar)
                    (scrollable main-area)
                    :divider-location 1/5))

(defn setup-main-window
  "Fills the main window with its content"
  [main-window]
  (config! main-window
           :content main-split)
  main-window)

(defn start-gui
  "Create the main window"
  []
  (-> main-window
      setup-main-window
      pack!
      show!))

I compiled this using lein uberjar and timed it with time java -jar. It reported 14.5 seconds. Is there something I'm doing wrong? I'm okay with 3 seconds startup but this is completely unacceptable.

回答1:

Clojure still has quite a bit of startup time, sadly. This is mainly because of the amount of compilation / code loading that happens when Clojure loads in all the required namespaces.

For the Swing-based GUI apps that I have written, I have often written the main entry point in Java, so that you can display the initial GUI or a splash screen quickly to the user - while the rest of the app / Clojure code loads in the background.