darkforest-eth/plugins

getPlanetsInRange() - faster

modukon opened this issue · 1 comments

I wrote my own very simple function to get planets in range and compared it to the default one,
The custom simple one is 3-4 faster, but maybe its not as good somehow? maybe i did something wrong

function getRange(planet, percentEnergySending = 100) {
    if (percentEnergySending === 0) return 0;
    return Math.max(Math.log2(percentEnergySending / 5), 0) * planet.range;
}
function getDistance(p1, p2) {
    let x = p2.location.coords.x - p1.location.coords.x;
    let y = p2.location.coords.y - p1.location.coords.y;
    return Math.sqrt(x*x + y*y);
}
function perfTest() {
    var planetId = "*put your planet id here*";
    var planet = df.getPlanetWithId(planetId);

    let ds = new Date();
    let l = df.getPlanetsInRange(planetId);
    let de = new Date();
    console.log("planetCount: "+l.length+" time: "+(de.getTime()-ds.getTime()))

    let xs = new Date();
    let allPlanets = df.getAllPlanets();
    let arr = [];
    let range = getRange(planet);
    for (var p of allPlanets) {
        if (!p.location) continue;
        if (!p.location.coords) continue;
        if (getDistance(planet, p) < range)
            arr.push(p);
    }
    let xe = new Date();
    console.log("planetCount: "+arr.length+" time: "+(xe.getTime()-xs.getTime()))
}
perfTest();

output:

planetCount: 16804 time: 54
planetCount: 16804 time: 17

That seems possible

Im sure you know, but you can see the source for it here
https://github.com/darkforest-eth/client/blob/7aef22d9d48bc5d0a2b0172ce7ffb056a591d5c0/src/Backend/GameLogic/GameManager.ts#L2950

Please PR against that repo to catch the core devs attention