Skip to content

Use out-of-band buffers for Python buffer protocol supporting objects #365

Open

Description

@jakirkham

For objects supporting Python's buffer protocol (like memoryview), it would be great to have a path where we use Pickle's protocol 5 for serialization of out-of-band buffers. This should avoid some copies when pickling/unpickling and allow more efficient transmission.

Activity

  1. pierreglaser commented on May 8, 2020

    @pierreglaser
    Member

    Sure. Is there any reason for why CPython does not have a memoryview reducer?

  2. jakirkham commented on May 8, 2020

    @jakirkham
    MemberAuthor

    That's a good question. I don't know. @pitrou, do you know? 🙂

  3. pitrou commented on May 8, 2020

    @pitrou
    Member

    Because it's not obvious how to serialize a memoryview.

  4. jakirkham commented on May 8, 2020

    @jakirkham
    MemberAuthor

    Meaning we don't know what memory to use for backing it?

  5. pitrou commented on May 8, 2020

    @pitrou
    Member

    Yes, exactly. One way of punting the question is to decide to deserialize a memoryview by recreating a bytes object with similar content. But what if your memoryview was backed by e.g. a mmap'ed file?

  6. jakirkham commented on May 8, 2020

    @jakirkham
    MemberAuthor

    Yeah that makes sense. I think in cloudpickle we have chosen to just use bytearray, but it makes sense this would be ambiguous generally and may not make sense outside of cloudpickle.

    What about other objects like bytes, bytearray, array, etc. where we do know how to allocate for them? Does it make sense for CPython to support out-of-band buffer serialization for them or are there some issues there as well?

  7. pitrou commented on May 8, 2020

    @pitrou
    Member

    It would certainly make sense indeed. But it's not necessarily trivial and someone has to do the work ;-)

    (note that a memoryview may have been sliced or casted... of course, you could choose to forbid those cases, but you would still have to detect them)

  8. jakirkham commented on May 8, 2020

    @jakirkham
    MemberAuthor

    What challenges do you envision in implementing out-of-band serialization for those other objects?

    (yeah that's fair. there is a lot the current implementation brushes under the rug ;)

  9. jakirkham commented on May 22, 2020

    @jakirkham
    MemberAuthor

    FWIW opened upstream issue ( https://bugs.python.org/issue40718 ) on serializing builtin bytes-like types.

  10. jakirkham commented on May 22, 2020

    @jakirkham
    MemberAuthor

    The one trick for performing out-of-band pickling of memoryviews is we would need access to the protocol version (as is the case with __reduce_ex__. A function won't work as we can't pass more arguments to the function when using the dispatch_table. Maybe we can workaround this constraint by using a method and accessing the protocol version via class state. Thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions