Skip to content

Conversation

@nirs
Copy link
Member

@nirs nirs commented Oct 24, 2024

With this converting the default ubuntu 24.10 qcow2 compressed image to raw is 5.4 times faster:

Before:

% limactl create --tty=false
3.50 GiB / 3.50 GiB [-------------------------------------] 100.00% 317.58 MiB/s

After:

% limactl create --tty=false
3.50 GiB / 3.50 GiB [-------------------------------------] 100.00% 1.67 GiB/s

Fixes #2579

@AkihiroSuda AkihiroSuda added this to the v1.0 milestone Oct 24, 2024

func (r *ProxyReaderAt) ReadAt(p []byte, off int64) (int, error) {
n, err := r.ReaderAt.ReadAt(p, off)
r.Bar.Add(n)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should check negative n?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is documented to return positive value so I think we are safe.

WriteAt writes len(p) bytes from p to the underlying data stream at offset off. It returns the number of bytes written from p (0 <= n <= len(p)) and any error encountered that caused the write to stop early. WriteAt must return a non-nil error if it returns n < len(p).

If we get negative value the convert will fail here:
lima-vm/go-qcow2reader@2cdb233#diff-b4093fac13dc66eefdab36a0bd2f4ee72c635a84a9c5687f8fc3e33185c6d9aeR131

Copy link
Member

@AkihiroSuda AkihiroSuda Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If err is non-nil and n is negative, r.Bar.Add should not be called?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If err is non-nil and n is negative

n is never negative

I pasted io.WriterAt docs before, but io.ReaderAt docs are the same:

ReadAt reads len(p) bytes into p starting at offset off in the underlying input source. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered.

r.Bar.Add should not be called?

err may be io.EOF:

If the n = len(p) bytes returned by ReadAt are at the end of the input source, ReadAt may return either err == EOF or err == nil.

So I think always adding n and let the caller handle errors in the right thing. This is also what pb.Reader is doing: https://github.com/cheggaaa/pb/blob/4ca3463f4bcf4446e1746efc6e484e795cfe7b12/reader.go#L15

We can handle n == 0, but I'm not sure it is helpful to add a check that fail for all good reads, for case that happens only when err != nil, maybe once when per convert.

@nirs nirs force-pushed the go-qcow2reader-convert branch from 5ab6a41 to ad0bd28 Compare October 25, 2024 13:41
@nirs
Copy link
Member Author

nirs commented Oct 25, 2024

Updated to build with latest version.

go.mod Outdated
)

// Temporary raplcement for testing https://github.com/lima-vm/go-qcow2reader/pull/36
replace github.com/lima-vm/go-qcow2reader v0.2.1 => github.com/nirs/go-qcow2reader v0.0.0-20241025131821-46183d3fe6b2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go.mod was not updated yet, should I updated it in this PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a commit to updated the dependency, can be removed if we go with the standard dependency update.

AkihiroSuda
AkihiroSuda previously approved these changes Oct 25, 2024
Copy link
Member

@AkihiroSuda AkihiroSuda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM after updating go.mod

nirs added 2 commits October 25, 2024 22:48
To consume the new convert package.

Signed-off-by: Nir Soffer <[email protected]>
With this converting the default ubuntu 24.10 qcow2 compressed image to
raw is 5.4 times faster:

Before:

    % limactl create --tty=false
    3.50 GiB / 3.50 GiB [-------------------------------------] 100.00% 317.58 MiB/s

After:

    % limactl create --tty=false
    3.50 GiB / 3.50 GiB [-------------------------------------] 100.00% 1.67 GiB/s

Signed-off-by: Nir Soffer <[email protected]>
@nirs nirs force-pushed the go-qcow2reader-convert branch from ad0bd28 to 0221c11 Compare October 25, 2024 19:53
@nirs nirs changed the title WIP: Replace copySparse with go-qcow2reader/convert Replace copySparse with go-qcow2reader/convert Oct 25, 2024
@nirs nirs marked this pull request as ready for review October 25, 2024 19:53
@nirs nirs requested a review from AkihiroSuda October 25, 2024 19:53
Copy link
Member

@AkihiroSuda AkihiroSuda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@AkihiroSuda AkihiroSuda merged commit ab4b4b5 into lima-vm:master Oct 25, 2024
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Converting qcow2 images to raw is too slow

2 participants