Compare commits

..

10 commits

Author SHA1 Message Date
Ukendio
d3b032666b Index is not a stable pointer
Some checks failed
analysis / Run Luau Analyze (push) Has been cancelled
unit-testing / Run Luau Tests (push) Has been cancelled
2025-04-25 23:53:09 +02:00
Ukendio
b1a2bc48a7 No nullable records 2025-04-25 23:41:32 +02:00
Ukendio
1002139fc9 Only max_id and alive_count if range_begin is larger than built in
ranges
2025-04-25 23:05:51 +02:00
Ukendio
d237b176e6 Fix listener array indexing in observers 2025-04-25 22:45:41 +02:00
Ukendio
0816c645da Merge branch 'Add-entity-ranges' of https://github.com/Ukendio/jecs into Add-entity-ranges 2025-04-25 22:36:29 +02:00
Ukendio
6a92d4e3c6 Add ecs_assert and fix entity range handling 2025-04-25 22:36:07 +02:00
Marcus
50363ac474
Merge branch 'main' into Add-entity-ranges 2025-04-25 22:08:32 +02:00
Ukendio
5b9c032451 Remove appending to array 2025-04-25 22:08:03 +02:00
Ukendio
5b31a1af22 Allow to disconnect signals 2025-04-25 22:07:22 +02:00
Ukendio
846722ba58 Initialize bundle after builtn components are set
Some checks are pending
analysis / Run Luau Analyze (push) Waiting to run
deploy-docs / build (push) Waiting to run
deploy-docs / Deploy (push) Blocked by required conditions
publish-npm / publish (push) Waiting to run
unit-testing / Run Luau Tests (push) Waiting to run
2025-04-25 15:47:51 +02:00
5 changed files with 179 additions and 46 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

@ -24,7 +24,7 @@ local function observers_new(world, description)
end end
local entity_index = world.entity_index :: any local entity_index = world.entity_index :: any
local function emplaced(entity: jecs.Entity) local function emplaced(entity, id, value)
local r = jecs.entity_index_try_get_fast( local r = jecs.entity_index_try_get_fast(
entity_index, entity :: any) entity_index, entity :: any)
@ -45,6 +45,106 @@ local function observers_new(world, description)
end end
end end
local function join(world, component)
local sparse_array = {}
local dense_array = {}
local values = {}
local max_id = 0
world:added(component, function(entity, id, value)
max_id += 1
sparse_array[entity] = max_id
dense_array[max_id] = entity
values[max_id] = value
end)
world:removed(component, function(entity, id)
local e_swap = dense_array[max_id]
local v_swap = values[max_id]
local dense = sparse_array[entity]
dense_array[dense] = e_swap
values[dense] = v_swap
sparse_array[entity] = nil
dense_array[max_id] = nil
values[max_id] = nil
end)
world:changed(component, function(entity, id, value)
values[sparse_array[entity]] = value
end)
return function()
local i = max_id
return function(): ...any
i -= 1
if i == 0 then
return nil
end
local e = dense_array[i]
return e, values[i]
end
end
end
local function query_changed(world, component)
assert(jecs.IS_PAIR(component) == false)
local callerid = debug.info(2, "sl")
local tracker = world.trackers[callerid]
if not tracker then
local records = {}
local connections = {}
tracker = {
records = records,
connections = connections
}
world.trackers[callerid] = tracker
table.insert(connections, world:added(component, function(entity, id, v)
tracker[entity] = {
new = v
}
end))
table.insert(connections, world:changed(component, function(entity, id, v)
local record = tracker[entity]
record.old = record.new
record.new = v
end))
table.insert(connections, world:removed(component, function(entity, id)
local record = tracker[entity]
record.old = record.new
record.new = nil
end))
end
local entity = nil
local record = nil
return function()
entity, record = next(tracker, entity)
if entity == nil then
return
end
return entity, record
end
end
local function spy_on_world_delete(world)
local world_delete = world.delete
world.delete = function(world, entity)
world_delete(world, entity)
for _, tracker in world.trackers do
tracker.records[entity] = nil
for _, connection in tracker.connections do
connection()
end
end
end
end
local function monitors_new(world, description) local function monitors_new(world, description)
local query = description.query local query = description.query
local callback = description.callback local callback = description.callback
@ -93,10 +193,11 @@ local function monitors_new(world, description)
end end
local function observers_add(world: jecs.World & { [string]: any }): PatchedWorld local function observers_add(world: jecs.World & { [string]: any }): PatchedWorld
type Signal = { [jecs.Entity]: { (...any) -> () } }
local signals = { local signals = {
added = {}, added = {} :: Signal,
emplaced = {}, emplaced = {} :: Signal,
removed = {} removed = {} :: Signal
} }
world.added = function(_, component, fn) world.added = function(_, component, fn)
@ -115,6 +216,12 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
world:set(component, jecs.OnAdd, on_add) world:set(component, jecs.OnAdd, on_add)
end end
table.insert(listeners, fn) table.insert(listeners, fn)
return function()
local n = #listeners
local i = table.find(listeners, fn)
listeners[i] = listeners[n]
listeners[n] = nil
end
end end
world.changed = function(_, component, fn) world.changed = function(_, component, fn)
@ -132,6 +239,12 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
world:set(component, jecs.OnChange, on_change) world:set(component, jecs.OnChange, on_change)
end end
table.insert(listeners, fn) table.insert(listeners, fn)
return function()
local n = #listeners
local i = table.find(listeners, fn)
listeners[i] = listeners[n]
listeners[n] = nil
end
end end
world.removed = function(_, component, fn) world.removed = function(_, component, fn)
@ -149,6 +262,12 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
world:set(component, jecs.OnRemove, on_remove) world:set(component, jecs.OnRemove, on_remove)
end end
table.insert(listeners, fn) table.insert(listeners, fn)
return function()
local n = #listeners
local i = table.find(listeners, fn)
listeners[i] = listeners[n]
listeners[n] = nil
end
end end
world.signals = signals world.signals = signals
@ -157,6 +276,8 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
world.monitor = monitors_new world.monitor = monitors_new
world.trackers = {}
return world :: PatchedWorld return world :: PatchedWorld
end end

View file

@ -5,23 +5,29 @@ local ReplicatedStorage = game:GetService("ReplicatedStorage")
local jecs = require(ReplicatedStorage.Lib:Clone()) local jecs = require(ReplicatedStorage.Lib:Clone())
local mirror = require(ReplicatedStorage.mirror:Clone()) local mirror = require(ReplicatedStorage.mirror:Clone())
local ecs = jecs.World.new()
local mcs = mirror.world()
return { return {
ParameterGenerator = function() ParameterGenerator = function()
local ecs = jecs.world()
ecs:range(1000, 20000)
local mcs = mirror.World.new()
return ecs, mcs
end, end,
Functions = { Functions = {
Mirror = function() Mirror = function(_, ecs, mcs)
for i = 1000, 1100 do for i = 1, 100 do
mcs:entity(i)
mcs:entity()
end end
end, end,
Jecs = function()
for i = 1, 1000 do Jecs = function(_, ecs, mcs)
ecs:entity(i) for i = 1, 100 do
ecs:entity()
end end
end, end,
}, },
} }

View file

@ -120,7 +120,6 @@ local ECS_GENERATION_MASK = bit32.lshift(1, 16)
local NULL_ARRAY = table.freeze({}) :: Column local NULL_ARRAY = table.freeze({}) :: Column
local NULL = newproxy(false) local NULL = newproxy(false)
local NULL_RECORD = table.freeze({ dense = 0 }) :: ecs_record_t
local ECS_INTERNAL_ERROR = [[ local ECS_INTERNAL_ERROR = [[
This is an internal error, please file a bug report via the following link: This is an internal error, please file a bug report via the following link:
@ -128,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
@ -287,6 +292,8 @@ 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
@ -302,9 +309,8 @@ local function entity_index_new_id(entity_index: ecs_entity_index_t): i53
local id = max_id + 1 local id = max_id + 1
local range_end = entity_index.range_end local range_end = entity_index.range_end
if range_end then ecs_assert(range_end == nil or id < range_end, ECS_INTERNAL_ERROR_INCOMPATIBLE_ENTITY)
assert(id < range_end)
end
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
@ -596,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
@ -734,37 +740,31 @@ end
local function world_range(world: ecs_world_t, range_begin: number, range_end: number?) local function world_range(world: ecs_world_t, range_begin: number, range_end: number?)
local entity_index = world.entity_index local entity_index = world.entity_index
if range_end then
range_end += 1
end
entity_index.range_begin = range_begin entity_index.range_begin = range_begin
entity_index.range_end = range_end entity_index.range_end = range_end
local max_id = entity_index.max_id local max_id = entity_index.max_id
if range_begin > max_id then
local dense_array = entity_index.dense_array local dense_array = entity_index.dense_array
local sparse_array = entity_index.sparse_array local sparse_array = entity_index.sparse_array
if range_begin > max_id then for i = max_id, range_begin do
for i = max_id, range_begin - 1 do dense_array[i] = i
dense_array[i] = 0 sparse_array[i] = {
sparse_array[i] = NULL_RECORD dense = 0
} :: ecs_record_t
end end
sparse_array[range_begin] = { dense = 0 } :: ecs_record_t entity_index.max_id = range_begin - 1
entity_index.alive_count = range_begin - 1
end end
entity_index.max_id = range_begin
entity_index.alive_count = range_begin
end end
local function world_entity(world: ecs_world_t, entity: i53?): i53 local function world_entity(world: ecs_world_t, entity: i53?): i53
local entity_index = world.entity_index local entity_index = world.entity_index
if entity then if entity then
local index = ECS_ID(entity) local index = ECS_ID(entity)
local range_begin = entity_index.range_begin
if range_begin then
assert(index > range_begin)
end
local max_id = entity_index.max_id local max_id = entity_index.max_id
local sparse_array = entity_index.sparse_array local sparse_array = entity_index.sparse_array
local dense_array = entity_index.dense_array local dense_array = entity_index.dense_array
@ -805,10 +805,6 @@ local function world_entity(world: ecs_world_t, entity: i53?): i53
dense_array[dense] = e_swap dense_array[dense] = e_swap
return any return any
else else
local range_end = entity_index.range_end
if range_end then
assert(index < range_end)
end
for i = max_id + 1, index do for i = max_id + 1, index do
sparse_array[i] = { dense = i } :: ecs_record_t sparse_array[i] = { dense = i } :: ecs_record_t
dense_array[i] = i dense_array[i] = i
@ -2572,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.

View file

@ -633,24 +633,30 @@ TEST("world:each()", function()
end end
end) end)
TEST("world:entity()", function() TEST("world:range()", function()
do CASE "range" do CASE ""
local world = jecs.world() local world = jecs.world()
world = lifetime_tracker_add(world, {}) world = lifetime_tracker_add(world, {})
world:range(1000, 2000) world:range(1000, 2000)
local e = world:entity()
CHECK(e == 1000)
world:entity(1590) world:entity(1590)
CHECK_EXPECT_ERR(function()
world:entity(5000) CHECK(world:entity(5000) == 5000)
end)
CHECK(world:contains(1590)) CHECK(world:contains(1590))
world:set(591, jecs.Name, "9888") world:set(591, jecs.Name, "9888")
CHECK(not world:contains(591)) CHECK(not world:contains(591))
CHECK(not world:contains(5000)) CHECK(world:contains(5000))
CHECK(not world:contains(988)) CHECK(not world:contains(988))
local e = world:entity(2000)
CHECK(e == 2000)
end end
end)
TEST("world:entity()", function()
do CASE "desired id" do CASE "desired id"
local world = jecs.world() local world = jecs.world()
local id = world:entity() local id = world:entity()