hexdump performance test, part 1

spoiler alert: it's slow as molasses

part 2 will be presented at a later date

(yes, please don't fill 8GB of memory with
random data and hexdump it)

Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
Leah Rowe
2026-03-29 13:23:31 +01:00
parent cec3de5c9e
commit b70ee41c5c
3 changed files with 24 additions and 6 deletions
+17 -1
View File
@@ -50,6 +50,22 @@ hextonum(char ch_s)
} }
/* basically hexdump -C */ /* basically hexdump -C */
/*
TODO: optimise this
write a full util for hexdump
how to optimise:
don't call print tens of thousands of times!
convert the numbers manually, and cache everything
in a BUFSIZ sized buffer, with everything properly
aligned. i worked out that i could fit 79 rows
in a 8KB buffer (1264 bytes of numbers represented
as strings in hex)
this depends on the OS, and would be calculated at
runtime.
then:
don't use printf. just write it to stdout (basically
a simple cat implementation)
*/
void void
spew_hex(const void *data, size_t len) spew_hex(const void *data, size_t len)
{ {
@@ -64,7 +80,7 @@ spew_hex(const void *data, size_t len)
for (i = 0; i < len; i += 16) { for (i = 0; i < len; i += 16) {
printf("%08zx ", i); printf("%0*zx ", sizeof(size_t) * 2, i);
for (j = 0; j < 16; j++) { for (j = 0; j < 16; j++) {
+3 -2
View File
@@ -179,7 +179,6 @@ scatn(ssize_t sc, const char **sv,
size_t max, char **rval) size_t max, char **rval)
{ {
int saved_errno = errno; int saved_errno = errno;
char *final = NULL; char *final = NULL;
char *rcur = NULL; char *rcur = NULL;
char *rtmp = NULL; char *rtmp = NULL;
@@ -192,7 +191,9 @@ scatn(ssize_t sc, const char **sv,
for (i = 0; i < sc; i++) { for (i = 0; i < sc; i++) {
if (i == 0) { if (if_err(sv[i] == NULL, EFAULT))
goto err;
else if (i == 0) {
if (sdup(sv[0], max, &final) < 0) if (sdup(sv[0], max, &final) < 0)
goto err; goto err;
continue; continue;
+4 -3
View File
@@ -18,6 +18,7 @@ main(int argc, char **argv)
{ {
int same = 0; int same = 0;
char *buf; char *buf;
size_t size = 8589934592;
(void) argc, (void) argv; (void) argc, (void) argv;
(void) errhook(exit_cleanup); (void) errhook(exit_cleanup);
@@ -26,12 +27,12 @@ main(int argc, char **argv)
/* https://man.openbsd.org/pledge.2 */ /* https://man.openbsd.org/pledge.2 */
xpledgex("stdio", NULL); xpledgex("stdio", NULL);
buf = rmalloc(BUFSIZ); buf = rmalloc(size);
if (!memcmp(buf, buf + (BUFSIZ >> 1), BUFSIZ >> 1)) if (!memcmp(buf, buf + (size >> 1), size >> 1))
same = 1; same = 1;
if (argc < 2) /* no spew */ if (argc < 2) /* no spew */
spew_hex(buf, BUFSIZ); spew_hex(buf, size);
free_and_set_null(&buf); free_and_set_null(&buf);
fprintf(stderr, "\n%s\n", same ? "You win!" : "You lose!"); fprintf(stderr, "\n%s\n", same ? "You win!" : "You lose!");