-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optimize allocations on pass of offsets to pipeline (#727)
* memory optimize pass of offsets to pipeline * optimize allocations on source check spam
- Loading branch information
1 parent
28be3e4
commit 6a1c63c
Showing
45 changed files
with
197 additions
and
242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package pipeline | ||
|
||
type Offsets struct { | ||
current int64 | ||
streamOffsets SliceMap | ||
} | ||
|
||
func NewOffsets(current int64, streamOffsets SliceMap) Offsets { | ||
return Offsets{ | ||
current: current, | ||
streamOffsets: streamOffsets, | ||
} | ||
} | ||
|
||
func (o Offsets) byStream(stream string) int64 { | ||
offset, found := o.streamOffsets.Get(StreamName(stream)) | ||
if !found { | ||
return -1 | ||
} | ||
return offset | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package pipeline | ||
|
||
import "testing" | ||
|
||
func TestOffset(t *testing.T) { | ||
offsets := SliceFromMap(map[StreamName]int64{ | ||
"stream1": 100, | ||
"stream2": 200, | ||
}) | ||
|
||
offset := NewOffsets(42, offsets) | ||
|
||
// Test Current method | ||
if got := offset.current; got != 42 { | ||
t.Errorf("Current() = %v; want 42", got) | ||
} | ||
|
||
// Test ByStream method for existing stream | ||
if got := offset.byStream("stream1"); got != 100 { | ||
t.Errorf("ByStream('stream1') = %v; want 100", got) | ||
} | ||
|
||
// Test ByStream method for non-existing stream | ||
if got := offset.byStream("stream3"); got != -1 { | ||
t.Errorf("ByStream('stream3') = %v; want -1", got) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package pipeline | ||
|
||
// SliceMap is a map of streamName to offset. | ||
// It could be just map[k]v, but Go > 1.17 internal map implementation can't | ||
// work with mutable strings that occurs when using unsafe cast from []byte. | ||
// Also it should be not slower on 1-2 keys like linked list, which is often the case for streams per job. | ||
type SliceMap []kv | ||
|
||
type kv struct { | ||
Stream StreamName | ||
Offset int64 | ||
} | ||
|
||
func SliceFromMap(m map[StreamName]int64) SliceMap { | ||
so := make(SliceMap, 0, len(m)) | ||
for k, v := range m { | ||
so = append(so, kv{k, v}) | ||
} | ||
return so | ||
} | ||
|
||
func (so *SliceMap) Get(streamName StreamName) (int64, bool) { | ||
for _, kv := range *so { | ||
if kv.Stream == streamName { | ||
return kv.Offset, true | ||
} | ||
} | ||
return 0, false | ||
} | ||
|
||
func (so *SliceMap) Set(streamName StreamName, offset int64) { | ||
for i := range *so { | ||
if (*so)[i].Stream == streamName { | ||
(*so)[i].Offset = offset | ||
return | ||
} | ||
} | ||
*so = append(*so, kv{streamName, offset}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.