[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] speedup sequential file reading
From: |
apache2 |
Subject: |
[PATCH] speedup sequential file reading |
Date: |
Tue, 10 May 2022 21:36:06 +0200 |
User-agent: |
Mutt/1.9.3 (2018-01-21) |
This patch changes the pread-like function for FILE* streams so
they use ftell() before calling fseeko(), and only calls fseeko()
when we are not at the offset we wanted to be at.
It may seem a bit backward to add yet another system call to the fast path here,
but for the zfs.pk pickle I added earlier it almost halves the runtime (good).
The problem seems to be that glibc discards its read buffer when you all fseek,
regardless of whether it's practically a no-op, so this might not be that great
on other libcs/platforms.
There are many things we could do to be more clever about it; like perhaps we
could fread() our way to (offset) instead of fseek, and thus avoid clearing our
buffers.
But I kind of like this patch; it doesn't add a whole lot of complexity, and it
solves the problem (for me). :-)
I have attached the before-and-after 'perf' histograms, I apologize for the
slightly different formats.
diff --git a/libpoke/ios-dev-file.c b/libpoke/ios-dev-file.c
index 66b451a6..1ab310c4 100644
--- a/libpoke/ios-dev-file.c
+++ b/libpoke/ios-dev-file.c
@@ -230,8 +230,9 @@ ios_dev_file_pread (void *iod, void *buf, size_t count,
ios_dev_off offset)
/* We are using FILE* for buffering, rather than low-level fd, so we
have to fake low-level pread by using fseeko. */
- if (fseeko (fio->file, offset, SEEK_SET) == -1)
- return IOD_EOF;
+ if (ftell(fio->file) != offset)
+ if (fseeko (fio->file, offset, SEEK_SET) == -1)
+ return IOD_EOF;
ret = fread (buf, 1, count, fio->file);
if (ferror (fio->file))
perf.hist.0
Description: Text document
perf.hist.1
Description: Text document
- [PATCH] speedup sequential file reading,
apache2 <=