Skip to content

Query performance optimization #3

Open
@MioQuispe

Description

@MioQuispe

At the moment the speed of querying depends on the amount of entities. As this should be one of the most common operations performed I think it should be optimized. I did a little experiment where I'd store all entities with the same mask in an array which could be accessed by their mask through a hashmap.

var hashMap = {}

hashMap[0] = [] //All entities with mask 0 stored here
hashMap[5] = [] //All entities with mask 5 stored here

...etc

The entities then move around based on their mask. This lets us get a constant query time no matter the amount of entities by simply testing the query mask against each index in the hashmap. Initial tests gave ~5mil queries per second no matter the amount of entities (with hardly any optimizations applied).

var result = []

var entityMasks = Object.keys(hashMap)
for (var index = 0; index < entityMasks.length; index++) {
  var entityMask = entityMasks[index]
  if ((entityMask & mask) === mask) {
    Array.prototype.push.apply(result, this._entityCache[entityMask])
  }
}

The problem is that you'd have to keep track of each entitys index in its hashMap[entityMask] array to remove it from there when its mask changes or it is destroyed. You'd also have to update the indices of the other entities in the mask array which would in turn make any operation that changes the mask of entities slower (the speed would now be determined by the amount of entities with the same mask and the index of the entity that was removed).

Thoughts? Or perhaps other ideas on how to do it? If something was unclear I'll explain it better.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions