Add entity ranges (#221)

* Add component registration and metadata API

* Fix test case erroring

* Initial commit

* Simplify entity swap logic in world_entity function

* Allow to disconnect signals

* Remove appending to array

* Add ecs_assert and fix entity range handling

* Fix listener array indexing in observers

* Only max_id and alive_count if range_begin is larger than built in
ranges

* No nullable records

* Index is not a stable pointer
This commit is contained in:
Marcus 2025-04-25 23:53:39 +02:00 committed by GitHub
parent 846722ba58
commit d1d5b1f207
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 223 additions and 58 deletions

View file

@ -11,6 +11,7 @@ The format is based on [Keep a Changelog][kac], and this project adheres to
## [Unreleased] ## [Unreleased]
- `[world]`: - `[world]`:
- Added `world:range` to allow for creating
- Changed `world:clear` to also look through the component record for the cleared `ID` - Changed `world:clear` to also look through the component record for the cleared `ID`
- Removes the cleared ID from every entity that has it - Removes the cleared ID from every entity that has it
- Changed entity ID layouts by putting the index in the lower bits, which should make every world function 1-5 nanoseconds faster - Changed entity ID layouts by putting the index in the lower bits, which should make every world function 1-5 nanoseconds faster

View file

@ -89,12 +89,6 @@ local function join(world, component)
end end
end end
local positions_join = join(world, Position)
for e, v in positions_join() do
end
local function query_changed(world, component) local function query_changed(world, component)
assert(jecs.IS_PAIR(component) == false) assert(jecs.IS_PAIR(component) == false)
local callerid = debug.info(2, "sl") local callerid = debug.info(2, "sl")
@ -208,12 +202,12 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
world.added = function(_, component, fn) world.added = function(_, component, fn)
local listeners = signals.added[component] local listeners = signals.added[component]
local max = #listeners + 1
local component_index = world.component_index :: jecs.ComponentIndex local component_index = world.component_index :: jecs.ComponentIndex
assert(component_index[component] == nil, "You cannot use hooks on components you intend to use this signal with") assert(component_index[component] == nil, "You cannot use hooks on components you intend to use this signal with")
if not listeners then if not listeners then
listeners = {} listeners = {}
signals.added[component] = listeners signals.added[component] = listeners
local function on_add(entity: number, id: number, value: any) local function on_add(entity: number, id: number, value: any)
for _, listener in listeners :: any do for _, listener in listeners :: any do
listener(entity, id, value) listener(entity, id, value)
@ -221,22 +215,21 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
end end
world:set(component, jecs.OnAdd, on_add) world:set(component, jecs.OnAdd, on_add)
end end
listeners[max] = fn table.insert(listeners, fn)
return function() return function()
local n = #listeners local n = #listeners
listeners[max] = listeners[n] local i = table.find(listeners, fn)
listeners[i] = listeners[n]
listeners[n] = nil listeners[n] = nil
end end
end end
world.changed = function(_, component, fn) world.changed = function(_, component, fn)
local listeners = signals.emplaced[component] local listeners = signals.emplaced[component]
local max = 0
local component_index = world.component_index :: jecs.ComponentIndex local component_index = world.component_index :: jecs.ComponentIndex
assert(component_index[component] == nil, "You cannot use hooks on components you intend to use this signal with") assert(component_index[component] == nil, "You cannot use hooks on components you intend to use this signal with")
if not listeners then if not listeners then
listeners = {} listeners = {}
max = 1
signals.emplaced[component] = listeners signals.emplaced[component] = listeners
local function on_change(entity: number, id: number, value: any) local function on_change(entity: number, id: number, value: any)
for _, listener in listeners :: any do for _, listener in listeners :: any do
@ -244,18 +237,18 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
end end
end end
world:set(component, jecs.OnChange, on_change) world:set(component, jecs.OnChange, on_change)
else
max = #listeners + 1
end end
listeners[max] = fn table.insert(listeners, fn)
return function() return function()
local n = #listeners
local i = table.find(listeners, fn)
listeners[i] = listeners[n]
listeners[n] = nil
end end
end end
world.removed = function(_, component, fn) world.removed = function(_, component, fn)
local listeners = signals.removed[component] local listeners = signals.removed[component]
local max = #listeners
local component_index = world.component_index :: jecs.ComponentIndex local component_index = world.component_index :: jecs.ComponentIndex
assert(component_index[component] == nil, "You cannot use hooks on components you intend to use this signal with") assert(component_index[component] == nil, "You cannot use hooks on components you intend to use this signal with")
if not listeners then if not listeners then
@ -268,10 +261,11 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
end end
world:set(component, jecs.OnRemove, on_remove) world:set(component, jecs.OnRemove, on_remove)
end end
listeners[max] = fn table.insert(listeners, fn)
return function() return function()
local n = #listeners local n = #listeners
listeners[max] = listeners[n] local i = table.find(listeners, fn)
listeners[i] = listeners[n]
listeners[n] = nil listeners[n] = nil
end end
end end

View file

@ -4,10 +4,9 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage") local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Matter = require(ReplicatedStorage.DevPackages.Matter) local Matter = require(ReplicatedStorage.DevPackages.Matter)
local ecr = require(ReplicatedStorage.DevPackages.ecr) local ecr = require(ReplicatedStorage.DevPackages.ecr)
local jecs = require(ReplicatedStorage.Lib) local jecs = require(ReplicatedStorage.Lib:Clone())
local newWorld = Matter.World.new()
local ecs = jecs.World.new() local ecs = jecs.World.new()
local mirror = require(ReplicatedStorage.mirror) local mirror = require(ReplicatedStorage.mirror:Clone())
local mcs = mirror.World.new() local mcs = mirror.World.new()
local A1 = Matter.component() local A1 = Matter.component()

View file

@ -2,34 +2,30 @@
--!native --!native
local ReplicatedStorage = game:GetService("ReplicatedStorage") local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Matter = require(ReplicatedStorage.DevPackages.Matter) local jecs = require(ReplicatedStorage.Lib:Clone())
local ecr = require(ReplicatedStorage.DevPackages.ecr) local mirror = require(ReplicatedStorage.mirror:Clone())
local jecs = require(ReplicatedStorage.Lib)
local newWorld = Matter.World.new()
local ecs = jecs.World.new()
return { return {
ParameterGenerator = function() ParameterGenerator = function()
local registry2 = ecr.registry() local ecs = jecs.world()
ecs:range(1000, 20000)
return registry2 local mcs = mirror.World.new()
return ecs, mcs
end, end,
Functions = { Functions = {
Matter = function() Mirror = function(_, ecs, mcs)
for i = 1, 1000 do for i = 1, 100 do
newWorld:spawn()
mcs:entity()
end end
end, end,
ECR = function(_, registry2) Jecs = function(_, ecs, mcs)
for i = 1, 1000 do for i = 1, 100 do
registry2.create()
end
end,
Jecs = function()
for i = 1, 1000 do
ecs:entity() ecs:entity()
end end
end, end,

137
jecs.luau
View file

@ -62,6 +62,8 @@ type ecs_entity_index_t = {
sparse_array: Map<i24, ecs_record_t>, sparse_array: Map<i24, ecs_record_t>,
alive_count: number, alive_count: number,
max_id: number, max_id: number,
range_begin: number?,
range_end: number?
} }
type ecs_query_data_t = { type ecs_query_data_t = {
@ -125,6 +127,12 @@ local ECS_INTERNAL_ERROR = [[
https://github.com/Ukendio/jecs/issues/new?template=BUG-REPORT.md https://github.com/Ukendio/jecs/issues/new?template=BUG-REPORT.md
]] ]]
local function ecs_assert(condition, msg: string?)
if not condition then
error(msg)
end
end
local ecs_metadata: Map<i53, Map<i53, any>> = {} local ecs_metadata: Map<i53, Map<i53, any>> = {}
local ecs_max_component_id = 0 local ecs_max_component_id = 0
local ecs_max_tag_id = EcsRest local ecs_max_tag_id = EcsRest
@ -185,6 +193,10 @@ local function ECS_ENTITY_T_LO(e: i53): i24
return e % ECS_ENTITY_MASK return e % ECS_ENTITY_MASK
end end
local function ECS_ID(e: i53)
return e % ECS_ENTITY_MASK
end
local function ECS_GENERATION(e: i53) local function ECS_GENERATION(e: i53)
return e // ECS_ENTITY_MASK return e // ECS_ENTITY_MASK
end end
@ -249,10 +261,10 @@ local function entity_index_is_alive(entity_index: ecs_entity_index_t, entity: i
return entity_index_try_get(entity_index, entity) ~= nil return entity_index_try_get(entity_index, entity) ~= nil
end end
local function entity_index_get_alive(index: ecs_entity_index_t, entity: i53): i53? local function entity_index_get_alive(entity_index: ecs_entity_index_t, entity: i53): i53?
local r = entity_index_try_get_any(index, entity) local r = entity_index_try_get_any(entity_index, entity)
if r then if r then
return index.dense_array[r.dense] return entity_index.dense_array[r.dense]
end end
return nil return nil
end end
@ -280,11 +292,15 @@ local function ecs_get_alive(world, entity)
return current return current
end end
local ECS_INTERNAL_ERROR_INCOMPATIBLE_ENTITY = "Entity is outside range"
local function entity_index_new_id(entity_index: ecs_entity_index_t): i53 local function entity_index_new_id(entity_index: ecs_entity_index_t): i53
local dense_array = entity_index.dense_array local dense_array = entity_index.dense_array
local alive_count = entity_index.alive_count local alive_count = entity_index.alive_count
local sparse_array = entity_index.sparse_array
local max_id = entity_index.max_id local max_id = entity_index.max_id
if alive_count ~= max_id then
if alive_count < max_id then
alive_count += 1 alive_count += 1
entity_index.alive_count = alive_count entity_index.alive_count = alive_count
local id = dense_array[alive_count] local id = dense_array[alive_count]
@ -292,11 +308,14 @@ local function entity_index_new_id(entity_index: ecs_entity_index_t): i53
end end
local id = max_id + 1 local id = max_id + 1
local range_end = entity_index.range_end
ecs_assert(range_end == nil or id < range_end, ECS_INTERNAL_ERROR_INCOMPATIBLE_ENTITY)
entity_index.max_id = id entity_index.max_id = id
alive_count += 1 alive_count += 1
entity_index.alive_count = alive_count entity_index.alive_count = alive_count
dense_array[alive_count] = id dense_array[alive_count] = id
entity_index.sparse_array[id] = { dense = alive_count } :: ecs_record_t sparse_array[id] = { dense = alive_count } :: ecs_record_t
return id return id
end end
@ -583,10 +602,10 @@ local function id_record_ensure(world: ecs_world_t, id: number): ecs_id_record_t
local is_pair = ECS_IS_PAIR(id) local is_pair = ECS_IS_PAIR(id)
if is_pair then if is_pair then
relation = entity_index_get_alive(entity_index, ECS_PAIR_FIRST(id)) :: i53 relation = entity_index_get_alive(entity_index, ECS_PAIR_FIRST(id)) :: i53
assert(relation and entity_index_is_alive( ecs_assert(relation and entity_index_is_alive(
entity_index, relation), ECS_INTERNAL_ERROR) entity_index, relation), ECS_INTERNAL_ERROR)
target = entity_index_get_alive(entity_index, ECS_PAIR_SECOND(id)) :: i53 target = entity_index_get_alive(entity_index, ECS_PAIR_SECOND(id)) :: i53
assert(target and entity_index_is_alive( ecs_assert(target and entity_index_is_alive(
entity_index, target), ECS_INTERNAL_ERROR) entity_index, target), ECS_INTERNAL_ERROR)
end end
@ -719,8 +738,100 @@ local function archetype_create(world: ecs_world_t, id_types: { i24 }, ty, prev:
return archetype return archetype
end end
local function world_entity(world: ecs_world_t): i53 local function world_range(world: ecs_world_t, range_begin: number, range_end: number?)
return entity_index_new_id(world.entity_index) local entity_index = world.entity_index
entity_index.range_begin = range_begin
entity_index.range_end = range_end
local max_id = entity_index.max_id
if range_begin > max_id then
local dense_array = entity_index.dense_array
local sparse_array = entity_index.sparse_array
for i = max_id, range_begin do
dense_array[i] = i
sparse_array[i] = {
dense = 0
} :: ecs_record_t
end
entity_index.max_id = range_begin - 1
entity_index.alive_count = range_begin - 1
end
end
local function world_entity(world: ecs_world_t, entity: i53?): i53
local entity_index = world.entity_index
if entity then
local index = ECS_ID(entity)
local max_id = entity_index.max_id
local sparse_array = entity_index.sparse_array
local dense_array = entity_index.dense_array
local alive_count = entity_index.alive_count
local r = sparse_array[index]
if r then
local dense = r.dense
if not dense or dense == 0 then
dense = index
end
local any = dense_array[dense]
if any == entity then
if alive_count > dense then
return entity
end
local e_swap = dense_array[alive_count]
local r_swap = sparse_array[alive_count]
r_swap.dense = dense
r.dense = alive_count
dense_array[alive_count] = entity
dense_array[dense] = e_swap
return entity
end
-- assert(any ~= 0) should never happen
local e_swap = dense_array[alive_count]
local r_swap = sparse_array[alive_count]
if dense <= alive_count then
alive_count += 1
entity_index.alive_count = alive_count
end
r_swap.dense = dense
r.dense = alive_count
dense_array[alive_count] = any
dense_array[dense] = e_swap
return any
else
for i = max_id + 1, index do
sparse_array[i] = { dense = i } :: ecs_record_t
dense_array[i] = i
end
entity_index.max_id = index
local e_swap = dense_array[alive_count]
local r_swap = sparse_array[alive_count]
r_swap.dense = index
alive_count += 1
entity_index.alive_count = alive_count
r = sparse_array[index]
r.dense = alive_count
sparse_array[index] = r
dense_array[index] = e_swap
dense_array[alive_count] = entity
return entity
end
end
return entity_index_new_id(entity_index, entity)
end end
local function world_parent(world: ecs_world_t, entity: i53) local function world_parent(world: ecs_world_t, entity: i53)
@ -2311,6 +2422,8 @@ export type EntityIndex = {
sparse_array: Map<i24, Record>, sparse_array: Map<i24, Record>,
alive_count: number, alive_count: number,
max_id: number, max_id: number,
range_begin: number?,
range_end: number?
} }
local World = {} local World = {}
@ -2332,6 +2445,7 @@ World.contains = world_contains
World.cleanup = world_cleanup World.cleanup = world_cleanup
World.each = world_each World.each = world_each
World.children = world_children World.children = world_children
World.range = world_range
local function world_new() local function world_new()
local entity_index = { local entity_index = {
@ -2454,6 +2568,9 @@ export type World = {
observable: any, observable: any,
--- Enforce a check on entities to be created within desired range
range: (self: World, range_begin: number, range_end: number?) -> (),
--- Creates a new entity --- Creates a new entity
entity: (self: World, id: Entity?) -> Entity, entity: (self: World, id: Entity?) -> Entity,
--- Creates a new entity located in the first 256 ids. --- Creates a new entity located in the first 256 ids.
@ -2558,6 +2675,8 @@ return {
ECS_META_RESET = ECS_META_RESET, ECS_META_RESET = ECS_META_RESET,
IS_PAIR = (ECS_IS_PAIR :: any) :: <P, O>(pair: Pair<P, O>) -> boolean, IS_PAIR = (ECS_IS_PAIR :: any) :: <P, O>(pair: Pair<P, O>) -> boolean,
ECS_PAIR_FIRST = ECS_PAIR_FIRST,
ECS_PAIR_SECOND = ECS_PAIR_SECOND,
pair_first = (ecs_pair_first :: any) :: <P, O>(world: World, pair: Pair<P, O>) -> Id<P>, pair_first = (ecs_pair_first :: any) :: <P, O>(world: World, pair: Pair<P, O>) -> Id<P>,
pair_second = (ecs_pair_second :: any) :: <P, O>(world: World, pair: Pair<P, O>) -> Id<O>, pair_second = (ecs_pair_second :: any) :: <P, O>(world: World, pair: Pair<P, O>) -> Id<O>,
entity_index_get_alive = entity_index_get_alive, entity_index_get_alive = entity_index_get_alive,

View file

@ -247,6 +247,7 @@ TEST("world:component()", function()
end) end)
TEST("world:contains()", function() TEST("world:contains()", function()
local tag = jecs.tag() local tag = jecs.tag()
local world = jecs.world() local world = jecs.world()
local id = world:entity() local id = world:entity()
@ -258,6 +259,7 @@ TEST("world:contains()", function()
CHECK(not world:contains(id)) CHECK(not world:contains(id))
end end
CHECK(world:contains(tag)) CHECK(world:contains(tag))
jecs.ECS_META_RESET() jecs.ECS_META_RESET()
end) end)
@ -631,7 +633,62 @@ TEST("world:each()", function()
end end
end) end)
TEST("world:range()", function()
do CASE ""
local world = jecs.world()
world = lifetime_tracker_add(world, {})
world:range(1000, 2000)
local e = world:entity()
CHECK(e == 1000)
world:entity(1590)
CHECK(world:entity(5000) == 5000)
CHECK(world:contains(1590))
world:set(591, jecs.Name, "9888")
CHECK(not world:contains(591))
CHECK(world:contains(5000))
CHECK(not world:contains(988))
local e = world:entity(2000)
CHECK(e == 2000)
end
end)
TEST("world:entity()", function() TEST("world:entity()", function()
do CASE "desired id"
local world = jecs.world()
local id = world:entity()
local e = world:entity(id + 5)
CHECK(e == id + 5)
CHECK(world:contains(e))
local e2 = world:entity()
CHECK(world:contains(e2))
-- world:print_entity_index()
local e3 = world:entity(275)
CHECK(e3 == 275)
CHECK(world:contains(e3))
CHECK(e3 == world:entity(e3))
world:delete(e3)
local e3v1 = world:entity(275)
CHECK(not world:contains(275))
CHECK(jecs.ECS_GENERATION(e3v1) == 1)
CHECK(jecs.ECS_ID(e3v1) == 275)
CHECK(world:contains(e3v1))
-- world:print_entity_index()
world:entity(e3)
world:entity(e3)
world:entity(275)
end
local N = 2^8 local N = 2^8
do CASE "unique IDs" do CASE "unique IDs"

View file

@ -28,7 +28,7 @@ end
local padding_enabled = false local padding_enabled = false
local function pad() local function pad()
if padding_enabled then if padding_enabled then
print("") print("------------------")
end end
end end
@ -64,7 +64,8 @@ local function lifetime_tracker_add(world: jecs.World, opt): PatchedWorld
world.print_entity_index = function() world.print_entity_index = function()
local max_id = entity_index.max_id local max_id = entity_index.max_id
local alive_count = entity_index.alive_count local alive_count = entity_index.alive_count
local alive = table.move(dense_array, 1 + jecs.Rest :: any, alive_count, 1, {}) local range_begin = entity_index.range_begin or jecs.Rest + 1
local alive = table.move(dense_array, range_begin :: any, alive_count, 1, {})
local dead = table.move(dense_array, alive_count + 1, max_id, 1, {}) local dead = table.move(dense_array, alive_count + 1, max_id, 1, {})
local sep = "|--------|" local sep = "|--------|"

View file

@ -229,7 +229,6 @@ end
local test_focused = false local test_focused = false
local function TEST(name: string, fn: () -> ()) local function TEST(name: string, fn: () -> ())
test = { test = {
name = name, name = name,
cases = {}, cases = {},
@ -244,12 +243,11 @@ local function TEST(name: string, fn: () -> ())
test.focus = true test.focus = true
test_focused = true test_focused = true
end end
table.insert(tests, t) table.insert(tests, t)
end end
local function FOCUS() local function FOCUS()
assert(test, "no active test")
check_for_focused = true check_for_focused = true
test_focused = false test_focused = false
end end