Skip to content

REPL slow when processing long outputs #3219

@sebastianpoeplau

Description

@sebastianpoeplau

Expected behavior

When running code which produces a lot of output in the CIDER REPL buffer, the output should show reasonably fast (within the limits of Emacs' capabilities regarding buffer updates).

Actual behavior

The REPL window hangs for quite some time before showing the output (see reproducing example below). The profiler suggests that cider-repl--emit-output is the culprit. I've experimented a bit with it, and it seems that the problem is caused by the part that scrolls the output window if necessary:

  (when-let* ((window (get-buffer-window buffer t)))
    ;; If the prompt is on the first line of the window, then scroll the window
    ;; down by a single line to make the emitted output visible.
    (when (and (pos-visible-in-window-p cider-repl-prompt-start-mark window)
               (< 1 cider-repl-prompt-start-mark)
               (not (pos-visible-in-window-p (1- cider-repl-prompt-start-mark) window)))
      (with-selected-window window
        (scroll-down 1))))

Commenting out this part of the function makes the problem disappear (while the REPL keeps working except for the logic that scrolls the window if the prompt happens to be on the first line). In particular, pos-visible-in-window-p seems to be really slow; note how replacing the block above with just (pos-visible-in-window-p cider-repl-prompt-start-mark) shows the same problematic behavior.

Steps to reproduce the problem

Start a CIDER session and define the following function:

(defn output-a-lot []
  (dotimes [_ 100]
    (println (range 1000))))

Call the function from the REPL buffer; the output should take several seconds to appear (adjust the loop count if necessary). Try to remove the part of cider-repl--emit-output mentioned above, evaluate the change, and call output-a-lot again in the REPL; the output should now appear more or less immediately.

Environment & Version information

CIDER version information

;; CIDER 1.5.0-snapshot (package: 1.5.0-snapshot), nREPL 0.8.3
;; Clojure 1.10.3, Java 11.0.15

Lein / Clojure CLI version

Leiningen 2.9.8 on Java 11.0.15 OpenJDK 64-Bit Server VM

Emacs version

GNU Emacs 27.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.30, cairo version 1.16.0) of 2022-01-24, modified by Debian

Operating system

Ubuntu 22.04

JDK distribution

openjdk version "11.0.15" 2022-04-19
OpenJDK Runtime Environment (build 11.0.15+10-Ubuntu-0ubuntu0.22.04.1)
OpenJDK 64-Bit Server VM (build 11.0.15+10-Ubuntu-0ubuntu0.22.04.1, mixed mode, sharing)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions