Skip to content

Commit

Permalink
Make world:set idempotent for tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Ukendio committed Aug 31, 2024
1 parent d617963 commit aa3e025
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 144 deletions.
273 changes: 138 additions & 135 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,135 +1,138 @@
# Jecs Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog][kac], and this project adheres to
[Semantic Versioning][semver].

[kac]: https://keepachangelog.com/en/1.1.0/
[semver]: https://semver.org/spec/v2.0.0.html

## [Unreleased]
- `[traits]`:
- Added cleanup condition `jecs.OnDelete` for when the entity or component is deleted
- Added cleanup action `jecs.Remove` which removes instances of the specified (component) id from all entities
- This is the default cleanup action
- Added component trait `jecs.Tag` which allows for zero-cost components used as tags
- Setting data to a component with this trait will do nothing
- `[luau]`:
- Exported `world:contains()`
- Exported `query:drain()`
- Exported `Query`
- Improved types for the hook `OnAdd`, `OnSet`, `OnRemove`
- Changed functions to accept any ID including pairs in type parameters
- Applies to `world:add()`, `world:set()`, `world:remove()`, `world:get()`, `world:has()` and `world:query()`
- New exported type `Id<T = nil> = Entity<T> | Pair`
- Changed `world:contains()` to return a `boolean` instead of an entity which may or may not exist
- Fixed `world:has()` to take the correct parameters

## [0.2.2] - 2024-07-07

### Added

- Added `query:replace(function(...T) return ...U end)` for replacing components in place
- Method is fast pathed to replace the data to the components for each corresponding entity

### Changed

- Iterator now goes backwards instead to prevent common cases of iterator invalidation

## [0.2.1] - 2024-07-06

### Added

- Added `jecs.Component` built-in component which will be added to ids created with `world:component()`.
- Used to find every component id with `query(jecs.Component)

## [0.2.0] - 2024-07-03

### Added

- Added `world:parent(entity)` and `jecs.ChildOf` respectively as first class citizen for building parent-child relationships.
- Give a parent to an entity with `world:add($source, pair(ChildOf, $target))`
- Use `world:parent(entity)` to find the target of the relationship
- Added user-facing Luau types

### Changed
- Improved iteration speeds 20-40% by manually indexing rather than using `next()` :scream:


## [0.1.1] - 2024-05-19

### Added

- Added `world:clear(entity)` for removing the components to the corresponding entity
- Added Typescript Types

## [0.1.0] - 2024-05-13

### Changed
- Optimized iterator

## [0.1.0-rc.6] - 2024-05-13

### Added

- Added a `jecs.Wildcard` term
- it lets you query any partially matched pairs

## [0.1.0-rc.5] - 2024-05-10

### Added

- Added Entity relationships for creating logical connections between entities
- Added `world:__iter method` which allows for iteration over the whole world to get every entity
- used for reconciling whole worlds such as via replication, saving/loading, etc
- Added `world:add(entity, component)` which adds a component to the entity
- it is an idempotent function, so calling it twice and in any order should be fine

### Fixed
- Fixed component overriding when in disorder
- Previously setting the components in different order results in it overriding component data because it incorrectly mapped the index of the column. So it took the index from the source archetype rather than the destination archetype

## [0.0.0-prototype.rc.3] - 2024-05-01

### Added

- Added observers
- Added an arm to query `query:without()` for chaining invariants.

### Changed
- Separates ranges for components and entity IDs.
- IDs created with `world:component()` will promote array lookups rather than map lookups in the `componentIndex` which is a significant boost

- No longer caches the column pointers directly and instead the column indices which stay persistent even when data is reallocated during swap-removals
- This was an issue with the iterator being invalidated when you move an entity to a different archetype.

### Fixedhttps://github.com/Ukendio/jecs/releases/tag/v0.0.0-prototype.rc.3

- Fixed a bug where changing an existing component would be slow because it was always appending changing the row of the entity record
- The fix dramatically improves times where it is basically down to just the speed of setting a field in a table

## [0.0.0-prototype.rc.2] - 2024-04-26

### Changed
- Optimized the creation of the query
- It will now finds the smallest archetype map to iterate over
- Optimized the query iterator
- It will now populates iterator with columns for faster indexing

- Renamed the insertion method from world:add to world:set to better reflect what it does.

## [0.0.0-prototype.rc.2] - 2024-04-23
- Initial release

[unreleased]: https://github.com/ukendio/jecs/compare/v0.0.0.0-prototype.rc.2...HEAD
[0.2.2]: https://github.com/ukendio/jecs/releases/tag/v0.2.2
[0.2.1]: https://github.com/ukendio/jecs/releases/tag/v0.2.1
[0.2.0]: https://github.com/ukendio/jecs/releases/tag/v0.2.0
[0.1.1]: https://github.com/ukendio/jecs/releases/tag/v0.1.1
[0.1.0]: https://github.com/ukendio/jecs/releases/tag/v0.1.0
[0.1.0-rc.6]: https://github.com/ukendio/jecs/releases/tag/v0.1.0-rc.6
[0.1.0-rc.5]: https://github.com/ukendio/jecs/releases/tag/v0.1.0-rc.5
[0.0.0-prototype-rc.3]: https://github.com/ukendio/jecs/releases/tag/v0.0.0-prototype.rc.3
[0.0.0-prototype.rc.2]: https://github.com/ukendio/jecs/releases/tag/v0.0.0-prototype.rc.2
[0.0.0-prototype-rc.1]: https://github.com/ukendio/jecs/releases/tag/v0.0.0-prototype.rc.1
# Jecs Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog][kac], and this project adheres to
[Semantic Versioning][semver].

[kac]: https://keepachangelog.com/en/1.1.0/
[semver]: https://semver.org/spec/v2.0.0.html

## [Unreleased]
- `[world]`:
- Improved performance for hooks
- Changed `world:set` to be idempotent when setting tags
- `[traits]`:
- Added cleanup condition `jecs.OnDelete` for when the entity or component is deleted
- Added cleanup action `jecs.Remove` which removes instances of the specified (component) id from all entities
- This is the default cleanup action
- Added component trait `jecs.Tag` which allows for zero-cost components used as tags
- Setting data to a component with this trait will do nothing
- `[luau]`:
- Exported `world:contains()`
- Exported `query:drain()`
- Exported `Query`
- Improved types for the hook `OnAdd`, `OnSet`, `OnRemove`
- Changed functions to accept any ID including pairs in type parameters
- Applies to `world:add()`, `world:set()`, `world:remove()`, `world:get()`, `world:has()` and `world:query()`
- New exported type `Id<T = nil> = Entity<T> | Pair`
- Changed `world:contains()` to return a `boolean` instead of an entity which may or may not exist
- Fixed `world:has()` to take the correct parameters

## [0.2.2] - 2024-07-07

### Added

- Added `query:replace(function(...T) return ...U end)` for replacing components in place
- Method is fast pathed to replace the data to the components for each corresponding entity

### Changed

- Iterator now goes backwards instead to prevent common cases of iterator invalidation

## [0.2.1] - 2024-07-06

### Added

- Added `jecs.Component` built-in component which will be added to ids created with `world:component()`.
- Used to find every component id with `query(jecs.Component)

## [0.2.0] - 2024-07-03

### Added

- Added `world:parent(entity)` and `jecs.ChildOf` respectively as first class citizen for building parent-child relationships.
- Give a parent to an entity with `world:add($source, pair(ChildOf, $target))`
- Use `world:parent(entity)` to find the target of the relationship
- Added user-facing Luau types

### Changed
- Improved iteration speeds 20-40% by manually indexing rather than using `next()` :scream:


## [0.1.1] - 2024-05-19

### Added

- Added `world:clear(entity)` for removing the components to the corresponding entity
- Added Typescript Types

## [0.1.0] - 2024-05-13

### Changed
- Optimized iterator

## [0.1.0-rc.6] - 2024-05-13

### Added

- Added a `jecs.Wildcard` term
- it lets you query any partially matched pairs

## [0.1.0-rc.5] - 2024-05-10

### Added

- Added Entity relationships for creating logical connections between entities
- Added `world:__iter method` which allows for iteration over the whole world to get every entity
- used for reconciling whole worlds such as via replication, saving/loading, etc
- Added `world:add(entity, component)` which adds a component to the entity
- it is an idempotent function, so calling it twice and in any order should be fine

### Fixed
- Fixed component overriding when in disorder
- Previously setting the components in different order results in it overriding component data because it incorrectly mapped the index of the column. So it took the index from the source archetype rather than the destination archetype

## [0.0.0-prototype.rc.3] - 2024-05-01

### Added

- Added observers
- Added an arm to query `query:without()` for chaining invariants.

### Changed
- Separates ranges for components and entity IDs.
- IDs created with `world:component()` will promote array lookups rather than map lookups in the `componentIndex` which is a significant boost

- No longer caches the column pointers directly and instead the column indices which stay persistent even when data is reallocated during swap-removals
- This was an issue with the iterator being invalidated when you move an entity to a different archetype.

### Fixedhttps://github.com/Ukendio/jecs/releases/tag/v0.0.0-prototype.rc.3

- Fixed a bug where changing an existing component would be slow because it was always appending changing the row of the entity record
- The fix dramatically improves times where it is basically down to just the speed of setting a field in a table

## [0.0.0-prototype.rc.2] - 2024-04-26

### Changed
- Optimized the creation of the query
- It will now finds the smallest archetype map to iterate over
- Optimized the query iterator
- It will now populates iterator with columns for faster indexing

- Renamed the insertion method from world:add to world:set to better reflect what it does.

## [0.0.0-prototype.rc.2] - 2024-04-23
- Initial release

[unreleased]: https://github.com/ukendio/jecs/compare/v0.0.0.0-prototype.rc.2...HEAD
[0.2.2]: https://github.com/ukendio/jecs/releases/tag/v0.2.2
[0.2.1]: https://github.com/ukendio/jecs/releases/tag/v0.2.1
[0.2.0]: https://github.com/ukendio/jecs/releases/tag/v0.2.0
[0.1.1]: https://github.com/ukendio/jecs/releases/tag/v0.1.1
[0.1.0]: https://github.com/ukendio/jecs/releases/tag/v0.1.0
[0.1.0-rc.6]: https://github.com/ukendio/jecs/releases/tag/v0.1.0-rc.6
[0.1.0-rc.5]: https://github.com/ukendio/jecs/releases/tag/v0.1.0-rc.5
[0.0.0-prototype-rc.3]: https://github.com/ukendio/jecs/releases/tag/v0.0.0-prototype.rc.3
[0.0.0-prototype.rc.2]: https://github.com/ukendio/jecs/releases/tag/v0.0.0-prototype.rc.2
[0.0.0-prototype-rc.1]: https://github.com/ukendio/jecs/releases/tag/v0.0.0-prototype.rc.1
23 changes: 14 additions & 9 deletions src/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ local function world_set(world: World, entity: i53, id: i53, data: unknown)
local idr = world.componentIndex[id]
local flags = idr.flags
local is_tag = bit32.band(flags, ECS_ID_IS_TAG) ~= 0
local has_on_set = bit32.band(idr.flags, ECS_ID_HAS_ON_SET) ~= 0
local has_on_set = bit32.band(flags, ECS_ID_HAS_ON_SET) ~= 0

if from == to then
if is_tag then
Expand All @@ -657,18 +657,23 @@ local function world_set(world: World, entity: i53, id: i53, data: unknown)
end
end

local tr = to.records[id]
local column = to.columns[tr.column]
local has_on_add = bit32.band(flags, ECS_ID_HAS_ON_ADD) ~= 0

if has_on_add then
invoke_hook(world, EcsOnAdd, id, entity)
end

if is_tag then
return
end
if not has_on_set then
column[record.row] = data
else
invoke_hook(world, EcsOnAdd, id, entity, data)
column[record.row] = data
invoke_hook(world, EcsOnSet, id, entity, data)

local tr = to.records[id]
local column = to.columns[tr.column]

column[record.row] = data

if has_on_set then
invoke_hook(world, EcsOnSet, id, entity, data)
end
end

Expand Down

0 comments on commit aa3e025

Please sign in to comment.