zdandoh / ecs

Golang ECS library using go generate
MIT License
5 stars 0 forks source link

Info on paging #8

Closed delaneyj closed 8 months ago

delaneyj commented 8 months ago

Entities are stored in [][]Entity and use a double lookup from the entity using bitwise operators. Since this is in memory I wanted to know more about the reasoning behind paging versus just a single level of slices.

zdandoh commented 8 months ago

The main reason I decided to use pages is because it allows for cheap and straightforward allocation of new entity blocks at runtime. If you use a single backing array and dynamically resize it then you'd invalidate the existing pointers into that array upon reallocation. So something like this:

ecs.Select(func(e ecs.Entity, pos *components.Pos {
    for i := 0; i < 100; i++ {
        ecs.NewEntity() // This triggers a reallocation of the underlying entity and component arrays
    }
    // How do you ensure the validity of the pos pointer here?
}))

Seems like a problem. Additionally, it seems like it would be cheaper to allocate more memory than to realloc and copy existing memory that may or may not be paged in. You could preallocate a large amount of memory and leave it up to virtual memory to handle the paging details, but I'm not sure how well that would work in practice.

delaneyj commented 8 months ago

:+1: