-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
util: add PreAlloc4Row and Insert for Chunk and List #7916
Changes from 8 commits
af650d0
577f676
837a517
6eb77ad
3b239b5
f91ebc0
79e6eb0
52497c0
b73c2e9
0ac6476
f5e4088
af8fc82
e59f8d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ package chunk | |
|
||
import ( | ||
"encoding/binary" | ||
"reflect" | ||
"unsafe" | ||
|
||
"github.com/cznic/mathutil" | ||
|
@@ -277,6 +278,63 @@ func (c *Chunk) AppendPartialRow(colIdx int, row Row) { | |
} | ||
} | ||
|
||
// PreAlloc4Row pre-allocates the memory space for a Row. | ||
// Nothing except for the nullBitMap of c.columns will be pre-written. | ||
func (c *Chunk) PreAlloc4Row(row Row) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about:
// PreAlloc pre-allocates the memory space in a Chunk to store the Row.
// NOTE:
// 1. The Chunk must be empty or holds no useful data.
// 2. The schema of the Row must be the same with the Chunk.
// 3. This API is paired with the `Insert()` function, which inserts all the
// rows into the Chunk after the pre-allocation. |
||
for i, srcCol := range row.c.columns { | ||
dstCol := c.columns[i] | ||
dstCol.appendNullBitmap(!srcCol.isNull(row.idx)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's better to only pre-allocate the memory for null bitmap by calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We write the nullBitMap info here is because that, |
||
elemLen := len(srcCol.elemBuf) | ||
if !srcCol.isFixed() { | ||
elemLen = int(srcCol.offsets[row.idx+1] - srcCol.offsets[row.idx]) | ||
dstCol.offsets = append(dstCol.offsets, int32(len(dstCol.data)+elemLen)) | ||
} | ||
dstCol.length++ | ||
needCap := len(dstCol.data) + elemLen | ||
alivxxx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if needCap <= cap(dstCol.data) { | ||
(*reflect.SliceHeader)(unsafe.Pointer(&dstCol.data)).Len = len(dstCol.data) + elemLen | ||
continue | ||
} | ||
// Grow the capacity according to golang.growslice. | ||
newCap := cap(dstCol.data) | ||
doubleCap := newCap << 1 | ||
if needCap > doubleCap { | ||
newCap = needCap | ||
} else { | ||
if len(dstCol.data) < 1024 { | ||
newCap = doubleCap | ||
} else { | ||
for 0 < newCap && newCap < needCap { | ||
newCap += newCap / 4 | ||
} | ||
if newCap <= 0 { | ||
newCap = needCap | ||
} | ||
} | ||
} | ||
dstCol.data = make([]byte, len(dstCol.data)+elemLen, newCap) | ||
} | ||
} | ||
|
||
// Insert inserts `row` on the position specified by `rowIdx`. | ||
// Note: Insert will cover the origin data, it should be called after | ||
// PreAlloc4Row. | ||
func (c *Chunk) Insert(rowIdx int, row Row) { | ||
for i, srcCol := range row.c.columns { | ||
dstCol := c.columns[i] | ||
var srcStart, srcEnd, destStart, destEnd int | ||
if srcCol.isFixed() { | ||
srcElemLen, destElemLen := len(srcCol.elemBuf), len(dstCol.elemBuf) | ||
alivxxx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
srcStart, destStart = row.idx*srcElemLen, rowIdx*destElemLen | ||
srcEnd, destEnd = srcStart+srcElemLen, destStart+destElemLen | ||
alivxxx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
srcStart, srcEnd = int(srcCol.offsets[row.idx]), int(srcCol.offsets[row.idx+1]) | ||
destStart, destEnd = int(dstCol.offsets[rowIdx]), int(dstCol.offsets[rowIdx+1]) | ||
} | ||
copy(dstCol.data[destStart:destEnd], srcCol.data[srcStart:srcEnd]) | ||
} | ||
} | ||
|
||
// Append appends rows in [begin, end) in another Chunk to a Chunk. | ||
func (c *Chunk) Append(other *Chunk, begin, end int) { | ||
for colID, src := range other.columns { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,6 +140,35 @@ func (l *List) Reset() { | |
l.consumedIdx = -1 | ||
} | ||
|
||
// PreAlloc4Row pre-allocates the storage memory for a Row. | ||
// Note: this function will *ONLY* allocate the needed memory for `row`, the | ||
// data will *NOT* be written into the List. List.Insert can be called to write | ||
// the data later on. | ||
func (l *List) PreAlloc4Row(row Row) (ptr RowPtr) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the comment of this function should also be updated. |
||
chkIdx := len(l.chunks) - 1 | ||
if chkIdx == -1 || l.chunks[chkIdx].NumRows() >= l.chunks[chkIdx].Capacity() { | ||
newChk := l.allocChunk() | ||
l.chunks = append(l.chunks, newChk) | ||
if chkIdx != l.consumedIdx { | ||
l.memTracker.Consume(l.chunks[chkIdx].MemoryUsage()) | ||
l.consumedIdx = chkIdx | ||
} | ||
chkIdx++ | ||
} | ||
chk := l.chunks[chkIdx] | ||
rowIdx := chk.NumRows() | ||
chk.PreAlloc4Row(row) | ||
l.length++ | ||
return RowPtr{ChkIdx: uint32(chkIdx), RowIdx: uint32(rowIdx)} | ||
} | ||
|
||
// Insert inserts `row` on the position specified by `ptr`. | ||
// Note: Insert will cover the origin data, it should be called after | ||
// PreAlloc4Row. | ||
func (l *List) Insert(ptr RowPtr, row Row) { | ||
l.chunks[ptr.ChkIdx].Insert(int(ptr.RowIdx), row) | ||
} | ||
|
||
// ListWalkFunc is used to walk the list. | ||
// If error is returned, it will stop walking. | ||
type ListWalkFunc = func(row Row) error | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we preallocate the memory for a batch of rows? The memory grow stratagem may allocate a lot of unused memory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preallocate memory for a batch of rows cannot avoid the waste problem brought by memory usage increment of chunk.data.
Unless the batch contains all of the rows which should be pre-allocated.
I adjust the strategy according to https://github.com/golang/go/blob/master/src/runtime/slice.go#L116-L135