Accelerate byte lookup in string with `bytealg/indexbyte`
`bytealg/indexbyte` will use AVX or SSE instruction set, if possible, which could accelerate `CopyStringIn` function by 28%. In worst case(CPU doesn't support SSE), `bytealg/indexbyte` will degenerate to traversal lookup. When dealing with short strings, `bytealg/indexbyte` has the same performance level as before. Signed-off-by: Jianfeng Tan <henry.tjf@antfin.com> Signed-off-by: Hang Su <darcy.sh@antfin.com>
This commit is contained in:
parent
28f431335b
commit
d72c63664b
|
@ -16,6 +16,7 @@
|
||||||
package usermem
|
package usermem
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -270,11 +271,10 @@ func CopyStringIn(ctx context.Context, uio IO, addr Addr, maxlen int, opts IOOpt
|
||||||
n, err := uio.CopyIn(ctx, addr, buf[done:done+readlen], opts)
|
n, err := uio.CopyIn(ctx, addr, buf[done:done+readlen], opts)
|
||||||
// Look for the terminating zero byte, which may have occurred before
|
// Look for the terminating zero byte, which may have occurred before
|
||||||
// hitting err.
|
// hitting err.
|
||||||
for i, c := range buf[done : done+n] {
|
if i := bytes.IndexByte(buf[done:done+n], byte(0)); i >= 0 {
|
||||||
if c == 0 {
|
|
||||||
return stringFromImmutableBytes(buf[:done+i]), nil
|
return stringFromImmutableBytes(buf[:done+i]), nil
|
||||||
}
|
}
|
||||||
}
|
|
||||||
done += n
|
done += n
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return stringFromImmutableBytes(buf[:done]), err
|
return stringFromImmutableBytes(buf[:done]), err
|
||||||
|
|
Loading…
Reference in New Issue