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
Currently each loop calls 2 times make([]byte) and copy. This could be reduced to 1, by storing the leftover as a length, instead of a byte slice:
buf:=make([]byte, chunkSize)
leftover:=0for {
n, err:=file.Read(buf[leftover:]) // append to the leftoveriferr!=nil {
iferrors.Is(err, io.EOF) {
break
}
panic(err)
}
toSend:=buf[:leftover+n]
lastNewLineIndex:=bytes.LastIndexByte(toSend, '\n')
buf=make([]byte, chunkSize) // prepare a new buffer for next readleftover=copy(buf, toSend[lastNewLineIndex+1:])
chunkStream<-toSend[:lastNewLineIndex+1]
}
On a sample file, this code is about 10% faster than the current version
The text was updated successfully, but these errors were encountered:
Hi, first of all thank you for sharing your code!
I was quite impressed by the reading speed, and I think that I found a way to make this part a bit faster:
1brc/main.go
Lines 131 to 155 in 8513d5e
Currently each loop calls 2 times
make([]byte)
andcopy
. This could be reduced to 1, by storing the leftover as a length, instead of a byte slice:On a sample file, this code is about 10% faster than the current version
The text was updated successfully, but these errors were encountered: