Code Monkey home page Code Monkey logo

Comments (1)

barograf avatar barograf commented on May 28, 2024

I have an observation that is this topic related. I've been testing actions in Urho in order to implement logic for my actual project. What I need is an ability to write asynchronous code in order to simulate a lot of objects, let's say thousands.

Unfortunatelly the performance of actions for such purposes is not good enough. My test was set up with a simple scene with a lot of nodes, each of them having attached a component which started some tasks, continuously repeating them. The performance hit was tremendous when delays between tasks were small, e.g. tens or hundreds of miliseconds. I checked two approaches, with DelayTime which is an Urho action, and even with native Task.Delay.

I was trying to improve the overall solution by implementing synchronization context which executed tasks during update event, but it didn't help at all. The next approach was with a custom task scheduler which was processing tasks in a thread. Although there were no framerate drops anymore, tasks couldn't be processed "on time", making small delays lasting more than they should do.

The problem is that it is a very time consuming to call all of these queued callbacks if delays are small. There is still a possibility that I am doing something wrong or do not completely know how to handle it correctly. But, anyway, I figured out how it could be solved without tasks, but still with an asynchronous support for logic of my project.

I simply created a component which processes Func<float, bool> delegates. The delegate takes delta time as an input parameter and returns bool whether it should be treated as finished or not. Queued delegates are processed in an update event. If they are finished, they are removed from collection.

It is quite easy to implement behaviors like a Sequence, WhenAll or WhenAny. Sequence has an internal list of mentioned delegates and advances actually processed delegate, returning true only if all of them were finished one by one. WhenAll runs all of stored delegates on every update event, and returns true if all of them returned true (the ones finished earlier are removed from internal collection). Higher-level behaviors can be implemented with an IEnumerable<Func<float, bool>> because it is easier to implement complex logic with generators.

The fact is that with this approach I could simulate thousands of objects with no CPU freezes due to synchronization. The only drawback is a constant FPS drop, but it was not something big. The next con is that it is not as handy in use as tasks.

from urho.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.