Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster REPL - don't load extra middleware on first sync eval request #2078

Merged
merged 3 commits into from
Oct 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

## master (unreleased)

### New Features

* [#2082](https://github.com/clojure-emacs/cider/pull/2082), [cider-nrepl#440](https://github.com/clojure-emacs/cider-nrepl/pull/440): Add specialized stacktraces for clojure.spec assertions.

### 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

* [#2088](https://github.com/clojure-emacs/cider/issues/2088): Fix functions defined with def being font locked as vars instead of functions.
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you move those down?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The REPL shows a bit earlier (.5-1 sec for me). Also, the repl is always shown, even when errors occur with those subsequent requests.

(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
7 changes: 6 additions & 1 deletion cider.el
Original file line number Diff line number Diff line change
Expand Up @@ -892,10 +892,15 @@ buffer."
(cider--check-required-nrepl-version)
(cider--check-clojure-version-supported)
(cider--check-middleware-compatibility)
(cider--debug-init-connection)
(cider--subscribe-repl-to-server-out)
(when cider-auto-mode
(cider-enable-on-existing-clojure-buffers))
;; Middleware on cider-nrepl side is differed until first usage, but,
;; loading middleware concurrently can lead to occasional "require" issues
;; (likely a clojure bug). Thus, we load the heavy debug middleware towards
;; the end, allowing for the faster "server-out" middleware to load
;; first.
(cider--debug-init-connection)
(run-hooks 'cider-connected-hook)))

(defun cider--disconnected-handler ()
Expand Down