Git Product home page Git Product logo

Comments (2)

eieio avatar eieio commented on May 5, 2024

Hi Mark,

Thanks for your patience getting back to you.

Unfortunately, the serializer isn't designed to handle encoding from sources other than memory. It would be very easy to break the protocol otherwise, since the serializer wouldn't be sure about what happened between calls.

Are you on a platform that supports mmap? If that's the case, then there might be something we can add to support your use case.

The library supports a concept called fungibility, which means you can define several versions of a data type and the library can use any of them interchangeably, as long as they would produce the same format data. We can use this mechanism to write an alternate Sprite that can point to an mmap'ed file.

To expand on your example, it might look something like this:

// Define the primary protocol for a Sprite.
struct Sprite {
  // Other fields ...
  std::vector<uint8_t> img;

  NOP_STRUCTURE(Sprite, /* other fields */, img);
};

// Define an alternate data type that conforms to the Sprite protocol and can be used
// with mmap.
struct SpriteMmap {
  // Other fields ...
  size_t img_size;
  uint8_t* img_data;

  NOP_STRUCTURE(SpriteMmap, /* other fields */, (img_size, img_data));
};

// Make sure SpriteMmap is fungible with Sprite.
static_assert(nop::IsFungible<Sprite, SpriteMmap>::value);

int fd = // Open file for reading.
const size_t file_size = // Size of image file in bytes.
uint8_t* img_data = mmap(nullptr, file_size, PROT_READ, MAP_SHARED, fd, /*offset*/ 0);

SpriteMmap sprite_file{/*other fields*/, file_size, img_data};

std::vector<uint8_t> buffer(nop::Encoding<SpriteMap>::Size(sprite_file));
nop::Serializer<nop::BufferEncoder> serializer{buffer.data(), buffer.size()};

serializer.Write(sprite_file);

Right now logical buffers only support grouping a size member with an array member, but we could add support for grouping a size member with a pointer member.

Does this sound like it would solve your problem?

from libnop.

ambitslix avatar ambitslix commented on May 5, 2024

Thanks, it would not work, but supporting zero copy is quite fundamental in my use case and I'm surprised that libraries don't support it. I'm using yas now where it is possible do to.

from libnop.

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.