Compare commits

..

1 commit

Author SHA1 Message Date
Clown
0355cc11b7
Merge 96bed9bd7e into 8822be58a9 2025-04-22 14:16:27 -07:00
9 changed files with 69 additions and 373 deletions

View file

@ -11,8 +11,6 @@ The format is based on [Keep a Changelog][kac], and this project adheres to
## [Unreleased]
- `[world]`:
- Added `world:range` to restrict entity range
- Changed `world:entity` to accept the overload to create an entity at the desired index
- Changed `world:clear` to also look through the component record for the cleared `ID`
- 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

View file

@ -24,7 +24,7 @@ local function observers_new(world, description)
end
local entity_index = world.entity_index :: any
local function emplaced(entity, id, value)
local function emplaced(entity: jecs.Entity)
local r = jecs.entity_index_try_get_fast(
entity_index, entity :: any)
@ -45,106 +45,6 @@ local function observers_new(world, description)
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 query = description.query
local callback = description.callback
@ -193,11 +93,10 @@ local function monitors_new(world, description)
end
local function observers_add(world: jecs.World & { [string]: any }): PatchedWorld
type Signal = { [jecs.Entity]: { (...any) -> () } }
local signals = {
added = {} :: Signal,
emplaced = {} :: Signal,
removed = {} :: Signal
added = {},
emplaced = {},
removed = {}
}
world.added = function(_, component, fn)
@ -207,21 +106,13 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
if not listeners then
listeners = {}
signals.added[component] = listeners
local function on_add(entity: number, id: number, value: any)
for _, listener in listeners :: any do
listener(entity, id, value)
end
end
world:set(component, jecs.OnAdd, on_add)
end
world:set(component, jecs.OnAdd, on_add) end
table.insert(listeners, fn)
return function()
local n = #listeners
local i = table.find(listeners, fn)
listeners[i] = listeners[n]
listeners[n] = nil
end
end
world.changed = function(_, component, fn)
@ -239,12 +130,6 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
world:set(component, jecs.OnChange, on_change)
end
table.insert(listeners, fn)
return function()
local n = #listeners
local i = table.find(listeners, fn)
listeners[i] = listeners[n]
listeners[n] = nil
end
end
world.removed = function(_, component, fn)
@ -262,12 +147,6 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
world:set(component, jecs.OnRemove, on_remove)
end
table.insert(listeners, fn)
return function()
local n = #listeners
local i = table.find(listeners, fn)
listeners[i] = listeners[n]
listeners[n] = nil
end
end
world.signals = signals
@ -276,8 +155,6 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
world.monitor = monitors_new
world.trackers = {}
return world :: PatchedWorld
end

View file

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

View file

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

194
jecs.luau
View file

@ -62,8 +62,6 @@ type ecs_entity_index_t = {
sparse_array: Map<i24, ecs_record_t>,
alive_count: number,
max_id: number,
range_begin: number?,
range_end: number?
}
type ecs_query_data_t = {
@ -127,12 +125,6 @@ local ECS_INTERNAL_ERROR = [[
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_max_component_id = 0
local ecs_max_tag_id = EcsRest
@ -193,10 +185,6 @@ local function ECS_ENTITY_T_LO(e: i53): i24
return e % ECS_ENTITY_MASK
end
local function ECS_ID(e: i53)
return e % ECS_ENTITY_MASK
end
local function ECS_GENERATION(e: i53)
return e // ECS_ENTITY_MASK
end
@ -261,10 +249,10 @@ local function entity_index_is_alive(entity_index: ecs_entity_index_t, entity: i
return entity_index_try_get(entity_index, entity) ~= nil
end
local function entity_index_get_alive(entity_index: ecs_entity_index_t, entity: i53): i53?
local r = entity_index_try_get_any(entity_index, entity)
local function entity_index_get_alive(index: ecs_entity_index_t, entity: i53): i53?
local r = entity_index_try_get_any(index, entity)
if r then
return entity_index.dense_array[r.dense]
return index.dense_array[r.dense]
end
return nil
end
@ -292,15 +280,11 @@ local function ecs_get_alive(world, entity)
return current
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 dense_array = entity_index.dense_array
local alive_count = entity_index.alive_count
local sparse_array = entity_index.sparse_array
local max_id = entity_index.max_id
if alive_count < max_id then
if alive_count ~= max_id then
alive_count += 1
entity_index.alive_count = alive_count
local id = dense_array[alive_count]
@ -308,14 +292,11 @@ local function entity_index_new_id(entity_index: ecs_entity_index_t): i53
end
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
alive_count += 1
entity_index.alive_count = alive_count
dense_array[alive_count] = id
sparse_array[id] = { dense = alive_count } :: ecs_record_t
entity_index.sparse_array[id] = { dense = alive_count } :: ecs_record_t
return id
end
@ -602,10 +583,10 @@ local function id_record_ensure(world: ecs_world_t, id: number): ecs_id_record_t
local is_pair = ECS_IS_PAIR(id)
if is_pair then
relation = entity_index_get_alive(entity_index, ECS_PAIR_FIRST(id)) :: i53
ecs_assert(relation and entity_index_is_alive(
assert(relation and entity_index_is_alive(
entity_index, relation), ECS_INTERNAL_ERROR)
target = entity_index_get_alive(entity_index, ECS_PAIR_SECOND(id)) :: i53
ecs_assert(target and entity_index_is_alive(
assert(target and entity_index_is_alive(
entity_index, target), ECS_INTERNAL_ERROR)
end
@ -738,101 +719,8 @@ local function archetype_create(world: ecs_world_t, id_types: { i24 }, ty, prev:
return archetype
end
local function world_range(world: ecs_world_t, range_begin: number, range_end: number?)
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 + 1, 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
r.dense = dense
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)
local function world_entity(world: ecs_world_t): i53
return entity_index_new_id(world.entity_index)
end
local function world_parent(world: ecs_world_t, entity: i53)
@ -1437,24 +1325,19 @@ local function world_delete(world: ecs_world_t, entity: i53)
end
local dense_array = entity_index.dense_array
local dense = record.dense
local i_swap = entity_index.alive_count
entity_index.alive_count = i_swap - 1
local index_of_deleted_entity = record.dense
local index_of_last_alive_entity = entity_index.alive_count
entity_index.alive_count = index_of_last_alive_entity - 1
local e_swap = dense_array[i_swap]
local r_swap = entity_index_try_get_any(entity_index, e_swap) :: ecs_record_t
r_swap.dense = dense
local last_alive_entity = dense_array[index_of_last_alive_entity]
local r_swap = entity_index_try_get_any(entity_index, last_alive_entity) :: ecs_record_t
r_swap.dense = index_of_deleted_entity
record.archetype = nil :: any
record.row = nil :: any
record.dense = i_swap
record.dense = index_of_last_alive_entity
dense_array[dense] = e_swap
dense_array[i_swap] = ECS_GENERATION_INC(entity)
end
local function world_exists(world: ecs_world_t, entity): boolean
return entity_index_try_get_any(world.entity_index, entity) ~= nil
dense_array[index_of_deleted_entity] = last_alive_entity
dense_array[index_of_last_alive_entity] = ECS_GENERATION_INC(entity)
end
local function world_contains(world: ecs_world_t, entity): boolean
@ -2428,8 +2311,6 @@ export type EntityIndex = {
sparse_array: Map<i24, Record>,
alive_count: number,
max_id: number,
range_begin: number?,
range_end: number?
}
local World = {}
@ -2448,11 +2329,9 @@ World.has = world_has
World.target = world_target
World.parent = world_parent
World.contains = world_contains
World.exists = world_exists
World.cleanup = world_cleanup
World.each = world_each
World.children = world_children
World.range = world_range
local function world_new()
local entity_index = {
@ -2488,6 +2367,21 @@ local function world_new()
entity_index_new_id(entity_index)
end
for i = EcsRest + 1, ecs_max_tag_id do
-- Initialize built-in components
entity_index_new_id(entity_index)
end
for i, bundle in ecs_metadata do
for ty, value in bundle do
if value == NULL then
world_add(self, i, ty)
else
world_set(self, i, ty, value)
end
end
end
world_add(self, EcsName, EcsComponent)
world_add(self, EcsOnChange, EcsComponent)
world_add(self, EcsOnAdd, EcsComponent)
@ -2510,20 +2404,6 @@ local function world_new()
world_add(self, EcsChildOf, ECS_PAIR(EcsOnDeleteTarget, EcsDelete))
for i = EcsRest + 1, ecs_max_tag_id do
entity_index_new_id(entity_index)
end
for i, bundle in ecs_metadata do
for ty, value in bundle do
if value == NULL then
world_add(self, i, ty)
else
world_set(self, i, ty, value)
end
end
end
return self
end
@ -2574,9 +2454,6 @@ export type World = {
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
entity: (self: World, id: Entity?) -> Entity,
--- Creates a new entity located in the first 256 ids.
@ -2616,9 +2493,6 @@ export type World = {
--- Checks if the world contains the given entity
contains:(self: World, entity: Entity) -> boolean,
--- Checks if the entity exists
exists: (self: World, entity: Entity) -> boolean,
each: <T>(self: World, id: Id<T>) -> () -> Entity,
children: <T>(self: World, id: Id<T>) -> () -> Entity,
@ -2684,8 +2558,6 @@ return {
ECS_META_RESET = ECS_META_RESET,
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_second = (ecs_pair_second :: any) :: <P, O>(world: World, pair: Pair<P, O>) -> Id<O>,
entity_index_get_alive = entity_index_get_alive,

View file

@ -247,7 +247,6 @@ TEST("world:component()", function()
end)
TEST("world:contains()", function()
local tag = jecs.tag()
local world = jecs.world()
local id = world:entity()
@ -259,7 +258,6 @@ TEST("world:contains()", function()
CHECK(not world:contains(id))
end
CHECK(world:contains(tag))
jecs.ECS_META_RESET()
end)
@ -633,62 +631,7 @@ TEST("world:each()", function()
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()
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
do CASE "unique IDs"

View file

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

View file

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