Skip to content

Commit

Permalink
Faster REPL - don't load extra middleware on first sync request
Browse files Browse the repository at this point in the history
  - Sending :inhibit-cider-middleware condition on first request

  - Combining require-repl-utils and set-initial-ns into one sync request for
    efficiency
  • Loading branch information
vspinu committed Sep 30, 2017
1 parent a1eee8c commit d75619a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

### Changes

* [cider-nrepl#438](https://github.com/clojure-emacs/cider-nrepl/pull/438): Improve startup time by differing loading CIDER's middleware until the first usage.
* [#2078](https://github.com/clojure-emacs/cider/pull/2078): Improve startup time by bundling together sync requests during startup.

### Bugs Fixed

* [#2004](https://github.com/clojure-emacs/cider/issues/2004), [#2039](https://github.com/clojure-emacs/cider/issues/2039), [cider-nrepl#420](https://github.com/clojure-emacs/cider-nrepl/issues/420): Fix namespace issues in instrumentation and debugging commands.
Expand Down
62 changes: 30 additions & 32 deletions cider-repl.el
Original file line number Diff line number Diff line change
Expand Up @@ -255,26 +255,25 @@ ENDPOINT is a plist as returned by `nrepl-connect'."
(add-hook 'nrepl-disconnected-hook 'cider--disconnected-handler nil 'local)
(current-buffer))))

(defun cider-repl-require-repl-utils ()
"Require standard REPL util functions into the current REPL."
(interactive)
(cider-nrepl-request:eval
"(when (clojure.core/resolve 'clojure.main/repl-requires)
(clojure.core/map clojure.core/require clojure.main/repl-requires))"
(lambda (_response) nil)))

(declare-function cider-set-buffer-ns "cider-mode")
(defun cider-repl-set-initial-ns (buffer)
"Set the REPL BUFFER's initial namespace (by altering `cider-buffer-ns').
This is \"user\" by default but can be overridden in apps like lein (:init-ns)."
(defun cider-repl-require-repl-utils-and-set-ns (buffer)
"Require standard REPL util functions and set the ns of the REPL's BUFFER.
Namespace is \"user\" by default, but can be overridden in apps like
lein (:init-ns). Both of these operations need to be done as a sync
request at the beginning of the session. Bundling them together for
efficiency."
;; we don't want to get a timeout during init
(let ((nrepl-sync-request-timeout nil))
(with-current-buffer buffer
(let ((initial-ns (or (read
(nrepl-dict-get
(cider-nrepl-sync-request:eval "(str *ns*)")
"value"))
"user")))
(let* ((command "(do (when (clojure.core/resolve 'clojure.main/repl-requires)
(clojure.core/map clojure.core/require clojure.main/repl-requires))
(str *ns*))")
(response (nrepl-send-sync-request
(lax-plist-put (nrepl--eval-request command)
"inhibit-cider-middleware" "true")
(cider-current-connection)))
(initial-ns (or (read (nrepl-dict-get response "value"))
"user")))
(cider-set-buffer-ns initial-ns)))))

(defvar cider-current-clojure-buffer nil
Expand All @@ -289,18 +288,29 @@ to call `cider-remember-clojure-buffer'.")
"Initialize the REPL in BUFFER.
BUFFER must be a REPL buffer with `cider-repl-mode' and a running
client process connection. Unless NO-BANNER is non-nil, insert a banner."
(cider-repl-set-initial-ns buffer)
(cider-repl-require-repl-utils)
(unless no-banner
(cider-repl--insert-banner-and-prompt buffer))
(when cider-repl-display-in-current-window
(add-to-list 'same-window-buffer-names (buffer-name buffer)))
(pcase cider-repl-pop-to-buffer-on-connect
(`display-only (display-buffer buffer))
((pred identity) (pop-to-buffer buffer)))
(cider-repl-require-repl-utils-and-set-ns buffer)
(unless no-banner
(cider-repl--insert-banner-and-prompt buffer))
(cider-remember-clojure-buffer cider-current-clojure-buffer)
buffer)

(defun cider-repl--insert-banner-and-prompt (buffer)
"Insert REPL banner and REPL prompt in BUFFER."
(with-current-buffer buffer
(when (zerop (buffer-size))
(insert (propertize (cider-repl--banner) 'font-lock-face 'font-lock-comment-face))
(when cider-repl-display-help-banner
(insert (propertize (cider-repl--help-banner) 'font-lock-face 'font-lock-comment-face))))
(goto-char (point-max))
(cider-repl--mark-output-start)
(cider-repl--mark-input-start)
(cider-repl--insert-prompt cider-buffer-ns)))

(defun cider-repl--banner ()
"Generate the welcome REPL buffer banner."
(let ((host (cider--connection-host (current-buffer)))
Expand Down Expand Up @@ -359,18 +369,6 @@ client process connection. Unless NO-BANNER is non-nil, insert a banner."
;; ======================================================================
"))

(defun cider-repl--insert-banner-and-prompt (buffer)
"Insert REPL banner and REPL prompt in BUFFER."
(with-current-buffer buffer
(when (zerop (buffer-size))
(insert (propertize (cider-repl--banner) 'font-lock-face 'font-lock-comment-face))
(when cider-repl-display-help-banner
(insert (propertize (cider-repl--help-banner) 'font-lock-face 'font-lock-comment-face))))
(goto-char (point-max))
(cider-repl--mark-output-start)
(cider-repl--mark-input-start)
(cider-repl--insert-prompt cider-buffer-ns)))


;;; REPL interaction

Expand Down

0 comments on commit d75619a

Please sign in to comment.