bennidi / mbassador

Powerful event-bus optimized for high throughput in multi-threaded applications. Features: Sync and Async event publication, weak/strong references, event filtering, annotation driven
MIT License
958 stars 146 forks source link

Messages pooling #54

Closed Namek closed 9 years ago

Namek commented 11 years ago

Hi! Is it possible to pool created messages? I mean, that I can simply obtain new messages/events from my own pool, but I can't find a proper moment to free those objects (put them back into the pool).

bennidi commented 11 years ago

I am not sure if I understand correctly what you want to achieve. What is the purpose of the message pool? But to give you some more information:

Namek commented 11 years ago

For clarification: I'd like to create Event system based on MBassador. I expect many events recreated over time so there may happen often unneeded garbage collection (Android development, where GC is more badass than JRE on PC). I wanted to prevent from this by pooling Event objects (keeping the old ones for reuse after they are sent to every listener/handler).

OK, I looked into MessagePublication class right now and I can see such line:

state = State.Finished;

so there is the moment. Also there's isFinished() method but that's for polling (not pooling) state of message publication. What I want is to have event-based information rather than polling-based (but that should be optional).

The problem is that whole MBassador would need pooling to make a sense of all of that. MessagePublication.Factory is the thing to change for me. Also is publishment of PublicationEvent.

Anyway thanks for information and thank you for a great library. Due to pooling I'll fork the project and will try to propose something. Are you interested in pull request for pooling feature?

bennidi commented 10 years ago

I think this is still an interesting feature request. This could be implemented by adding callbacks to the MessagePublication that are invoked depending on the processing of the message publication (onStart/onCompletion/onError).

Namek commented 10 years ago

Since it needed a lot of work I threw away that idea back then. Message pooling is needed for performance reasons so I'm not sure about those callbacks you mention - they would had to be pooled too. My idea was to create such situation where no objects are created during publishing events.

bennidi commented 10 years ago

The callback would not necessarily be passed as an anonymous class with each publication. The same handler could be reused for each publication. If it receives the message publication as parameter and has a reference to the global object pool, then no new objects would be created. What kind of message throughput are you expecting by the way? I also saw in your discussion of https://github.com/junkdog/artemis-odb/issues/17 that reflection is an issue for you...

Namek commented 10 years ago

My expectation was to implement solution which doesn't create garbage. As I can remember my analyse it wasn't hard to code this idea into your library but it would make too many changes, which you wouldn't accept anyway.

I was making a shooting game where every shot could be counted for analytics (through event). I know this can be done in other ways but that's just an example for the expected throughput. These days I don't use Java and also I am not making that game, the project is going to be resumed but unfortunately not today.

Reflection sure is a breaking topic. It can't be used when application is compiled to HTML+JavaScript using GWT. Which is being done when application is implemented in libgdx for web. Or, actually you can use Reflection BUT you have to map some functions into GWT functions. In libgdx there is a bridge already made: https://github.com/libgdx/libgdx/tree/master/gdx/src/com/badlogic/gdx/utils/reflect but Artemis (which you have linked) doesn't belong to libgdx, it's just often used together. So yeah, it would be best to not use Reflection.

Other resources due to Reflection in libgdx: http://www.badlogicgames.com/wordpress/?p=2764 - before it was done https://github.com/libgdx/libgdx/wiki/Reflection

clintdoriot commented 6 years ago

@Namek did you ever find a good eventbus for libGDX that GWT compatible?

Namek commented 6 years ago

@chirhotec https://github.com/DaanVanYperen/artemis-odb-contrib/tree/master/contrib-eventbus/src/main/java/net/mostlyoriginal https://github.com/DaanVanYperen/artemis-odb-contrib/wiki/EventBasics