Giter Club home page Giter Club logo

Comments (6)

gdamore avatar gdamore commented on July 4, 2024 1

So once you send, yes, the function assumes ownership of the message, and will return it back to the pool when done.

It might be possible to build a system where instead of nng_msg_free returning the message to the heap directly, it calls a user-supplied callback which could then reuse it. This would reduce pressure on the heap, although you probably would not achieve precisely what you want with respect to a single memory region.

Other solutions are also possible under the hood, but they would involve more data copies, which is something that I think you're trying to avoid.

from nng.

gdamore avatar gdamore commented on July 4, 2024

Sorry for being so late to get back to you.

The best way to do explicitly manage memory is to use nng_msg_alloc() with large messages, and then use those. This will avoid copying data.

For those you can use nng_ctx_send, nng_ctx_recv, or nng_send_aio(), or nng_recv_aio().

What happens is that these utilize the message memory directly. The older NNG_FLAG_ALLOC is a convenience and intended mostly for compatibility with legacy nanomsg. It isn't as efficient as allocating the messages directly.

To allocate the message directly, you can allocate it for as large as you think it will grow. Then you can either truncate it and grow it, or just access the body directly.

You should ignore nng_aio_set_iov... that API is indeed for the underlying byte stream APIs, and not intended for the SP message oriented protocols.

from nng.

gdamore avatar gdamore commented on July 4, 2024

Note that if you use nng_msg_append() or something like that, then it definitely is not zero copy. The trick to zero copy is to use the message memory directly.

Also, btw, it isn't truly zero copy. It's less copies. True zero copy requires rather sophisticated memory management and hardware support for direct DMA, etc. Generally what we have done here is usually good enough, but for extreme high performance you'll probably want to investigate RDMA or something like that.

from nng.

gdamore avatar gdamore commented on July 4, 2024

And 100K requests per second is not extreme high performance, unless they are very large.

from nng.

fsorrent avatar fsorrent commented on July 4, 2024

Thanks for your reply.

Using nng_msg_alloc() and building the message in place may be a very reasonable solution. On the other hand, I was considering doing one allocation per context, rather than per message. I was hoping to get around The function assumes ownership of the message.

If I could pre-allocate the space for responses too, that would solve the alignment problem.

If it is not possible, would you like to sketch what the implementation would entail? In case anyone finds time to work on it.

from nng.

gdamore avatar gdamore commented on July 4, 2024

Note that with respect to responses, the function normally allocates messages with a certain amount of "head room". This ensures that you can inject or change headers if you need to. Again, this isn't exactly what you're asking for, but it may help.

from nng.

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.