Code Monkey home page Code Monkey logo

Comments (13)

RajivKurian avatar RajivKurian commented on May 18, 2024

I was expecting something like this:

// Just allocate a buffer flyweight object without having it point to real memory.
private static final UnsafeBuffer BUFFER = new UnsafeBuffer(0);

// This tries to reserve lengthOfMyData bytes on the underlying logbuffer.
// If we managed to reserve the appropriate number of bytes, then change BUFFER's internals to the right pointer + length.
final boolean result = publication.offer(BUFFER, lengthOfMyData);
if (result) {
// Do optional bounds checking to make sure we don't go past lengthOfMyData.
BUFFER.putByte(byte1);
BUFFER.putByte(byte2);
.....
}

from aeron.

mjpt777 avatar mjpt777 commented on May 18, 2024

The statement is that the data is copy-free in the processing the log buffers. This is very unusual in a message transport.

However it is funny that you mention because this week I'm working on an additional API that will allow the claiming of a range in the log buffer so it can be written directly into. This will not really matter when going off box but should make a difference in the IPC case on box. The API will look something like:

final LogClaim logClaim = new LogClaim(); // Can be stored for future use

if (publication.claim(messageLength, logClaim))
{
    final MutableDirectBuffer buffer = logClaim.buffer();
    final int offset = logClaim.offset();

    // insert data directly into the buffer or wrap with a flyweight

    logClaim.publish();
}

from aeron.

RajivKurian avatar RajivKurian commented on May 18, 2024

Awesome!

from aeron.

mjpt777 avatar mjpt777 commented on May 18, 2024

I've pushed a commit that offers this functionality. 8ed70c4

Works like the following:

final BufferClaim bufferClaim = new BufferClaim(); // Can be stored for future use

if (publication.tryClaim(messageLength, bufferClaim))
{
    final MutableDirectBuffer buffer = bufferClaim .buffer();
    final int offset = bufferClaim .offset();

    // insert data directly into the buffer or wrap with a flyweight

    bufferClaim .commit();
}

from aeron.

mikeb01 avatar mikeb01 commented on May 18, 2024

What will happen to the state of the LogBuffer if client code calls tryClaim(), but never calls BufferClaim.commit(). Would it be useful to support passing in a lambda and ensure that the commit is always called?

public boolean tryClaim(final int length, final BufferClaim bufferClaim, Consumer<BufferClaim> callback)
{
    boolean claimed = tryClaim(length, bufferClaim);
    if (claimed)
    {
        try
        {
            callback.accept(bufferClaim);
        }
        finally
        {
            bufferClaim.commit();
        }
    }

    return claimed;
}

from aeron.

mjpt777 avatar mjpt777 commented on May 18, 2024

It is a good point and something people have to be carefully about. The issue with such a lambda is that is it likely to be capturing and thus will allocate and have an impact on latency.

from aeron.

mikeb01 avatar mikeb01 commented on May 18, 2024

You can work around the capturing, by allowing arguments to be floated through. The Disruptor's EventSink interface does this. See the description about using Java 8, it has a couple of notes around capturing Lambdas.

from aeron.

mjpt777 avatar mjpt777 commented on May 18, 2024

In your example bb will be captured and thus allocation will take place.

from aeron.

mikeb01 avatar mikeb01 commented on May 18, 2024

Not in the second example, where a method reference is used.

from aeron.

mjpt777 avatar mjpt777 commented on May 18, 2024

OK I see what you are doing. This can work for a single argument which might be sufficient for many cases. Could be an option consider.

from aeron.

mikeb01 avatar mikeb01 commented on May 18, 2024

I supported up to three and varargs (although varargs will potentially allocate). In the multi-producer case, failing to call commit/publish will leave the Disruptor (and similarly the LogBuffer - I think) in an invalid and unrepairable state. The most common case of this occurring is when the caller fails to do their exception handling correctly (commit must be in a finally block). With the Disruptor I recommend the publishEvent call as the default approach only only use next()/publish() if some advanced use case is required. I would suggest something similar for Aeron.

from aeron.

RichardWarburton avatar RichardWarburton commented on May 18, 2024

Varargs definitely allocates. Its also probably worse than a capturing lambda as well, because it needs to initialise the array of arguments.

I think really the only people who should be using this API are advanced users of the IPC case who are being bottlenecked by the additional copying cost of the current API. Do we want to caveat that in the javadoc for this class?

from aeron.

mjpt777 avatar mjpt777 commented on May 18, 2024

This is even more advanced that the Disruptor as you get direct access to the underlying buffer. Definitely an advanced feature only.

from aeron.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.