Merge pull request #4023 from Feoramund/simd-index

Vectorize `index_byte`
This commit is contained in:
Jeroen van Rijn
2024-08-10 21:01:41 +02:00
committed by GitHub
6 changed files with 362 additions and 17 deletions
+1
View File
@@ -1,4 +1,5 @@
package benchmarks
@(require) import "bytes"
@(require) import "crypto"
@(require) import "hash"
+116
View File
@@ -0,0 +1,116 @@
package benchmark_bytes
import "core:bytes"
import "core:fmt"
import "core:log"
import "core:testing"
import "core:time"
// These are the normal, unoptimized algorithms.
plain_index_byte :: proc(s: []u8, c: byte) -> (res: int) #no_bounds_check {
for i := 0; i < len(s); i += 1 {
if s[i] == c {
return i
}
}
return -1
}
plain_last_index_byte :: proc(s: []u8, c: byte) -> (res: int) #no_bounds_check {
for i := len(s)-1; i >= 0; i -= 1 {
if s[i] == c {
return i
}
}
return -1
}
sizes := [?]int {
15, 16, 17,
31, 32, 33,
256,
512,
1024,
1024 * 1024,
1024 * 1024 * 1024,
}
run_trial_size :: proc(p: proc([]u8, byte) -> int, size: int, idx: int, warmup: int, runs: int) -> (timing: time.Duration) {
data := make([]u8, size)
defer delete(data)
for i in 0..<size {
data[i] = u8('0' + i % 10)
}
data[idx] = 'z'
accumulator: int
for _ in 0..<warmup {
accumulator += p(data, 'z')
}
for _ in 0..<runs {
start := time.now()
accumulator += p(data, 'z')
done := time.since(start)
timing += done
}
timing /= time.Duration(runs)
log.debug(accumulator)
return
}
HOT :: 3
@test
benchmark_plain_index_cold :: proc(t: ^testing.T) {
report: string
for size in sizes {
timing := run_trial_size(plain_index_byte, size, size - 1, 0, 1)
report = fmt.tprintf("%s\n +++ % 8M | %v", report, size, timing)
timing = run_trial_size(plain_last_index_byte, size, 0, 0, 1)
report = fmt.tprintf("%s\n (last) +++ % 8M | %v", report, size, timing)
}
log.info(report)
}
@test
benchmark_plain_index_hot :: proc(t: ^testing.T) {
report: string
for size in sizes {
timing := run_trial_size(plain_index_byte, size, size - 1, HOT, HOT)
report = fmt.tprintf("%s\n +++ % 8M | %v", report, size, timing)
timing = run_trial_size(plain_last_index_byte, size, 0, HOT, HOT)
report = fmt.tprintf("%s\n (last) +++ % 8M | %v", report, size, timing)
}
log.info(report)
}
@test
benchmark_simd_index_cold :: proc(t: ^testing.T) {
report: string
for size in sizes {
timing := run_trial_size(bytes.index_byte, size, size - 1, 0, 1)
report = fmt.tprintf("%s\n +++ % 8M | %v", report, size, timing)
timing = run_trial_size(bytes.last_index_byte, size, 0, 0, 1)
report = fmt.tprintf("%s\n (last) +++ % 8M | %v", report, size, timing)
}
log.info(report)
}
@test
benchmark_simd_index_hot :: proc(t: ^testing.T) {
report: string
for size in sizes {
timing := run_trial_size(bytes.index_byte, size, size - 1, HOT, HOT)
report = fmt.tprintf("%s\n +++ % 8M | %v", report, size, timing)
timing = run_trial_size(bytes.last_index_byte, size, 0, HOT, HOT)
report = fmt.tprintf("%s\n (last) +++ % 8M | %v", report, size, timing)
}
log.info(report)
}
+89
View File
@@ -0,0 +1,89 @@
package test_core_bytes
import "core:bytes"
import "core:slice"
import "core:testing"
@private SIMD_SCAN_WIDTH :: 8 * size_of(uintptr)
@test
test_index_byte_sanity :: proc(t: ^testing.T) {
// We must be able to find the byte at the correct index.
data := make([]u8, 2 * SIMD_SCAN_WIDTH)
defer delete(data)
slice.fill(data, '-')
INDEX_MAX :: SIMD_SCAN_WIDTH - 1
for offset in 0..<INDEX_MAX {
for idx in 0..<INDEX_MAX {
sub := data[offset:]
sub[idx] = 'o'
if !testing.expect_value(t, bytes.index_byte(sub, 'o'), idx) {
return
}
if !testing.expect_value(t, bytes.last_index_byte(sub, 'o'), idx) {
return
}
sub[idx] = '-'
}
}
}
@test
test_index_byte_empty :: proc(t: ^testing.T) {
a: [1]u8
testing.expect_value(t, bytes.index_byte(a[0:0], 'o'), -1)
testing.expect_value(t, bytes.last_index_byte(a[0:0], 'o'), -1)
}
@test
test_index_byte_multiple_hits :: proc(t: ^testing.T) {
for n in 5..<256 {
data := make([]u8, n)
defer delete(data)
slice.fill(data, '-')
data[n-1] = 'o'
data[n-3] = 'o'
data[n-5] = 'o'
// Find the first one.
if !testing.expect_value(t, bytes.index_byte(data, 'o'), n-5) {
return
}
// Find the last one.
if !testing.expect_value(t, bytes.last_index_byte(data, 'o'), n-1) {
return
}
}
}
@test
test_index_byte_zero :: proc(t: ^testing.T) {
// This test protects against false positives in uninitialized memory.
for n in 1..<256 {
data := make([]u8, n + 64)
defer delete(data)
slice.fill(data, '-')
// Positive hit.
data[n-1] = 0
if !testing.expect_value(t, bytes.index_byte(data[:n], 0), n-1) {
return
}
if !testing.expect_value(t, bytes.last_index_byte(data[:n], 0), n-1) {
return
}
// Test for false positives.
data[n-1] = '-'
if !testing.expect_value(t, bytes.index_byte(data[:n], 0), -1) {
return
}
if !testing.expect_value(t, bytes.last_index_byte(data[:n], 0), -1) {
return
}
}
}
+1
View File
@@ -9,6 +9,7 @@ download_assets :: proc() {
}
}
@(require) import "bytes"
@(require) import "c/libc"
@(require) import "compress"
@(require) import "container"