Skip to content

Add option to decode type bytestring as a mutable bytearray rather than bytes #266

@twoturtles

Description

@twoturtles

Things to check first

  • I have searched the existing issues and didn't find my feature already requested there

Feature description

For performance reasons, it would be nice if there were an option / flag to decode cbor bytes strings (cbor type 2) as python bytearrays rather than bytes. This would allow the user to modify the buffer rather than do a memcpy first to convert it to a mutable type.

This change would actually eliminate two memcpys. The current implementation (I'm looking at decode.c decode_definite_long_bytestring()) copies the packet (in chunks) into a bytearray and then copies the bytearray to a bytes object before returning it. Then to get a mutable array, the user has to copy the bytes into a bytearray.
If the decode just returned the bytearray instead, that would remove two memcpys (bytearray -> bytes during decode, then bytes -> bytearray in python to make it mutable).

From looking through the issues, I think this might affect other use cases:
Issue #15
Issue #240
So maybe it would be best to make it an optional decode flag?

Use case

My use case is receiving cbor packets that include large chunks of bytes (images). My end goal is to get a mutable numpy array using np.frombuffer().

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions