- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 81
 
lazy_static rhai Engine #112
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
          
     Closed
      
      
    
  
     Closed
                    Changes from 3 commits
      Commits
    
    
            Show all changes
          
          
            4 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original version of enforce is lock-free, will this change makes
enforcecan't handle concurrent request because every request have to wait for the write lock.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if it matters, I can't get it to panic even using
RwLock::try_writein either an async spawned context or on a std thread. The only way to pass theEnforcerto a thread or anasync_std::spawnis to wrap it in some type of sync control. I'm pushing another commit mostly to show the test I wrote for this. If you can think of any other ways to pass this into concurrent execution it'd be great to add them to the test.This also reminds me I was wondering about using
async_std::sync::RwLock/Mutexinstead of std's RwLock/Mutex. There is a discussion about this sort of hereUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we do have only one enforcer, it's ok to use lazy_static, but I would say if the users created multiple enforcers. It will cause problems because basically they will share the same
Engineand the more enforcers we have, the worser result we get. => This is also why I removed lazy_static event emitter from the code@xcaptain @DevinR528 I think inline engine, scope will improve bench about 15%, but yes I didn't succeed to implement it yesterday due to lifetime issue.
May try again in the future
@DevinR528 You are right we shouldn't use
std::sync::RwLockto wrap a type which implemented many async functions. Because.awaitrequiresSendandstd::sync::RwLockGuardis not send. That causes also difficutiles when I try to implement a clonableSyncedEnforcer, which is basicallyArc<RwLock<Enforcer/CachedEnforcer>>. This maybe out of topicThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of just one
Enginecould you have a lazy_staticArc<Mutex<Vec<Engine>>>and every timeEnforcer::newis called you push a newEngineinto the vec and assign an index for thatEnforcerinstance? I'm going to make a somewhat educated guess that the lifetime issues may be insurmountable 😢, basically the lifetime of the &str's passed toScope::push_constantbecome the lifetime of the&mut selfborrow forEnforcer::enforceand there doesn't seem to be a way to say a lifetime that is smaller than the lifetime of theEnforcerstruct which is what you need.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DevinR528
Arc<Mutex<Vec<Engine>>>still require write access, it means when one enforcer is enforcing, there won't be any other enforcer can acquire a engine.I believe
rhai::Engine's lifetime is to for the registered functions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh right the vec won't work at all. I had one more idea creating a lockless AtomicPtr struct that has a flag for when the
Engineis in use and either starts we a fewEngines or adds to a vec as needed.