Giter Site home page Giter Site logo

Zero-copy encoding and decoding about mqttrs HOT 4 OPEN

00imvj00 avatar 00imvj00 commented on August 18, 2024
Zero-copy encoding and decoding

from mqttrs.

Comments (4)

vincentdephily avatar vincentdephily commented on August 18, 2024

@00imvj00 Please take a look at vincentdephily@71627f9

I've spent some time trying to convert to impl Trait but have hit a few walls. We've got a few options:

  • Stop trying to be flexible, and embrace BytesMut everywhere.
  • Wait for (or help) the Bytes crate to switch the specified bugs.
  • Use the .bytes() workaround and document that only continuous-memory backends may be used (or if upstream creates a Trait for that, use it).
  • Use a different crate altogether, or reinvent the wheel.

I'm currently leaning towards option 2, but would like some opinions. I'd also be ok with throwing that commit away and go with option 1.

Note that one thing we need to do to get toward zero-copy is convert some Packet members (Connect.password, Publish.payload...) from Vec<u8> to Bytes (or use &[u8] and deal with the shared lifetime between Packet and its source/destination buffer). If that's where we're going, maybe it's silly to try to support other Buf backends.

from mqttrs.

00imvj00 avatar 00imvj00 commented on August 18, 2024

For Zero-copy, instead of this, how about we keep references of offset?

For example: If the overall packet length is 64 bytes, we store these bytes as byte-array internally and then for the password, instead of a string, we just store something like, from index to index, and build method get_password() -> &str, where we will return the reference.

from mqttrs.

vincentdephily avatar vincentdephily commented on August 18, 2024

A Bytes struct obtained from Bytes::slice*() or Bytes::split*() is pretty-much just a reference inside a &[u8], with some refcounting to figure out when the actual data can be dropped. I'm just not sure how it handles references to old data. Is it viable to keep using the same BytesMut to receive data from the network and then decode(), yielding a stream of Packet ? Or would holding a Byte from the connect packet force the buffer to grow forever ? Should we change the API to decode(Bytes) -> Result<Packet,Error> (forcing a .freeze()) to avoid that footgun ?

I'm going to dogfood that in different contexts and see, but it's going to take a while.

from mqttrs.

00imvj00 avatar 00imvj00 commented on August 18, 2024

Yup, at this point we can experiment with couple of approaches and see which feels ergonomic.

Again, the goal is to keep is simple and make it super performant.

from mqttrs.

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.