From d75619a612bda26e78e38a66d9a87ea4c66e8002 Mon Sep 17 00:00:00 2001 From: Vitalie Spinu Date: Tue, 22 Aug 2017 02:37:32 +0200 Subject: [PATCH] Faster REPL - don't load extra middleware on first sync request - Sending :inhibit-cider-middleware condition on first request - Combining require-repl-utils and set-initial-ns into one sync request for efficiency --- CHANGELOG.md | 3 +++ cider-repl.el | 62 +++++++++++++++++++++++++-------------------------- 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a227ad89..885716757 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/cider-repl.el b/cider-repl.el index a22e82920..11dc6d55c 100644 --- a/cider-repl.el +++ b/cider-repl.el @@ -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 @@ -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))) @@ -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