aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Lu <chris.lu@gmail.com>2018-11-18 20:31:39 -0800
committerChris Lu <chris.lu@gmail.com>2018-11-18 20:31:39 -0800
commitd2924c77483ba17807f3dc56b859fc1541969eae (patch)
tree49215b8ff832a94137f2a78eec4f7b08c4a6a615
parentcb5a10c6a32a7aba4c1abe81be4981ceba64f0c8 (diff)
downloadseaweedfs-d2924c77483ba17807f3dc56b859fc1541969eae.tar.xz
seaweedfs-d2924c77483ba17807f3dc56b859fc1541969eae.zip
merge intervals is a bit faster
-rw-r--r--weed/filer2/filechunks.go60
-rw-r--r--weed/filer2/filechunks_test.go2
2 files changed, 34 insertions, 28 deletions
diff --git a/weed/filer2/filechunks.go b/weed/filer2/filechunks.go
index 79aa50ec8..27ff2f461 100644
--- a/weed/filer2/filechunks.go
+++ b/weed/filer2/filechunks.go
@@ -47,8 +47,6 @@ func CompactFileChunks(chunks []*filer_pb.FileChunk) (compacted, garbage []*file
}
}
- cleanupIntervals(visibles)
-
return
}
@@ -92,8 +90,6 @@ func ViewFromChunks(chunks []*filer_pb.FileChunk, offset int64, size int) (views
}
}
- cleanupIntervals(visibles)
-
return views
}
@@ -114,6 +110,23 @@ var bufPool = sync.Pool{
}
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 {
if v.start < chunk.Offset && chunk.Offset < v.stop {
newVisibles = append(newVisibles, newVisibleInterval(
@@ -136,12 +149,17 @@ func mergeIntoVisibles(visibles []*visibleInterval, chunk *filer_pb.FileChunk) (
newVisibles = append(newVisibles, v)
}
}
- newVisibles = append(newVisibles, newVisibleInterval(
- chunk.Offset,
- chunk.Offset+int64(chunk.Size),
- chunk.FileId,
- chunk.Mtime,
- ))
+ newVisibles = append(newVisibles, newV)
+
+ for i := len(newVisibles) - 1; i > 0; i-- {
+ if newV.start < newVisibles[i-1].start {
+ newVisibles[i] = newVisibles[i-1]
+ } else {
+ newVisibles[i] = newV
+ break
+ }
+ }
+
return
}
@@ -155,21 +173,11 @@ func nonOverlappingVisibleIntervals(chunks []*filer_pb.FileChunk) (visibles []*v
visibles = mergeIntoVisibles(visibles, chunk)
}
- sort.Slice(visibles, func(i, j int) bool {
- return visibles[i].start < visibles[j].start
- })
-
logPrintf("visibles", visibles)
return
}
-func cleanupIntervals(visibles []*visibleInterval) {
- for _, v := range visibles {
- bufPool.Put(v)
- }
-}
-
// find non-overlapping visible intervals
// visible interval map to one file chunk
@@ -181,12 +189,12 @@ type visibleInterval struct {
}
func newVisibleInterval(start, stop int64, fileId string, modifiedTime int64) *visibleInterval {
- b := bufPool.Get().(*visibleInterval)
- b.start = start
- b.stop = stop
- b.fileId = fileId
- b.modifiedTime = modifiedTime
- return b
+ return &visibleInterval{
+ start: start,
+ stop: stop,
+ fileId: fileId,
+ modifiedTime: modifiedTime,
+ }
}
func min(x, y int64) int64 {
diff --git a/weed/filer2/filechunks_test.go b/weed/filer2/filechunks_test.go
index 7e3a20f61..12e495f32 100644
--- a/weed/filer2/filechunks_test.go
+++ b/weed/filer2/filechunks_test.go
@@ -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))
}
- cleanupIntervals(intervals)
-
}
}