Skip to content

Commit

Permalink
Fast paths on entity_index_try_get for user facing api
Browse files Browse the repository at this point in the history
  • Loading branch information
Ukendio committed Nov 16, 2024
1 parent 0ab66f4 commit eb31506
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,16 @@ local function entity_index_try_get(entity_index: EntityIndex, entity: number):
return r
end

local function entity_index_try_get_fast(entity_index: EntityIndex, entity: number): Record?
local r = entity_index.sparse_array[ECS_ENTITY_T_LO(entity)]
if r then
if entity_index.dense_array[r.dense] ~= entity then
return nil
end
end
return r
end

local function entity_index_get_alive(index: EntityIndex, e: i24): i53
local r = entity_index_try_get_any(index, e)
if r then
Expand Down Expand Up @@ -387,7 +397,7 @@ local function world_get_one_inline(world: World, entity: i53, id: i53): any
end

local function world_has_one_inline(world: World, entity: number, id: i53): boolean
local record = entity_index_try_get(world.entity_index, entity)
local record = entity_index_try_get_fast(world.entity_index, entity)
if not record then
return false
end
Expand All @@ -403,7 +413,7 @@ local function world_has_one_inline(world: World, entity: number, id: i53): bool
end

local function world_has(world: World, entity: number, ...: i53): boolean
local record = entity_index_try_get(world.entity_index, entity)
local record = entity_index_try_get_fast(world.entity_index, entity)
if not record then
return false
end
Expand All @@ -426,7 +436,7 @@ end

local function world_target(world: World, entity: i53, relation: i24, index: number?): i24?
local nth = index or 0
local record = entity_index_try_get(world.entity_index, entity)
local record = entity_index_try_get_fast(world.entity_index, entity)
if not record then
return nil
end
Expand Down Expand Up @@ -735,7 +745,7 @@ end

local function world_add(world: World, entity: i53, id: i53): ()
local entity_index = world.entity_index
local record = entity_index_try_get(entity_index, entity)
local record = entity_index_try_get_fast(entity_index, entity)
if not record then
return
end
Expand Down Expand Up @@ -763,7 +773,7 @@ end

local function world_set(world: World, entity: i53, id: i53, data: unknown): ()
local entity_index = world.entity_index
local record = entity_index_try_get(entity_index, entity)
local record = entity_index_try_get_fast(entity_index, entity)
if not record then
return
end
Expand Down Expand Up @@ -836,7 +846,7 @@ end

local function world_remove(world: World, entity: i53, id: i53)
local entity_index = world.entity_index
local record = entity_index_try_get(entity_index, entity)
local record = entity_index_try_get_fast(entity_index, entity)
if not record then
return
end
Expand Down

0 comments on commit eb31506

Please sign in to comment.