Provent GC by reusing the event in ringbuffer.

95 views
Skip to first unread message

chenchua...@163.com

unread,
Feb 7, 2012, 10:07:52 PM2/7/12
to Disruptor
As I know, the ringbuffer resuing the event to provent GC and the
improve performance.
But when we call method publishEvent(final EventTranslator<E>
translator),we have to create a instance of EventTranslator,and after
calling the publishEvent this eventTranslator will drop and clean by
GC.

Doesn't that have the same performance influence between clean a
instance of EventTranslator and clean a instance of event in
ringBuffer?

Olivier

unread,
Feb 8, 2012, 2:46:15 AM2/8/12
to lmax-di...@googlegroups.com
The translator should be created at startup and the publisher should reuse the same instance.

Olivier

Igor Petrouk

unread,
Feb 8, 2012, 2:59:51 AM2/8/12
to lmax-di...@googlegroups.com
Usually what I do is something like this.

public class Publisher implements EventTranslator<RingElement>{
    Data internalData;

    public void send(Data data){
        internalData = data;
        disruptor.publishEvent(this); // <-- !!!!
        internalData = null;
    }

    @Override
    public RingElement translateTo(RingElement event, long sequence) {
        event.data = internalData;
        return event;
    }
}

chenchua...@163.com

unread,
Feb 9, 2012, 2:47:40 AM2/9/12
to Disruptor
Thanks for your replay.
But when we have multiple threads of producer,I seems that we can't
use the same instance directly.

Actually when we calling a mothod,we may create lots of objects and
these objects will be clean after finishing the method.
So,Is that really important to reuse the event instances in
ringbuffer?


On Feb 8, 3:46 pm, Olivier <m...@odeheurles.com> wrote:
> The translator should be created at startup and the publisher should reuse the same instance.
>
> Olivier
>
> On 8 févr. 2012, at 03:07, "chenchuanfeng...@163.com" <chenchuanfeng...@163.com> wrote:
>
>
>

Michael Barker

unread,
Feb 9, 2012, 11:47:53 AM2/9/12
to lmax-di...@googlegroups.com
> Thanks for your replay.
> But when we have multiple threads of producer,I seems that we can't
> use the same instance directly.

You could use a thread local or model your producers such that you
have one per thread and each owns an instance of the translator.

> Actually when we calling a mothod,we may create lots of objects and
> these objects will be clean after finishing the method.
> So,Is that really important to reuse the event instances in
> ringbuffer?

The Disruptor does not proscribe that you much build zero garbage
systems and always reuse the data inside of the event. The point of
the Disruptor is that it allows you to choose and makes is possible to
build systems that are very memory efficient. Existing messaging
libraries push you toward immutable objects as the main way to move
data between threads and the Disruptor supports that too, if that is
the most appropriate solution.

Mike

Reply all
Reply to author
Forward
0 new messages