Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing Binary Heap's indexOf #31

Open
agentx-cgn opened this issue Aug 1, 2014 · 5 comments
Open

Replacing Binary Heap's indexOf #31

agentx-cgn opened this issue Aug 1, 2014 · 5 comments

Comments

@agentx-cgn
Copy link

Hi,

I've chosen this implementation for a RTS game, optimized it for Mozilla's SpiderMonkey and got very promising results with weighting thousands of nodes in a matter of one digit millisecond. So even full map scans run in an acceptable time frame. IonMonkey compiles all hot function into native code and spends now most time inside the internal indexOf of the BinaryHeap.

Has there been an attempt to replace the content array with a key/value store to avoid continuously searching in an tens of thousands entries array?

@bgrins
Copy link
Owner

bgrins commented Aug 2, 2014

Interesting, what changes did you make to optimize it for SpiderMonkey? I'd be curious to see how the optimized version does in other engines as well. Could you maybe make a jsPerf to compare the original vs. the optimized one?

Regarding changes to BinaryHeap, I haven't really looked into any optimizations so there may be some low hanging fruit there.

@agentx-cgn
Copy link
Author

Well, it's not exact science, but hardcoding diagonal and neighbors, replacing branching with ternary when possible seem to help. Initialization profits heavily from reusing the graph.

You'll find the current thing around here: https://github.com/agentx-cgn/Hannibal/blob/master/ai.js#L297 Sorry, won't have the time for a stand-alone at jsperf.com

@agentx-cgn
Copy link
Author

I gave the non indexOf a quick try, but didn't replace the array instead let the nodes keep track of their index.

// this.sinkDown(this.content.indexOf(node));
this.sinkDown(node.index);

It's now 5-6 times faster!

@bgrins
Copy link
Owner

bgrins commented Dec 8, 2014

Interesting, I'm not sure how each node is keeping track of its index, but if you had a PR or a code sample I'd be happy to take a look at this if it is this much faster. I'd be specifically interested in this index tracking change, not the smaller optimizations like ternaries instead of branching

@agentx-cgn
Copy link
Author

Had to adapt path structure, heap with index tracking starts now around here: https://github.com/agentx-cgn/Hannibal/blob/master/source/simulation/ai/hannibal/ai.js#L381

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants