Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions benchmarks/blake3-scalar/benchmark.stderr.expected
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[blake3] hashing ./default.input
[blake3] input size = 65536
[blake3] returned Hash("609ac06ee32a629fbbc50ae8262526962f9414e16ba4175ee65394056b8b51fa")
[blake3] input size = 3100000
[blake3] returned Hash("059bb0ac5450537c4d30544423687821a498c9af13c23782eed5e69fde7730ea")
Binary file modified benchmarks/blake3-scalar/default.input
Binary file not shown.
7 changes: 5 additions & 2 deletions benchmarks/blake3-simd/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ RUN mkdir /benchmark
# I am not sure that all of the parameters passed below are needed; this is what I received. It
# would be preferable if the blake3 would tell us what SIMD parameters they plan to use for their
# NPM package (https://www.npmjs.com/package/blake3), which currently uses only scalar instructions.
# The benchmark reads its workload from disk into the heap, and runs under a
# plain WASI runtime (no JS), so we use a fixed linear memory large enough for
# the workload and disable memory growth (growth would import an Emscripten JS
# function the runtime does not provide).
RUN emcc -O3 -s STANDALONE_WASM=1 \
-s INITIAL_MEMORY=1048576 -s MAXIMUM_MEMORY=16777216 \
-s ALLOW_MEMORY_GROWTH=1 -s TOTAL_STACK=131072 \
-s INITIAL_MEMORY=67108864 -s ALLOW_MEMORY_GROWTH=0 -s TOTAL_STACK=131072 \
-s "EXPORTED_FUNCTIONS=['_main']" \
-msimd128 -msse4.1 -msse4.2 \
-DBLAKE3_NO_SSE41 -DBLAKE3_NO_AVX2 -DBLAKE3_NO_AVX512 \
Expand Down
75 changes: 68 additions & 7 deletions benchmarks/blake3-simd/benchmark.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,84 @@
#include "blake3.h"
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "sightglass.h"
#define BUFFER_SIZE 65536

// This benchmark is built with Emscripten (for the SSE2 -> Wasm SIMD blake3
// implementation), whose libc filesystem does not reach WASI preopened
// directories. So, like the splay benchmark, we read the workload from disk by
// calling the WASI `path_open`/`fd_read` syscalls directly. This lets the input
// be resized without recompiling, similar to the blake3-scalar benchmark.
#define WASI_IMPORT(name) \
__attribute__((import_module("wasi_snapshot_preview1"), import_name(name)))

typedef struct {
const void *buf;
size_t len;
} wasi_iovec_t;

WASI_IMPORT("path_open")
int wasi_path_open(int fd, int dirflags, const char *path, size_t path_len,
int oflags, uint64_t rights_base, uint64_t rights_inheriting,
int fdflags, int *opened_fd);

WASI_IMPORT("fd_read")
int wasi_fd_read(int fd, const wasi_iovec_t *iovs, size_t iovs_len, size_t *nread);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A little unfortunate to bake in WASIp1 -- if we have refactor-energy here, maybe we could point it toward using wasi-sdk instead?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I can look into this in a follow up


// The benchmark directory is preopened by the runner as the first preopen (fd 3).
#define PREOPEN_FD 3
#define RIGHT_FD_READ (1ULL << 1)
#define RIGHT_FD_SEEK (1ULL << 2)

int main()
{
const char *path = "default.input";
int fd = -1;
int rc = wasi_path_open(PREOPEN_FD, 0, path, strlen(path), 0,
RIGHT_FD_READ | RIGHT_FD_SEEK,
RIGHT_FD_READ | RIGHT_FD_SEEK, 0, &fd);
if (rc != 0 || fd < 0)
{
fprintf(stderr, "failed to open default.input (rc=%d)\n", rc);
return 1;
}

// Read the whole file into a growable buffer.
size_t cap = 1 << 20, len = 0;
unsigned char *buffer = (unsigned char *)malloc(cap);
for (;;)
{
if (len == cap)
{
cap *= 2;
buffer = (unsigned char *)realloc(buffer, cap);
}
wasi_iovec_t iov = {buffer + len, cap - len};
size_t nread = 0;
rc = wasi_fd_read(fd, &iov, 1, &nread);
if (rc != 0)
{
fprintf(stderr, "fd_read failed (rc=%d)\n", rc);
return 1;
}
if (nread == 0)
break;
len += nread;
}

fprintf(stderr, "[blake3] hashing ./default.input\n");
fprintf(stderr, "[blake3] input size = %zu\n", len);

// Initialize the hasher.
blake3_hasher hasher;
blake3_hasher_init(&hasher);

// Initialize the 64K buffer.
unsigned char buffer[BUFFER_SIZE] = { 0 };

// Define the hash output; BLAKE3_OUT_LEN is the default output length, 32 bytes.
uint8_t output[BLAKE3_OUT_LEN];
fprintf(stderr, "[blake3] hashing a zero-filled buffer of %d bytes\n", BUFFER_SIZE);

bench_start();
blake3_hasher_update(&hasher, buffer, BUFFER_SIZE);
blake3_hasher_update(&hasher, buffer, len);
blake3_hasher_finalize(&hasher, output, BLAKE3_OUT_LEN);
bench_end();

Expand All @@ -30,5 +90,6 @@ int main()
}
fprintf(stderr, "\n");

free(buffer);
return 0;
}
5 changes: 3 additions & 2 deletions benchmarks/blake3-simd/benchmark.stderr.expected
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[blake3] hashing a zero-filled buffer of 65536 bytes
[blake3] returned 3bdeaf8f8e98780b318106aafdc3ca257f73df123d97b69112b26044c91a7d56
[blake3] hashing ./default.input
[blake3] input size = 3100000
[blake3] returned 059bb0ac5450537c4d30544423687821a498c9af13c23782eed5e69fde7730ea
Binary file modified benchmarks/blake3-simd/benchmark.wasm
Binary file not shown.
Binary file added benchmarks/blake3-simd/default.input
Binary file not shown.
2 changes: 1 addition & 1 deletion benchmarks/bz2/benchmark.stdout.expected
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
bz2: starting
compressed length: 57507
compressed length: 10945
bz2: OK
Loading
Loading