Skip to content

Commit

Permalink
Optimize API requests a bit by deferring them
Browse files Browse the repository at this point in the history
* compiler-explorer.el (compiler-explorer--inhibit-request): New
defvar.
(compiler-explorer--request-async-1): Rename from
`compiler-explorer--request-async'.
(compiler-explorer--request-async): New function.
(compiler-explorer--restore-session):
(compiler-explorer-new-session): Defer making the API requests till the
end.
  • Loading branch information
mkcms committed Jun 4, 2024
1 parent 9d6a083 commit 3f3416a
Showing 1 changed file with 35 additions and 18 deletions.
53 changes: 35 additions & 18 deletions compiler-explorer.el
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,13 @@ with `json-parse', and a message is displayed.")
if (compiler-explorer--filter-enabled-p k)
nconc (list k (or v :json-false))))

(defun compiler-explorer--request-async ()
"Queue compilation and execution and return immediately.
This calls `compiler-explorer--handle-compilation-response' and
`compiler-explorer--handle-execution-response' once the responses arrive."
(defvar compiler-explorer--inhibit-request nil
"If non-nil, inhibit making the async compilation/execution request.
This can be temporarily let-bound to defer making async requests
when multiple functions try to do it in a block of code.")

(defun compiler-explorer--request-async-1 ()
"Subr of `compiler-explorer--request-async'."
(pcase-dolist
(`(,executorRequest ,symbol ,handler)
`((:json-false
Expand Down Expand Up @@ -436,6 +439,13 @@ This calls `compiler-explorer--handle-compilation-response' and
(compiler-explorer--build-overlays nil)
(force-mode-line-update))

(defun compiler-explorer--request-async ()
"Queue compilation and execution and return immediately.
This calls `compiler-explorer--handle-compilation-response' and
`compiler-explorer--handle-execution-response' once the responses arrive."
(unless compiler-explorer--inhibit-request
(compiler-explorer--request-async-1)))

(defvar compiler-explorer--project-dir)

;; TODO: This variable is present only because in Emacs-26
Expand Down Expand Up @@ -571,6 +581,9 @@ output buffer."
(compiler-explorer--replace-buffer-contents buf (current-buffer)))
(ansi-color-apply-on-region (point-min) (point-max))))))


;; UI

(defun compiler-explorer--header-line-format-common ()
"Get the mode line template used in compiler explorer mode."
(let* ((is-exe (eq (current-buffer)
Expand Down Expand Up @@ -1052,17 +1065,19 @@ It must have been created with `compiler-explorer--current-session'."
(unless (funcall pred val)
(error "Invalid %s: %s" sym val)))

(compiler-explorer-new-session lang-name compiler)
(with-current-buffer (get-buffer compiler-explorer--buffer)
(let ((inhibit-modification-hooks t))
(erase-buffer)
(insert source)
(set-buffer-modified-p nil)))
(pcase-dolist (`(,id . ,vid) libs)
(compiler-explorer-add-library id vid))
(compiler-explorer-set-compiler-args args)
(compiler-explorer-set-execution-args exe-args)
(compiler-explorer-set-input input)
(let ((compiler-explorer--inhibit-request t))
(compiler-explorer-new-session lang-name compiler)
(with-current-buffer (get-buffer compiler-explorer--buffer)
(let ((inhibit-modification-hooks t))
(erase-buffer)
(insert source)
(set-buffer-modified-p nil)))
(pcase-dolist (`(,id . ,vid) libs)
(compiler-explorer-add-library id vid))
(compiler-explorer-set-compiler-args args)
(compiler-explorer-set-execution-args exe-args)
(compiler-explorer-set-input input))
(compiler-explorer--request-async)
(compiler-explorer--define-menu)))

(defvar compiler-explorer--last-session nil)
Expand Down Expand Up @@ -1788,9 +1803,11 @@ compiler."
(let (success)
(unwind-protect
(progn
(compiler-explorer-new-session-1 lang
(if (eq compiler t) nil compiler)
(eq compiler t))
(let ((compiler-explorer--inhibit-request t))
(compiler-explorer-new-session-1 lang
(if (eq compiler t) nil compiler)
(eq compiler t)))
(compiler-explorer--request-async)
(setq success t))
(unless success
(compiler-explorer--cleanup 'skip-save-session)))))
Expand Down

0 comments on commit 3f3416a

Please sign in to comment.