merge intervals is a bit faster

This commit is contained in:
Chris Lu 2018-11-18 20:31:39 -08:00
parent cb5a10c6a3
commit d2924c7748
2 changed files with 34 additions and 28 deletions

View file

@ -47,8 +47,6 @@ func CompactFileChunks(chunks []*filer_pb.FileChunk) (compacted, garbage []*file
} }
} }
cleanupIntervals(visibles)
return return
} }
@ -92,8 +90,6 @@ func ViewFromChunks(chunks []*filer_pb.FileChunk, offset int64, size int) (views
} }
} }
cleanupIntervals(visibles)
return views return views
} }
@ -114,6 +110,23 @@ var bufPool = sync.Pool{
} }
func mergeIntoVisibles(visibles []*visibleInterval, chunk *filer_pb.FileChunk) (newVisibles []*visibleInterval) { func mergeIntoVisibles(visibles []*visibleInterval, chunk *filer_pb.FileChunk) (newVisibles []*visibleInterval) {
newV := newVisibleInterval(
chunk.Offset,
chunk.Offset+int64(chunk.Size),
chunk.FileId,
chunk.Mtime,
)
length := len(visibles)
if length == 0 {
return append(visibles, newV)
}
last := visibles[length-1]
if last.stop <= chunk.Offset {
return append(visibles, newV)
}
for _, v := range visibles { for _, v := range visibles {
if v.start < chunk.Offset && chunk.Offset < v.stop { if v.start < chunk.Offset && chunk.Offset < v.stop {
newVisibles = append(newVisibles, newVisibleInterval( newVisibles = append(newVisibles, newVisibleInterval(
@ -136,12 +149,17 @@ func mergeIntoVisibles(visibles []*visibleInterval, chunk *filer_pb.FileChunk) (
newVisibles = append(newVisibles, v) newVisibles = append(newVisibles, v)
} }
} }
newVisibles = append(newVisibles, newVisibleInterval( newVisibles = append(newVisibles, newV)
chunk.Offset,
chunk.Offset+int64(chunk.Size), for i := len(newVisibles) - 1; i > 0; i-- {
chunk.FileId, if newV.start < newVisibles[i-1].start {
chunk.Mtime, newVisibles[i] = newVisibles[i-1]
)) } else {
newVisibles[i] = newV
break
}
}
return return
} }
@ -155,21 +173,11 @@ func nonOverlappingVisibleIntervals(chunks []*filer_pb.FileChunk) (visibles []*v
visibles = mergeIntoVisibles(visibles, chunk) visibles = mergeIntoVisibles(visibles, chunk)
} }
sort.Slice(visibles, func(i, j int) bool {
return visibles[i].start < visibles[j].start
})
logPrintf("visibles", visibles) logPrintf("visibles", visibles)
return return
} }
func cleanupIntervals(visibles []*visibleInterval) {
for _, v := range visibles {
bufPool.Put(v)
}
}
// find non-overlapping visible intervals // find non-overlapping visible intervals
// visible interval map to one file chunk // visible interval map to one file chunk
@ -181,12 +189,12 @@ type visibleInterval struct {
} }
func newVisibleInterval(start, stop int64, fileId string, modifiedTime int64) *visibleInterval { func newVisibleInterval(start, stop int64, fileId string, modifiedTime int64) *visibleInterval {
b := bufPool.Get().(*visibleInterval) return &visibleInterval{
b.start = start start: start,
b.stop = stop stop: stop,
b.fileId = fileId fileId: fileId,
b.modifiedTime = modifiedTime modifiedTime: modifiedTime,
return b }
} }
func min(x, y int64) int64 { func min(x, y int64) int64 {

View file

@ -193,8 +193,6 @@ func TestIntervalMerging(t *testing.T) {
t.Fatalf("failed to compact test case %d, len %d expected %d", i, len(intervals), len(testcase.Expected)) t.Fatalf("failed to compact test case %d, len %d expected %d", i, len(intervals), len(testcase.Expected))
} }
cleanupIntervals(intervals)
} }
} }