Jump to content

April 2017 »

2324252627 28 29

Recent Comments

- - - - -

Reinventing the Agent Component Bus

Vaerydian Asynchronous Programming
4: Adsense

Recently I've been teaching myself a lot about Node.js out of curiosity and out of interest in learning more about new areas of web development. I've come to really like how it works, its simplicity, and the huge open source community that has popped up around it. Its event-based async design is quite an effective approach to concurrent programming, so i really took a shine to it. Then I looked at my own Agent Component Bus (ACB), then at Node, then at the ACB, then at Node... yea... ACB, you're going to change...

My earlier work with my ACB was no slouch. It easily handled hundreds of separate agents doing several different processes asynchronous of the main game thread, but, it had a very rigid design and implementation path. So, no matter what process you were handling, you always had a component retrieve cycle, a process cycle, and a commit cycle. This is great if your process is doing component manipulation or game state assessments and taking action based on that assessment, but not so much if you're doing just a read, or just a write, or neither. Also, agents were very fat. They had to have extra logic to keep track of state, which components were active in their process pipeline, etc. Basically, the ACB was a pain to program for, effective yes, but a pain.

So I stripped it down to its guts. Assessed what i needed to change it into an evented-like design. I found that most of my Entity Component System extensions could be simplified, along with their associated ACB components. That the Bus itself wasnt needed and that all work could be handled by the ResourcePool and TaskWorker. Finally, the Agent only needed to be a data/delegate reference class, stripping the need for state information. When i tested it out, its was fast! Now, when multi-threaded, the new concept code processed roughly within 5% of the work packages original ACB concept would process, but it did this with a 6x cycle disadvantage; so i knew the new internal architecture was much better. Methods, overrides, interface calls, are about 6x faster than delegate calls in c#. However, there is much less overhead in the new architecture, so it makes back that speed in efficiency. Single-Threaded, the new architecture is faster, however multi-threaded, its within 5%.

"So, NetGnome, if its not faster, why would i use it?"
It is faster in-practice however. Those up above were using the core concept code. In conceptual implementation they basically performed at the same speed, with the original ACB edging out in performance. However, implementation into the game engine is a MUCH different matter. The new code hardly has any overhead compared to the old code and is extremely flexible.

Old code you had to have BusComponent classes that managed the data you wanted and what you did with them, Fat Agents to watch states and notifications, and a Bus AND TaskWorker to issue tasks and work them with a ResourcePool keeping track of everything plus the game engine integration systems.

New code only requires a light Agent, TaskWorker to work tasks, callbacks, and events, ResourcePool to handle data and queuing of tasks, events, callbacks, etc. and much more simplified engine integration systems.

Most interaction is now done in the following forms:
public static void issueTask(Agent agent, TaskHandler task, CallBackHandler callBack, params object[] parameters)

public static void emit(string eventName, Agent agent, params object[] parameters)
In this way, you can issue tasks like so:
ResourcePool.issueTask(agent, doSomething, delegate(TaskObject to){
  //handle callback
}, 1, DateTime.Now, "foo");

public TaskObject doSomething(TaskObject to){
  //do stuff and return a TO for the callback
As well as handle and issue events like so:
ResourcePool.on("CUSTOM_EVENT_NAME", agent, delegate(EventObject eo){
  //handle event here

ResourcePool.emit("CUSTOM_EVENT_NAME", agent, 1, DateTime.Now, "foo");
What makes it particularly useful, is that you can use anonymous delegates or named ones, whatever fits your desire, the new ACB doesn't care.

But, with anything concurrent, there is always the chance that you'll have to issue something synchronous, like working with List<T> structures in specific ways. That is why i also built in a Synchronous Operation and supporting system. It guarantees that the code passed to it will be performed in-sync on the game engine when the SyncOperationSystem is called to process. It looks kinda like this:
public static void issueSyncOperation(string syncEvent, Agent agent, Operation operation, params object[] parameters)
When it is finished, it will emit an event called syncEvent if you want to handle something after it was called.

"So, NetGnome, what can i use this for?"
Anything you want. Hell, I actually built a crappy http server out of this and it was able to serve very simple static webpages at about 12k requests per second at a concurrency of 4000 with 8 threads (i haven't upped my open file limit higher, so i don't know if it could take-on the C10K challenge Posted Image ). As long as you conform to the delegate structure below, you can run anything you want in it.
public delegate void CallBackHandler(TaskObject taskObject);
public delegate TaskObject TaskHandler(TaskObject taskObject);
public delegate void EventCallBackHandler(EventObject eventObject);

public struct TaskObject{
	public TaskHandler Task;
	public CallBackHandler CallBack;
        public Agent Agent;
	public object[] Parameters;
public struct EventObject{
	public string Name;
        public Agent Agent;
	public object[] Parameters;
Some other interesting notes, I've set up the task workers to run both by count and by timing. So if you want it only to process 5 tasks, 30 events, and 2000 callbacks per cycle, you can do that; or if you would rather it only work as long as 3000 ticks before the thread sleeps for 0 - n ticks, you can do that too.

Overall, the changes have worked great and performance is great and programming for it is about 100x easier now Posted Image

If you want to check it out, head over to the repo on GitHub (note: you'll also need my ECSFramework to make it work).

Anyway, that's all for now!

Note: GameDev.net moderates comments.