Skip to content

Commit

Permalink
Added: Deleting queries from query cache
Browse files Browse the repository at this point in the history
Changed: TListItem::create -> handle
Changed: core::erase_fast -> swap_erase
  • Loading branch information
richardbiely committed Oct 6, 2024
1 parent 58940f9 commit 7497742
Show file tree
Hide file tree
Showing 17 changed files with 850 additions and 204 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,29 @@ q.add({p, QueryOpKind::All, QueryAccess::Write})
.add({pl, QueryOpKind::None, QueryAccess::None});
```
Building cache requires memory. Because of that, sometimes it comes handy having the ability to release this data. Calling ```myQuery.reset()``` will remove any data allocated by the query. The next time the query is used to fetch results the cache is rebuilt.
```cpp
q.reset();
```

If this is a cached query, even after resetting it it still remains in the query cache. To remove it from there all queries with the matching signature will need to be destroyed first:

```cpp
ecs::Query q1 = w.query();
ecs::Query q2 = w.query();
q1.add<Position>();
q2.add<Position>();

(void)q1.count(); // do some operation that compiles the query and inserts it into the query cache
(void)q2.count(); // do some operation that compiles the query and inserts it into the query cache

q1 = w.query(); // First reference to cached query is destroyed.
q2 = w.query(); // Last reference to cache query is destroyed. The cache is cleared of queries with the given signature
```

Technically, any query could be reset by default initializing it, e.g. ```myQuery = {}```. This, however, puts the query into an invalid state. Only queries created via World::query have a valid state.

### Query string
Another way to define queries is using the string notation. This allows you to define the entire query or its parts using a string composed of simple expressions. Any spaces in between modifiers and expressions are trimmed.

Expand Down
1 change: 1 addition & 0 deletions gaia.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,6 @@
"upci",
"upos"
],
"makefile.configureOnOpen": false,
}
}
8 changes: 6 additions & 2 deletions include/gaia/cnt/ilist.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ namespace gaia {
return {(pointer)m_items.data() + size()};
}

void reserve(size_type cap) {
m_items.reserve(cap);
}

//! Allocates a new item in the list
//! \return Handle to the new item
GAIA_NODISCARD TItemHandle alloc(void* ctx) {
Expand All @@ -115,7 +119,7 @@ namespace gaia {
GAIA_GCC_WARNING_DISABLE("-Wmissing-field-initializers");
GAIA_CLANG_WARNING_DISABLE("-Wmissing-field-initializers");
m_items.push_back(TListItem::create(itemCnt, 0U, ctx));
return TListItem::create(m_items.back());
return TListItem::handle(m_items.back());
GAIA_GCC_WARNING_POP()
GAIA_CLANG_WARNING_POP()
}
Expand All @@ -128,7 +132,7 @@ namespace gaia {
auto& j = m_items[m_nextFreeIdx];
m_nextFreeIdx = j.idx;
j = TListItem::create(index, j.gen, ctx);
return TListItem::create(j);
return TListItem::handle(j);
}

//! Allocates a new item in the list
Expand Down
6 changes: 2 additions & 4 deletions include/gaia/ecs/archetype.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ namespace gaia {
namespace detail {
GAIA_NODISCARD inline bool cmp_comps(EntitySpan comps, EntitySpan compsOther) {
// Size has to match
GAIA_FOR(EntityKind::EK_Count) {
if (comps.size() != compsOther.size())
if (comps.size() != compsOther.size())
return false;
}

// Elements have to match
GAIA_EACH(comps) {
Expand Down Expand Up @@ -467,7 +465,7 @@ namespace gaia {
// with the last one in the array. Therefore, we first update the last item's
// index with the current chunk's index and then do the swapping.
m_chunks.back()->set_idx(chunkIndex);
core::erase_fast(m_chunks, chunkIndex);
core::swap_erase(m_chunks, chunkIndex);

// Delete the chunk now. Otherwise, if the chunk happened to be the last
// one we would end up overriding released memory.
Expand Down
8 changes: 4 additions & 4 deletions include/gaia/ecs/chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -1139,23 +1139,23 @@ namespace gaia {
return m_header.lifespanCountdown > 0;
}

//! Marks the chunk as dead
//! Marks the chunk as dead (ready to delete)
void die() {
m_header.dead = 1;
}

//! Checks is this chunk is dying
//! Checks is this chunk is dead (ready to delete)
GAIA_NODISCARD bool dead() const {
return m_header.dead == 1;
}

//! Starts the process of dying
//! Starts the process of dying (not yet ready to delete, can be revived)
void start_dying() {
GAIA_ASSERT(!dead());
m_header.lifespanCountdown = ChunkHeader::MAX_CHUNK_LIFESPAN;
}

//! Makes the chunk alive again
//! Makes a dying chunk alive again
void revive() {
GAIA_ASSERT(!dead());
m_header.lifespanCountdown = 0;
Expand Down
8 changes: 4 additions & 4 deletions include/gaia/ecs/chunk_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ namespace gaia {
if (pPage->full()) {
// Remove the page from the open list and update the swapped page's pointer
container.pagesFree.back()->m_idx = 0;
core::erase_fast(container.pagesFree, 0);
core::swap_erase(container.pagesFree, 0);

// Move our page to the full list
pPage->m_idx = (uint32_t)container.pagesFull.size();
Expand Down Expand Up @@ -301,7 +301,7 @@ namespace gaia {
if (wasFull) {
// Our page is no longer full. Remove it from the full list and update the swapped page's pointer
container.pagesFull.back()->m_idx = pPage->m_idx;
core::erase_fast(container.pagesFull, pPage->m_idx);
core::swap_erase(container.pagesFull, pPage->m_idx);

// Move our page to the open list
pPage->m_idx = (uint32_t)container.pagesFree.size();
Expand All @@ -314,7 +314,7 @@ namespace gaia {
if (pPage->empty()) {
GAIA_ASSERT(!container.pagesFree.empty());
container.pagesFree.back()->m_idx = pPage->m_idx;
core::erase_fast(container.pagesFree, pPage->m_idx);
core::swap_erase(container.pagesFree, pPage->m_idx);
}

try_delete_this();
Expand Down Expand Up @@ -345,7 +345,7 @@ namespace gaia {

GAIA_ASSERT(pPage->m_idx == i);
container.pagesFree.back()->m_idx = i;
core::erase_fast(container.pagesFree, i);
core::swap_erase(container.pagesFree, i);
free_page(pPage);
}
};
Expand Down
4 changes: 2 additions & 2 deletions include/gaia/ecs/entity_container.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace gaia {

EntityContainer() = default;

static EntityContainer create(uint32_t index, uint32_t generation, void* pCtx) {
GAIA_NODISCARD static EntityContainer create(uint32_t index, uint32_t generation, void* pCtx) {
auto* ctx = (EntityContainerCtx*)pCtx;

EntityContainer ec{};
Expand All @@ -85,7 +85,7 @@ namespace gaia {
return ec;
}

static Entity create(const EntityContainer& ec) {
GAIA_NODISCARD static Entity handle(const EntityContainer& ec) {
return Entity(ec.idx, ec.gen, (bool)ec.ent, (bool)ec.pair, (EntityKind)ec.kind);
}

Expand Down
Loading

0 comments on commit 7497742

Please sign in to comment.