MCPcopy Create free account
hub / github.com/SakuraEngine/SakuraEngine / defragment

Method defragment

modules/engine/runtime/src/ecs/storage.cpp:350–444  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

348}
349
350void sugoi_storage_t::defragment()
351{
352 using namespace sugoi;
353 if (scheduler)
354 {
355 SKR_ASSERT(scheduler->is_main_thread(this));
356 scheduler->sync_storage(this);
357 }
358 for (auto& pair : groups)
359 {
360 auto g = pair.second;
361 if (g->chunks.size() < 2)
362 continue;
363
364 // step 1 : calculate best layout for group
365 auto total = g->size;
366 auto arch = g->archetype;
367 uint32_t largeCount = 0;
368 uint32_t normalCount = 0;
369 uint32_t smallCount = 0;
370 while (total > arch->chunkCapacity[2])
371 {
372 total -= arch->chunkCapacity[2];
373 largeCount++;
374 }
375 while (total > arch->chunkCapacity[1])
376 {
377 total -= arch->chunkCapacity[1];
378 normalCount++;
379 }
380 if (normalCount == 0 && largeCount == 0) // it's small group
381 {
382 while (total > arch->chunkCapacity[0])
383 {
384 total -= arch->chunkCapacity[0];
385 smallCount++;
386 }
387 }
388 else // else prefer normal size chunk
389 normalCount++;
390
391 // step 2 : grab and sort existing chunk for reuse
392 skr::stl_vector<sugoi_chunk_t*> chunks = std::move(g->chunks);
393 std::sort(chunks.begin(), chunks.end(), [](sugoi_chunk_t* lhs, sugoi_chunk_t* rhs) {
394 return lhs->pt > rhs->pt || lhs->count > rhs->count;
395 });
396
397 // step 3 : reaverage data into new layout
398 skr::stl_vector<sugoi_chunk_t*> newChunks;
399 int o = 0;
400 int j = (int)(chunks.size() - 1);
401 auto fillChunk = [&](sugoi_chunk_t* chunk) {
402 while (chunk->get_capacity() != chunk->count)
403 {
404 if (j <= o) // no more chunk to reaverage
405 return;
406 auto source = chunks[j];
407 if (source == chunk)

Callers 1

sugoiS_defragementFunction · 0.80

Calls 15

move_viewFunction · 0.85
destruct_chunkFunction · 0.85
is_main_threadMethod · 0.80
sync_storageMethod · 0.80
move_entitiesMethod · 0.80
add_chunkMethod · 0.80
moveFunction · 0.50
sortFunction · 0.50
minFunction · 0.50
destroyFunction · 0.50
createFunction · 0.50
sizeMethod · 0.45

Tested by

no test coverage detected