mirror of
https://github.com/majd/ipatool
synced 2026-11-26 09:17:52 +08:00
perf: read ahead when reading version metadata from the IPA
get-version-metadata reads Info.plist out of the remote IPA through httpRangeReaderAt. archive/zip parses the central directory through a 4 KiB bufio.Reader, and each ReadAt was a separate range request. Those requests go through the Store "transfer" client, which opens the emulator for every request, so an IPA with many files cost hundreds of emulator round trips: 298 requests and ~71s (55s CPU) for one Chrome version. On a miss, httpRangeReaderAt now fetches at least 1 MiB starting at the requested offset and serves later reads from that window. Reads that cross EOF are still clamped, and only the end of the IPA is downloaded.
This commit is contained in:
1 parent
20290220cd
commit
6194bf4ecd
2 files changed
+70
-21
No files matched your search
@@ -165,6 +165,26 @@ var _ = Describe("HTTPRangeReaderAt", func() {
|
||||
Expect(string(buf[:n])).To(Equal("ef"))
|
||||
Expect(rangeLog).To(ContainElement("bytes=4-5"))
|
||||
})
|
||||
|
||||
It("serves small sequential reads from one read-ahead request", func() {
|
||||
data := bytes.Repeat([]byte("0123456789abcdef"), 4096)
|
||||
rangeLog := []string{}
|
||||
server, _, _ := testIPAServerWithRangeLog(data, &rangeLog)
|
||||
defer server.Close()
|
||||
|
||||
reader, _, err := newHTTPRangeReaderAt(newDownloadTestClient(), server.URL)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
buf := make([]byte, 4096)
|
||||
for off := int64(1024); off+int64(len(buf)) <= int64(len(data)); off += int64(len(buf)) {
|
||||
n, err := reader.ReadAt(buf, off)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(buf[:n]).To(Equal(data[off : off+int64(n)]))
|
||||
}
|
||||
|
||||
// The first entry is the size probe made when the reader is opened.
|
||||
Expect(rangeLog).To(Equal([]string{"bytes=0-0", fmt.Sprintf("bytes=1024-%d", len(data)-1)}))
|
||||
})
|
||||
})
|
||||
|
||||
var _ = Describe("AppStore (GetVersionMetadata)", func() {
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
apphttp "github.com/majd/ipatool/v2/pkg/http"
|
||||
@@ -31,10 +32,19 @@ type versionMetadata struct {
|
||||
ReleaseDate time.Time
|
||||
}
|
||||
|
||||
// rangeReaderReadAhead is the minimum size of a remote read. archive/zip parses the central
|
||||
// directory through a 4 KiB bufio.Reader, and every request made through the Store client
|
||||
// runs in the emulator, so each miss reads ahead and later reads are served from memory.
|
||||
const rangeReaderReadAhead int64 = 1 << 20
|
||||
|
||||
type httpRangeReaderAt struct {
|
||||
client apphttp.Client[interface{}]
|
||||
url string
|
||||
size int64
|
||||
|
||||
mu sync.Mutex
|
||||
windowStart int64
|
||||
window []byte
|
||||
}
|
||||
|
||||
func newHTTPRangeReaderAt(client apphttp.Client[interface{}], url string) (*httpRangeReaderAt, int64, error) {
|
||||
@@ -120,24 +130,51 @@ func (r *httpRangeReaderAt) ReadAt(p []byte, off int64) (int, error) {
|
||||
return 0, io.EOF
|
||||
}
|
||||
|
||||
requestEnd := off + int64(len(p)) - 1
|
||||
rangeEnd := requestEnd
|
||||
|
||||
if rangeEnd >= r.size {
|
||||
rangeEnd = r.size - 1
|
||||
end := off + int64(len(p))
|
||||
if end > r.size {
|
||||
end = r.size
|
||||
}
|
||||
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
if off < r.windowStart || end > r.windowStart+int64(len(r.window)) {
|
||||
fetchEnd := off + max(int64(len(p)), rangeReaderReadAhead)
|
||||
if fetchEnd > r.size {
|
||||
fetchEnd = r.size
|
||||
}
|
||||
|
||||
data, err := r.fetch(off, fetchEnd-1)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
r.windowStart = off
|
||||
r.window = data
|
||||
}
|
||||
|
||||
n := copy(p[:end-off], r.window[off-r.windowStart:])
|
||||
|
||||
if off+int64(len(p)) > r.size {
|
||||
return n, io.EOF
|
||||
}
|
||||
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// fetch downloads bytes start...stop (inclusive) with a single range request.
|
||||
func (r *httpRangeReaderAt) fetch(start, stop int64) ([]byte, error) {
|
||||
req, err := r.client.NewRequest("GET", r.url, nil)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to create request: %w", err)
|
||||
return nil, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Accept-Encoding", "identity")
|
||||
req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", off, rangeEnd))
|
||||
req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", start, stop))
|
||||
|
||||
res, err := r.client.Do(req)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("request failed: %w", err)
|
||||
return nil, fmt.Errorf("request failed: %w", err)
|
||||
}
|
||||
|
||||
if res != nil && res.Body != nil {
|
||||
@@ -145,25 +182,17 @@ func (r *httpRangeReaderAt) ReadAt(p []byte, off int64) (int, error) {
|
||||
}
|
||||
|
||||
if res.StatusCode != http.StatusPartialContent {
|
||||
return 0, fmt.Errorf("expected partial content response, got status %d", res.StatusCode)
|
||||
return nil, fmt.Errorf("expected partial content response, got status %d", res.StatusCode)
|
||||
}
|
||||
|
||||
readLength := int(rangeEnd-off) + 1
|
||||
|
||||
n, err := io.ReadFull(res.Body, p[:readLength])
|
||||
if err == io.ErrUnexpectedEOF || err == io.EOF {
|
||||
return n, io.EOF
|
||||
}
|
||||
data := make([]byte, stop-start+1)
|
||||
|
||||
_, err = io.ReadFull(res.Body, data)
|
||||
if err != nil {
|
||||
return n, fmt.Errorf("failed to read response body: %w", err)
|
||||
return nil, fmt.Errorf("failed to read response body: %w", err)
|
||||
}
|
||||
|
||||
if requestEnd >= r.size {
|
||||
return n, io.EOF
|
||||
}
|
||||
|
||||
return n, nil
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (t *appstore) readVersionMetadataFromIPA(url string) (versionMetadata, error) {
|
||||
|
||||
Reference in new issue
Block a user