Skip to content
This repository has been archived by the owner on Mar 9, 2019. It is now read-only.

Set slice capacity #545

Merged
merged 1 commit into from
Mar 26, 2016
Merged

Set slice capacity #545

merged 1 commit into from
Mar 26, 2016

Conversation

benbjohnson
Copy link
Member

Overview

This commit sets the capacity on slices returned from Bucket.Get() to match the slice length. Previously the capacity would be the size of the mmap max size.

This does not cause any backwards compatibility issues, however, it does allow users to append() to the returned slice since that will cause Go to realloc a new slice on the heap.

Fixes #544

/cc @sasha-s

@@ -117,7 +118,9 @@ func (n *leafPageElement) key() []byte {
// value returns a byte slice of the node value.
func (n *leafPageElement) value() []byte {
buf := (*[maxAllocSize]byte)(unsafe.Pointer(n))
return (*[maxAllocSize]byte)(unsafe.Pointer(&buf[n.pos+n.ksize]))[:n.vsize]
ret := (*[maxAllocSize]byte)(unsafe.Pointer(&buf[n.pos+n.ksize]))[:n.vsize]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't you just set the capacity by doing

ret := (*[maxAllocSize]byte)(unsafe.Pointer(&buf[n.pos+n.ksize]))[:n.vsize:n.vsize]

instead of using reflect?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great minds think alike. ;-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always forget about the third slice parameter. :)

Fixed in 2b4a0e2.

This commit sets the capacity on slices returned from
`Bucket.Get()` to match the slice length. Previously
the capacity would be the size of the mmap max size.

This does not cause any backwards compatibility issues,
however, it does allow users to `append()` to the returned
slice since that will cause Go to realloc a new slice on the
heap.

Fixes boltdb#544
@benbjohnson
Copy link
Member Author

@sasha-s I fixed it on keys as well in 2b4a0e2.

@benbjohnson
Copy link
Member Author

@sasha-s I compared bolt bench on this branch vs master and I don't see any performance difference.

@benbjohnson benbjohnson merged commit b35bb5d into boltdb:master Mar 26, 2016
@benbjohnson benbjohnson deleted the slice-capacity branch March 26, 2016 03:17
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants