Skip to content

Seek requirement in write_to makes it impossible to stream encoded images #1922

@Lonami

Description

@Lonami

I would like to be able ImageBuffer::write_to without requiring my writer to impl Seek.

My specific use case for this functionality is for shotgun, which can write_to(stdout()) (and does not implement Seek, as it might e.g. be piped to another process).

This is more generally applicable to other streaming workloads (such as a web-server that might want to stream data over HTTP but now can't because the output stream is append-only).

Draft

While I'm not familiar with the implementation of this crate, I suspect it should be possible for most formats to simply implement some sort of write_no_seek. The existing write_to could delegate to that method, so there would not be much code duplication. For those formats that do benefit significantly from seeking, two implementations would need to be maintained, which I understand could be more annoying. I suppose shared parts of the encoding could still be extracted into functions to be reused by both write implementations, so I don't think it would be that bad.

Much like how older versions used an intermediate buffer, I suspect the same could apply with this change. My guess is that this intermediate buffer could be smaller. Without this feature, one would need to create a buffer for the entire output, if the final stream is not seekable, which is less ideal.

If this makes sense and would be something that has a chance at being merged, I might give it a go.

Another option would be to use a feature-flag, to make the Seek requirement optional (and enabled by default) claiming it can help improve performance. But in my case it's likely that it degrades performance as now decoding (on a different program) can't start until encoding is complete.

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