<commit_before>package memmove

import (
	"testing"
	"unsafe"
)

var x [2048]byte
var y [2048]byte

var z struct {
	pad1 [64]byte
	data [2048]byte
	pad2 [64]byte
}

func TestMemMove(t *testing.T) {
	testMemMove(t, MemMove)
}
func TestMemMoveSSE2(t *testing.T) {
	testMemMove(t, MemMoveSSE2)
}
func TestMemMoveAVX(t *testing.T) {
	testMemMove(t, MemMoveAVX)
}
func testMemMove(t *testing.T, f func(dst, src *[2048]byte)) {
	for i := range z.pad1 {
		z.pad1[i] = 0
	}
	for i := range z.pad2 {
		z.pad2[i] = 0
	}
	for i := range z.data {
		z.data[i] = 0
	}
	n := byte(1)
	for i := range x {
		x[i] = n
		n *= 3
	}

	f(&z.data, &x)

	for i := range z.pad1 {
		if z.pad1[i] != 0 {
			t.Fatalf("overwrite in prepad %d", i)
		}
	}
	for i := range z.pad2 {
		if z.pad2[i] != 0 {
			t.Fatalf("overwrite in postpad %d", i)
		}
	}
	n = byte(1)
	for i := range z.data {
		if z.data[i] != n {
			t.Fatalf("bad copy @ %d", i)
		}
		n *= 3
	}
}

func BenchmarkMemMove(b *testing.B) {
	benchMemMove(b, MemMove)
}

func BenchmarkMemMoveSSE2(b *testing.B) {
	benchMemMove(b, MemMoveSSE2)
}

func BenchmarkMemMoveAVX(b *testing.B) {
	benchMemMove(b, MemMoveAVX)
}

func benchMemMove(b *testing.B, f func(dst, src *[2048]byte)) {
	b.SetBytes(int64(unsafe.Sizeof(x)))
	for i := 0; i < b.N; i++ {
		f(&x, &y)
	}
}

// Results:
// 3.7 GHz Quad-Core Intel Xeon E5
// BenchmarkMemMove-8    	20000000	        77.5 ns/op	26413.79 MB/s
// BenchmarkMemMoveSSE2-8	50000000	        37.1 ns/op	55234.76 MB/s
// BenchmarkMemMoveAVX-8 	50000000	        37.1 ns/op	55229.94 MB/s
// (prefetch doesn't help, aligned reads/writes don't help)

// Intel(R) Core(TM) i7-4600U CPU @ 2.10GHz
// BenchmarkMemMove-4    	20000000	        85.1 ns/op	24056.98 MB/s
// BenchmarkMemMoveSSE2-4	30000000	        44.4 ns/op	46176.00 MB/s
// BenchmarkMemMoveAVX-4 	50000000	        24.3 ns/op	84248.36 MB/s
<commit_msg>Add more results.<commit_after>package memmove

import (
	"testing"
	"unsafe"
)

var x [2048]byte
var y [2048]byte

var z struct {
	pad1 [64]byte
	data [2048]byte
	pad2 [64]byte
}

func TestMemMove(t *testing.T) {
	testMemMove(t, MemMove)
}
func TestMemMoveSSE2(t *testing.T) {
	testMemMove(t, MemMoveSSE2)
}
func TestMemMoveAVX(t *testing.T) {
	testMemMove(t, MemMoveAVX)
}
func testMemMove(t *testing.T, f func(dst, src *[2048]byte)) {
	for i := range z.pad1 {
		z.pad1[i] = 0
	}
	for i := range z.pad2 {
		z.pad2[i] = 0
	}
	for i := range z.data {
		z.data[i] = 0
	}
	n := byte(1)
	for i := range x {
		x[i] = n
		n *= 3
	}

	f(&z.data, &x)

	for i := range z.pad1 {
		if z.pad1[i] != 0 {
			t.Fatalf("overwrite in prepad %d", i)
		}
	}
	for i := range z.pad2 {
		if z.pad2[i] != 0 {
			t.Fatalf("overwrite in postpad %d", i)
		}
	}
	n = byte(1)
	for i := range z.data {
		if z.data[i] != n {
			t.Fatalf("bad copy @ %d", i)
		}
		n *= 3
	}
}

func BenchmarkMemMove(b *testing.B) {
	benchMemMove(b, MemMove)
}

func BenchmarkMemMoveSSE2(b *testing.B) {
	benchMemMove(b, MemMoveSSE2)
}

func BenchmarkMemMoveAVX(b *testing.B) {
	benchMemMove(b, MemMoveAVX)
}

func benchMemMove(b *testing.B, f func(dst, src *[2048]byte)) {
	b.SetBytes(int64(unsafe.Sizeof(x)))
	for i := 0; i < b.N; i++ {
		f(&x, &y)
	}
}

// Results:
// 3.7 GHz Quad-Core Intel Xeon E5
// BenchmarkMemMove-8    	20000000	        77.5 ns/op	26413.79 MB/s
// BenchmarkMemMoveSSE2-8	50000000	        37.1 ns/op	55234.76 MB/s
// BenchmarkMemMoveAVX-8 	50000000	        37.1 ns/op	55229.94 MB/s
// (prefetch doesn't help, aligned reads/writes don't help)

// Intel(R) Core(TM) i7-4600U CPU @ 2.10GHz
// BenchmarkMemMove-4    	20000000	        85.1 ns/op	24056.98 MB/s
// BenchmarkMemMoveSSE2-4	30000000	        44.4 ns/op	46176.00 MB/s
// BenchmarkMemMoveAVX-4 	50000000	        24.3 ns/op	84248.36 MB/s

// Intel(R) Xeon(R) CPU E5-1650 0 @ 3.20GHz
// BenchmarkMemMove-12     20000000                84.1 ns/op      24357.54 MB/s
// BenchmarkMemMoveSSE2-12 30000000                37.5 ns/op      54555.20 MB/s
// BenchmarkMemMoveAVX-12  30000000                38.2 ns/op      53670.40 MB/s

{'fraction_non_alphanumeric': 0.15638985579313774, 'fraction_numerical': 0.10542018896071606, 'mean_word_length': 2.6506352087114338, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 15, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6461888', 'n_tokens_mistral': 2138, 'n_tokens_neox': 1832, 'n_words': 420}