Compare commits

..

No commits in common. "d3b032666bf5e3670eb9eff4538feab4b5cb3f62" and "cb238ab29e9686ff53ab9c9b915d35d519be8916" have entirely different histories.

5 changed files with 45 additions and 178 deletions

View file

@ -11,7 +11,6 @@ The format is based on [Keep a Changelog][kac], and this project adheres to
## [Unreleased]
- `[world]`:
- Added `world:range` to allow for creating
- 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)
@ -216,12 +115,6 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
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 +132,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 +149,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 +157,6 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl
world.monitor = monitors_new
world.trackers = {}
return world :: PatchedWorld
end

View file

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

View file

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

View file

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