You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.funcCommonPrefixLen(a, b []byte) int {
i:=0fori<len(a) &&i<len(b) &&a[i] ==b[i] {
i++
}
returni
}
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:
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.
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.:
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 addCommonPrefixLen(a, b []byte) int
function tobytes
package and optimize it on the supported architectures in the same way asbytes.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 .
The text was updated successfully, but these errors were encountered: