Skip to content

Render Arbre trees into one shared buffer - #756

Open
senid231 wants to merge 6 commits into
activeadmin:masterfrom
senid231:perf/buffered-render
Open

senid231 wants to merge 6 commits into
activeadmin:masterfrom
senid231:perf/buffered-render

Conversation

@senid231

@senid231 senid231 commented Nov 23, 2026 •

Copy link
Copy Markdown

Rendering a large tree now allocates a fraction of the memory, and the output stays byte-identical. Before this change, every nesting level copied its subtree's HTML about five times (indent, ElementCollection#to_s, html_safe), and each text node built a throwaway Arbre::Context. Related to #67.

Elements now write into one shared buffer, with the depth passed down instead of walking up the parent chain. A class or a single object that overrides to_s, content, indent, opening_tag, closing_tag, indent_level, insert_tag, build_tag or a similar hook still renders through that override. Overrides added later through include, prepend or extend are detected too. Document writes its doctype into the buffer.

Results on a 500-row by 44-cell table:

Before After
Allocated 2.08M objects, 223 MB 431k objects, 39 MB
Render CPU 395 ms 125 ms
Peak RSS growth 91 MB 24 MB

A real ActiveAdmin 4 index page (500 rows) allocates 2.36M → 0.98M objects, and its HTML is identical to before.

Builder methods also hand their argument array straight to Tag#build's body when build is still Arbre's own, so a plain tag allocates 4 objects instead of 10.

Verification: new specs in spec/arbre/unit/rendering_spec.rb; 49 edge cases rendered with this branch and with 2.2.1 compare byte for byte, encoding included, under ActiveSupport 8.1 and 7.1; ActiveAdmin v4.0.0.beta23's suite passes (1588 examples).

Each subtree's HTML used to be copied several times per nesting level,
and every text node built a throwaway Arbre::Context. Elements now write
into one buffer, and classes that override a rendering or building hook
keep going through that override, so output and API stay unchanged.

Co-Authored-By: Clanker
Builder methods hand their argument array straight to Tag#build's body
when build is still Arbre's own, and Element#build takes an anonymous
splat it never reads. Overridden or extended builds go the usual way.

Co-Authored-By: Clanker
Attributes are written in one pass, the opening and closing tags are
written inline, and the render-flag lookup reads two instance variables
instead of building an array. The override checks stay in place.

Co-Authored-By: Clanker
@senid231

Copy link
Copy Markdown
Author

@tagliala please take a look when you have time

@Fivell
Fivell requested a review from javierjulio November 23, 2026 16:43

This branch has not been deployed

No deployments
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