Skip to content
Browse files

Rewrote future and async sends to use an Actor pool instead of 1 acto…

…r per object to improve performance when dealing with many actors at once.
  • Loading branch information...
1 parent 7aacf26 commit d77bb6f3c64db40c1169bb90e2d8a77d1e41eaba @bakkdoor committed Nov 7, 2011
Showing with 48 additions and 6 deletions.
  1. +41 −0 lib/actor.fy
  2. +1 −0 lib/boot.fy
  3. +3 −3 lib/future.fy
  4. +3 −3 lib/object.fy
View
41 lib/actor.fy
@@ -0,0 +1,41 @@
+class Actor {
+ @@actor_pool = nil
+ @@pool_size = 10
+
+ def Actor pool_size: size {
+ @@pool_size = size
+ }
+
+ def Actor spawn_actors {
+ @@actor_pool = (0..@@pool_size) map: {
+ Actor spawn: {
+ loop: {
+ sender = nil
+ try {
+ type, msg, sender = Actor receive
+ receiver, msg, params = msg
+ match type {
+ case 'async ->
+ receiver receive_message: msg with_params: params
+ case 'future ->
+ val = receiver receive_message: msg with_params: params
+ sender completed: val
+ }
+ } catch Exception => e {
+ { sender failed: e } if: sender
+ }
+ }
+ }
+ }
+ }
+
+ def Actor pool {
+ @@actor_pool
+ }
+
+ def Actor[receiver] {
+ @@actor_pool if_nil: { Actor spawn_actors }
+ id = receiver object_id % (@@actor_pool size)
+ @@actor_pool[id]
+ }
+}
View
1 lib/boot.fy
@@ -50,3 +50,4 @@ require: "vars"
require: "documentation"
require: "package.fy"
+require: "actor"
View
6 lib/future.fy
@@ -1,11 +1,11 @@
class FutureSend {
read_slots: [ 'receiver, 'message, 'params ]
- def initialize: @actor receiver: @receiver message: @message with_params: @params ([]) {
+ def initialize: @receiver message: @message with_params: @params ([]) {
@completed_mutex = Mutex new
@condvar = ConditionVariable new
@completed = false
@failed = false
- @actor ! ('future, (@message, @params), self)
+ Actor[@receiver] ! ('future, (@receiver, @message, @params), self)
}
def failed: @fail_reason {
@@ -146,4 +146,4 @@ class FutureCollection {
def await_all {
@futures each: 'value
}
-}
+}
View
6 lib/object.fy
@@ -510,7 +510,7 @@ class Object {
Creates a @FutureSend@ object (a Future / Promise) that will hold the value of sending @message to @self.
"""
- FutureSend new: __actor__ receiver: self message: message with_params: params
+ FutureSend new: self message: message with_params: params
}
def send_async: message with_params: params ([]) {
@@ -522,7 +522,7 @@ class Object {
Sends @message with @params to @self asynchronously and immediately returns @nil.
"""
- __actor__ ! ('async, (message, params), nil)
+ Actor[self] ! ('async, (self, message, params), nil)
nil
}
@@ -666,4 +666,4 @@ class Object {
let: '*stdout* be: f in: block
}
}
-}
+}

0 comments on commit d77bb6f

Please sign in to comment.
Something went wrong with that request. Please try again.