Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: bytes: add CommonPrefixLen function #59086

Closed
valyala opened this issue Mar 17, 2023 · 1 comment
Closed

proposal: bytes: add CommonPrefixLen function #59086

valyala opened this issue Mar 17, 2023 · 1 comment

Comments

@valyala
Copy link
Contributor

valyala commented Mar 17, 2023

Some compression algorithms and text processing algorithms frequently require finding the common prefix length of two byte slices. It is trivial to implement such function in a straightforward way, e.g.:

// CommonPrefixLen returns the length of common prefix in a and b.
func CommonPrefixLen(a, b []byte) int {
  i := 0
  for i < len(a) && i < len(b) && a[i] == b[i] {
    i++
  }
  return i
}

This function frequently becomes a hot spot in data compression and text processing algorithms. But this simple code isn't fast enough because it performs byte-by-byte comparison. It is possible to increase its' performance via various optimizations. For example, it is possible to compare 8 bytes at a time with slightly complicated code like this one from zstd implementation in Go:

https://github.com/klauspost/compress/blob/7633d627f0ca03e4e9e3f718b44f3328720a0167/zstd/zstd.go#L109-L128

The performance of this code can be improved further via various assembly tricks involving SSE, AVX, etc., which are currently used behind bytes.Equal() function. The resulting code quickly becomes non-trivial and hard to maintain on multiple architectures. It would be great to add CommonPrefixLen(a, b []byte) int function to bytes package and optimize it on the supported architectures in the same way as bytes.Equal() is optimized currently. This will simplify implementing performance-oriented compression and text processing algorithms in pure Go.

cc'ing @klauspost , the author of various compression algorithms in pure Go - see https://github.com/klauspost/compress .

@gopherbot gopherbot added this to the Proposal milestone Mar 17, 2023
@seankhliao
Copy link
Member

Duplicate of #53120

@seankhliao seankhliao marked this as a duplicate of #53120 Mar 17, 2023
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Mar 17, 2023
@golang golang locked and limited conversation to collaborators Mar 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants