Skip to content

memoryview, python-side allocation #617

@elbaro

Description

@elbaro
  • Feature request: PyMemoryView
  • Feature request: PyByteArray::new_with_size(num_bytes)
  • Question: Can we allocate some buffer in python-side?

Consider this example:

let vec = vec![0;length];
// write on vec
// ...
return PyBytes::new(&vec[..]).to_object(py);

PyBytes::new is very slow because it copies. A memoryview on Rust slice works. but it is hard to manage the lifetime. If we can allocate on the python side from the beginning, the copy is not necessary. Is this possible?

let buf = some_allocation(length);
let bytes = buf.as_mut_bytes();
// write directly on bytes
// ...
return bytes;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions