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

ReadFrom trashes the old instace #121

Closed
omerfirmak opened this issue Apr 17, 2023 · 1 comment · Fixed by #122
Closed

ReadFrom trashes the old instace #121

omerfirmak opened this issue Apr 17, 2023 · 1 comment · Fixed by #122

Comments

@omerfirmak
Copy link
Contributor

ReadFrom creates a new bitset and overrides the previous one when it does not encounter any error during the process.

bitset/bitset.go

Lines 949 to 968 in d53c44e

newset := New(uint(length))
if uint64(newset.length) != length {
return 0, errors.New("unmarshalling error: type mismatch")
}
var item [8]byte
nWords := wordsNeeded(uint(length))
reader := bufio.NewReader(io.LimitReader(stream, 8*int64(nWords)))
for i := 0; i < nWords; i++ {
if _, err := io.ReadFull(reader, item[:]); err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return 0, err
}
newset.set[i] = binaryOrder.Uint64(item[:])
}
*b = *newset

For applications that constantly marshal/unmarshal bitsets, this is a huge problem. What ReadFrom should do instead is try to reuse the existing bitset and the underlying []uint64 it has. It should avoid creating any one-time temporary heap buffers as well.

To be honest I don't know why it works like that but the fact that ReadFrom does not modify the existing instance if it encounters an error makes this a little tricky.

@lemire
Copy link
Member

lemire commented Apr 17, 2023

I agree. Let us fix that in the next release.

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 a pull request may close this issue.

2 participants