Skip to content

perf: use vek32 SIMD for EuclideanDistance#19

Merged
ammario merged 1 commit into
coder:mainfrom
suykerbuyk:perf/euclidean-simd
Apr 9, 2026
Merged

perf: use vek32 SIMD for EuclideanDistance#19
ammario merged 1 commit into
coder:mainfrom
suykerbuyk:perf/euclidean-simd

Conversation

@suykerbuyk
Copy link
Copy Markdown
Contributor

Summary

EuclideanDistance used a plain Go loop while CosineDistance already used vek32 with AVX2 SIMD acceleration. This replaces the loop with vek32.Distance, resolving the TODO at distance.go:20.

Benchmark at 1536 dimensions (OpenAI default) on AMD Ryzen 7 7745HX:

Before: EuclideanDistance  335 ns/op
After:  EuclideanDistance   47 ns/op  (7.1x faster)
Cosine: CosineDistance      80 ns/op  (unchanged, already SIMD)

The vek dependency is already in go.mod — no new dependencies.

Also adds BenchmarkEuclideanDistance to distance_test.go for parity with the existing BenchmarkCosineSimilarity.

Test plan

  • go test ./... — all tests pass
  • go vet ./... — clean
  • go test -bench=BenchmarkEuclidean -benchmem — confirms 7x speedup

🤖 Generated with Claude Code

EuclideanDistance used a plain Go loop while CosineDistance already used
vek32 with AVX2 SIMD acceleration. Replace the loop with vek32.Distance
which provides the same SIMD optimization.

Benchmark at 1536 dimensions (OpenAI default) on AMD Ryzen 7 7745HX:

  Before: EuclideanDistance  335 ns/op
  After:  EuclideanDistance   47 ns/op  (7.1x faster)
  Cosine: CosineDistance      80 ns/op  (unchanged, already SIMD)

Also adds BenchmarkEuclideanDistance to distance_test.go.
Copy link
Copy Markdown
Member

@ammario ammario left a comment

Choose a reason for hiding this comment

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

Simple enough, thank you

@ammario ammario merged commit 1e7c441 into coder:main Apr 9, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants