Coder Social home page Coder Social logo

Engine ThreadSafety about jace HOT 6 OPEN

pieterderycke avatar pieterderycke commented on July 28, 2024
Engine ThreadSafety

from jace.

Comments (6)

pieterderycke avatar pieterderycke commented on July 28, 2024

The be honest. I have never tested it to be the case. The main potential issue I see is with the caching of generated delegates. But I think it can definitely be made thread safe without a performance impact.

For which reason do you need to have it thread-safe?

from jace.

BoasE avatar BoasE commented on July 28, 2024

just to avoid unnecessary allocations. e.g. i could have a static engine .

from jace.

aviita avatar aviita commented on July 28, 2024

Thread safety would also make the cache usage more efficient. In my current implementation I am not sharing the CalculationEngine at all between objects. I could definitely do something about this already, but the thread safety would make sense from caching point of view too. Now the MemoryCache and the ConcurrentDictionary it uses are created per CalculationEngine instance.

I have a usa case which relies quite heavily on Jace formula calculations and I can see that lots of time is spent on the DynamicCompiler.BuildFromula().
image

Here converting the cache to be a singleton/thread safe might also do the trick. It could be even configured in the JaceOptions if the cache is to be handled as singleton or not. Another option would be to use .Net MemoryCache directly and rely on it's cleanup and thread safety mechanisms. Built formulas should be thread safe already as they are just pure functions.

In the my case of which you can see the performance above I have only 32 different formulas, but 1168 instances of the engine.

from jace.

aviita avatar aviita commented on July 28, 2024

I could improve my code by creating the Engine per user, so I would be ensured to have no concurrency issues, but even that approach leaves two problems.

  1. There is a performance penalty for new user sessions.
  2. There is memory overhead to even in having the cache per user.

I my case I think I could even have all the formulas cached as I have maximum of ~1900 formulas, but I think the real number is even much lower as that 32 different formulas for one use case already has almost maximum complexity. I have roughly 60 other cases for the formulas and they are much simpler mostly.

from jace.

aviita avatar aviita commented on July 28, 2024

Ah, and I did notice I did invalid lookup to the .Net documentation. .Net Standard does not have the MemoryCache, so that must be why ConcurrentDictionary is used. So looking now more into the approach of making it static.

from jace.

aviita avatar aviita commented on July 28, 2024

As for the thread safety in general. It would seem to me that ConstantRegistry for one would need work to be thread safe. And FunctionRegistry could potentially be even more problematic if shared. But if ommitting use of those two features it could be possible to make the engine a singleton. This is somewhat risky though. Maybe in that case there should be separate implementation of the CalculationEngine, something like ConcurrentCalculationEngine that would ommit the functionality that is not supported.

from jace.

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.