Skip to content

Speed up indentation of forms with many children - #174

Merged
bbatsov merged 3 commits into
mainfrom
faster-value-position
Nov 7, 2026
Merged

bbatsov merged 3 commits into
mainfrom
faster-value-position

Conversation

@bbatsov

@bbatsov bbatsov commented Nov 7, 2026

Copy link
Copy Markdown
Member

Indenting a form with many children slowed down with the cube of its length, mostly because Emacs finds a node's field by walking the siblings before it: a comment form holding 400 functions took 13 seconds, and cljs/core.cljs 4.7 (now 0.3 and 0.7). Telling value children apart by their type gives the same answer as the field for every child of a collection across 928 Clojure files, and the same indent-region output.

Getting the field of a node walks the siblings before it, and
clojure-ts--value-position got the field of every sibling before a
line, so the time to indent a form grew with the cube of its length:
indent-region took 13 seconds on a comment form holding 400 functions,
and 4.7 on cljs/core.cljs.

In a collection, the field follows from the node's type, except for the
prefix of a namespaced map, which comes right after its #.
Finding the indentation rule counted the forms before a line at every
level it climbed, though only a rule for a given argument needs the
count, and only to tell whether the line is at that argument.  A block
rule likewise only compares it with its number of special arguments.
In a form with many children, each line paid for counting all the forms
before it.
clojure-ts--anchor-parent-opening-paren searches the parent for its
paren, and the search tests the parent itself first, so getting the
node's text copied the whole form for every line in its body.
@bbatsov
bbatsov force-pushed the faster-value-position branch from 2dacc19 to ee879d0 Compare November 7, 2026 08:53
@bbatsov
bbatsov merged commit 40fd5ba into main Nov 7, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant