isActive() call extremely expensive.



  • Structure.isActive() is extremely expensive due to the inefficient implementation, so one has stopped using it. The effect is extremely pronounced in a room with a lot of rampart structures present. One believes, that the same algorithm is used in the server side for determining the active structures and refactoring the code responsible for determining the active structures vs the controller level, would give a significant boost to the servers' performance.

    ta



  • @adeptofnone Move to Techincal Issues & Bugs kthx bye





  • Have you tried caching it? Here is my code on how I cached it.

    //cache the isActive() function.. This requires all structures to have memory
    //see https://stackoverflow.com/questions/30147800/extend-source-prototype-to-have-a-memory-object for an example
    
    Object.defineProperty(OwnedStructure.prototype, 'IsActive', { 
    	get: function() {
    	    if (!this.my) return false //out ours
    	    if (!this.memory.active) this.memory.active = this.isActive() //initialize
    	    return this.memory.active
    	},
    	configurable: true,
    });
    
    //determine if we need to refresh the isActive() cache on an RCL downgrade
    Room.prototype.ActiveBuildingCache = function() {
        //level didn't change
        if (this.memory.currentlevel === this.controller.level) return true
        
        //We increased the RCL. Therefore, everything should be the same
        if (this.memory.currentlevel < this.controller.level) {
            this.memory.currentlevel = this.controller.level //set to new RCL
        } else {
            //decreased level.. Need to recalc what is active and what isn't
            this.memory.currentlevel = this.controller.level //set the new RCL
            const mystructures = this.find(FIND_MY_STRUCTURES) //get all structures
            
            for (let s of mystructures) delete s.memory.active //delete the active flag
        }
        
        return true
    }
    


  • @crazydubc the engine does its own active check in a call when you use the structure



  • The engine code actually very rarely to never calls the structure isActive method, it's hard coded to check against controller and does a very, very expensive partition operation every time it's callled. It's also however not easy to fix, as the ideal would be to calculate and cache this information whenever RCL changed or a structure was placed/destroyed.



  • This was meant for end users who are utilizing the isActive() call. Not server side.