memusage: Make fields of STL helper structs public

bitcoin/bitcoin: PR #36869

Review completed with no actionable findings.

Coverage details

Background

Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.

Documented decisions

Memory accounting was introduced to expose actual mempool resource consumption, with resource limiting explicitly identified as a further use. The contemporary limiting proposal framed bounded memory consumption as protection against denial of service.

These helpers account for container storage beyond transaction payload sizes. At the pinned head, their estimates contribute to CTxMemPool::DynamicMemoryUsage(), which is used when trimming the mempool, and to the memory limit for disconnected-block transactions. This is the resource-management purpose behind the layout estimates, independent of the access-specifier change.

Source 1 · Source 2

Dynamic memory accounting deliberately separates an object's own storage from allocations owned indirectly by that object. Pieter Wuille explained that accounting for entries through their containing map allows alignment and malloc overhead to be included.

This explains why the affected helpers model whole container nodes, including linkage fields, rather than merely multiplying the number of elements by sizeof(element). In both pinned versions, the synthetic node size passes through MallocUsage; unordered-container estimates additionally include bucket storage. The fields contribute to the estimate without needing to be accessed.

Source 1

Ralph's inferences

The synthetic structs are accounting models, not alternative container implementations: their purpose is to let the compiler calculate a plausible node size, including padding around payload and linkage fields.

The pinned base and head use stl_tree_node, list_node, and unordered_node as sizeof operands in container-memory formulas. Their members are not traversed or manipulated by those formulas. This connects the historical concern with alignment and allocation overhead to why otherwise unused fields exist, without establishing a historical reason for their original private access.

Source 1

Cheap outer-container estimates combined with separately maintained inner usage make repeated resource-limit checks practical without recursively walking all stored data.

The pinned memusage.h explicitly keeps generic accounting nonrecursive for efficiency. The mempool and disconnected-transaction callers combine container estimates with cached inner usage. This reflects a time-versus-accounting-detail tradeoff, consistent with the historical objection that scanning an entire mempool whenever its limit is exceeded would be inefficient. The patch leaves that accounting structure unchanged.

Source 1 · Source 2