Skip to content

Just an idea: Api is very restricted and so it cause a lot performance problem #63

@vilinet

Description

@vilinet

I see this code base were left behind but i still want to raise this issue, maybe if someone has issue with performance can learn from it.

Would be great functions for be able to avoid creating a Hexagon and using instead CubeCoordinate would improve performance drasticly.

For example one of the mostly used function is the getNeighboursOf.

Firstly i am ok with the current one if you really need the list of hexagon neighbours.
I am writing about the cases when you do not need the hexagon(the most cases as they likely wont change position once they are created, if they move you just change the projection and so theri coordinate wont be affected)

So here are my results with using only CubeCoord:

getNeighboursOf on 100x100 grid(sure the grid size does not matter for neighbours, only maybe for looking up time in the storage)

Using hexagons:
Avg Duration: 2.991 seconds

Using CubeCoord only:
Avg Duration: 0.139 seconds

So roughly 20x improvent and so it could be improved even further beyond.
It also uses the user's collection to avoid object creation, it is nice and much more fitting for a game.

The code:
override fun getNeighborsOf(coordinate: CubeCoordinate, outputCollection: HashSet) {
for (i in NEIGHBORS.indices) {
val cube = getNeighborCoordinateByIndex(coordinate, i)
if(hexagonDataStorage.containsCoordinate(cube)){
outputCollection.add(cube)
}
}
}

Test:
long start = System.currentTimeMillis();

    for(int i=0; i<1000000; i++) {
        arr.clear();
        grid.getNeighborsOf(hexagons[18].getCubeCoordinate(), arr);
    }

    long end = System.currentTimeMillis();

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