khora_lanes/ecs_lane/compaction_lane.rs
1// Copyright 2025 eraflo
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! A lane for compacting component pages by cleaning up orphaned data.
16
17use khora_data::ecs::{PageIndex, SemanticDomain, WorldMaintenance};
18
19/// A work plan for a single frame's garbage collection pass.
20#[derive(Debug, Default)]
21pub struct GcWorkPlan {
22 /// The budget to determine how many items we can clean this frame.
23 pub budget: usize,
24 /// The list of orphaned data locations to clean up.
25 pub items_to_clean: Vec<(PageIndex, SemanticDomain)>,
26}
27
28/// The lane responsible for executing the physical cleanup of orphaned component data.
29#[derive(Debug, Default)]
30pub struct CompactionLane;
31
32impl CompactionLane {
33 /// Creates a new `CompactionLane`.
34 pub fn new() -> Self {
35 Self
36 }
37
38 /// Executes the compaction work defined in the `GcWorkPlan`.
39 ///
40 /// It requires a trait object with `WorldMaintenance` capabilities to perform
41 /// its low-level cleanup operations.
42 pub fn run(&self, world: &mut dyn WorldMaintenance, work_plan: &GcWorkPlan) {
43 for (location, domain) in work_plan.items_to_clean.iter().take(work_plan.budget) {
44 world.cleanup_orphan_at(*location, *domain);
45 }
46 }
47}