Skip to content

Write down the allocation model - #237

Merged
kahrendt merged 2 commits into
mainfrom
allocation-model-docs
Nov 4, 2026
Merged

kahrendt merged 2 commits into
mainfrom
allocation-model-docs

Conversation

@kahrendt

@kahrendt kahrendt commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Summary

The allocation series (#225-#236) enforced a rule that was written down only in tests/test_allocations.cpp's header and in PR bodies. ESPHome's operator new draws only on internal RAM and aborts on failure, so after start() connection setup and message handling allocate through the fallible platform_malloc family, and bounded values are held inline. This PR states that rule and records where the code still allocates with operator new. Docs only.

Change

  • docs/conventions.md: "Embedded resource discipline" gains the rule: operator new at construction, add_*() and start() only; afterwards platform_malloc, whose failure refuses or closes the connection or drops the message; bounded values inline (InlineText, InlineVector, EnumSet, std::array); peer text bounded where it is parsed; public std::string/std::vector types built on the thread that hands them to the consumer. The Documentation bullet lists the allocation model among internals.md's contents.
  • docs/internals.md: a new "Memory" section: the three allocators (operator new, platform_malloc, noise-c's calloc) and how each fails, what is held inline, a table of the operator new allocations that remain after start() with when and on which thread each runs, and the one-pointer capture the std::functions built after start() rely on.
  • CLAUDE.md: the Memory convention names the inline types and points at the new section; the test build note says what sendspin_alloc_tests counts; both descriptions of internals.md mention memory.
  • .claude/skills/embedded-review/SKILL.md: a new operator new after start() is listed in "Memory" or reported as a finding.

Tests

Docs only. Each claim in the Memory section was checked against the code; the table is the checklist the remaining allocation groups (tracked in Sendspin/backlog#92) remove rows from.

The rule the allocation series enforced was stated only in the
allocation test's header and PR bodies. ESPHome's operator new draws
only on internal RAM and aborts on failure, so after start() connection
setup and message handling allocate through the fallible platform_malloc
family, and bounded values are held inline.

- conventions.md "Embedded resource discipline" states the rule; its
  Documentation bullet lists the allocation model among internals.md's
  contents.
- internals.md gains a "Memory" section: the allocators and how each
  fails, what is held inline, a table of the operator new allocations
  that remain after start(), and the one-pointer capture the
  std::functions built after start() depend on.
- CLAUDE.md names the inline types and what sendspin_alloc_tests counts.
- The embedded-review skill checks a new operator new after start().
@kahrendt
kahrendt marked this pull request as ready for review November 3, 2026 23:48
@kahrendt
kahrendt requested a max review from Copilot November 3, 2026 23:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The allocation documentation omits post-start allocation sites and an aborting failure path, overstates callback allocation guarantees, and misidentifies noise-c state ownership.

5 open issues
What changed in this PR

Documents the allocation model established by the allocation-reduction series, giving contributors a shared reference for ESPHome’s internal-RAM and allocation-failure constraints. This PR changes documentation only.

Changes:

  • Define post-start() allocation rules, inline storage expectations, and public-value conversions.
  • Inventory remaining heap allocations, their timing and threads, and callback storage assumptions.
  • Align contributor guidance and the embedded-review checklist with the allocation model.
File Description
docs/​internals.md Add the memory model and remaining-allocation inventory.
docs/​conventions.md Establish allocator and bounded-storage rules.
CLAUDE.md Update memory guidance, documentation references, and allocation-test notes.
.claude/​skills/​embedded-review/​SKILL.md Require new post-start operator new sites to be documented or flagged.

Review effort: Max

Comment thread docs/internals.md Resolved
Comment thread docs/internals.md Resolved
Comment thread docs/internals.md Resolved
Comment thread docs/internals.md Resolved
Comment thread docs/internals.md Resolved
- Scope: the host transport, which copies each frame into a std::string,
  and the host-only message trace sit outside the model.
- Failures: noise-c's hash states are allocated like its others, and
  platform_random_bytes() and psk_id_for() on a std::array abort when
  noise-c cannot allocate their state.
- NoiseSession alone owns the noise-c handshake and cipher states.
- Every table row names its thread; the record store's changed-slot
  list and group/update's later copies are added.
- A std::function capture change is checked against the small buffer
  rather than assumed to allocate.
@kahrendt
kahrendt merged commit d0b439a into main Nov 4, 2026
16 checks passed
@kahrendt
kahrendt deleted the allocation-model-docs branch November 4, 2026 00:21
@kahrendt kahrendt added the documentation Improvements or additions to documentation label Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants