Repository navigation
archetype_destroy keeps three references to the destroyed archetype and under-counts wildcard record sizes - #340
Merged
Conversation
…dcard record sizes
Ukendio
approved these changes
Sep 17, 2026
Ukendio
left a comment
Owner
There was a problem hiding this comment.
Thanks for the contribution. There are some things that I am going to change for the major release. But I will be merging this to keep the change indexable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When an archetype is destroyed (
world:cleanup(), orworld:delete()of an entity used as a component or pair target), three structures still point at it afterwards:world.archetype_edges[archetype.id]- the edge table is emptied but its key is never removed, so every destroyed archetype leaves one empty table behind.componentrecord.cache-archetype_append_to_recordsappends the archetype id to thecachelist of every component record of the archetype (including the(R, *)and(*, T)wildcard records), and nothing ever removes it.world:deletewalks this list for pairs targeting the deleted entity and skips ids of dead archetypes, so the stale entries are harmless functionally, but the list grows by one entry per component per created archetype for the whole life of the world.componentrecord.wildcard_pairsof a(R, *)record keeps the record of every pair(R, T)ever seen; when a pair record leavescomponent_index(itssizereached zero) it stays referenced fromwildcard_pairs.sizeof the wildcard records(R, *)and(*, T)drifts upwards.archetype_createdoesidr_r.size += 1/idr_t.size += 1once per pair in the archetype, so an archetype with(R, T1)and(R, T2)adds 2 to(R, *), butarchetype_destroyiteratescolumns_map, where(R, *)is a single key, and subtracts 1. After such an archetype is destroyed the wildcard record keepssize >= 1with an emptyrecordsmap and is never removed fromcomponent_index.Item 4 is not just a leak. The stale
(R, *)record survivesworld:delete(R)together with itsflags(ECS_ID_DELETE,ECS_ID_IS_TAG,ECS_ID_IS_EXCLUSIVE) and hooks. Pair ids are built from the entity index only, so when the index ofRis recycled,id_record_ensurehands the stale record to the new entity: an entityR2that has no(OnDelete, Delete)trait at all inherits the cascade policy of the deadR, andworld:delete(R2)deletes every entity that holds an(R2, *)pair instead of just removing the pair. The same happens for(*, T)records when the index of a target is recycled. Repro:With a single
(R, T)pair one(no drift) the record is dropped onworld:delete(R)andfsurvives. A minimal fix isidr.size -= idr.counts[archetype_id]inarchetype_destroy(counts[archetype_id]is exactly how many timesarchetype_createincrementedsizefor that archetype), done beforecounts[archetype_id]is cleared.In a world where entities are built component by component and empty archetypes are reclaimed periodically, the same intermediate archetypes are created and destroyed over and over (we measured ~250 archetype creations per second in a 30-NPC combat scene), and the process memory of the world grew by 5–9 MB per minute with a flat entity count:
component_index.*.cachereached 776 000 numbers in 141 s,archetype_edgeshad one key per archetype ever created.jecs@0.11.0