Skip to content

Commit 97ffeba

Browse files
author
roman.atachiants@careem.com
committedAug 1, 2021
implicit sorting instead
1 parent da8aa9e commit 97ffeba

File tree

3 files changed

+6
-15
lines changed

3 files changed

+6
-15
lines changed
 

‎sorted/codecs.go

+2-11
Original file line numberDiff line numberDiff line change
@@ -135,21 +135,12 @@ func (c *uintSliceCodec) DecodeTo(d *binary.Decoder, rv reflect.Value) (err erro
135135

136136
// ------------------------------------------------------------------------------
137137

138-
// TimestampCodec returns a timestamp codec.
139-
func TimestampCodec(sort bool) binary.Codec {
140-
return &timestampCodec{
141-
sort: sort,
142-
}
143-
}
144-
145-
type timestampCodec struct {
146-
sort bool // Whether codec needs to sort or not
147-
}
138+
type timestampCodec struct{}
148139

149140
// EncodeTo encodes a value into the encoder.
150141
func (c timestampCodec) EncodeTo(e *binary.Encoder, rv reflect.Value) (err error) {
151142
data := rv.Interface().(Timestamps)
152-
if c.sort {
143+
if !sort.IsSorted(Uint64s(data)) {
153144
sort.Sort(Uint64s(data))
154145
}
155146

‎sorted/codecs_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ func BenchmarkSortedSlice(b *testing.B) {
6262

6363
/*
6464
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
65-
BenchmarkTimes/encode-12 238062 4586 ns/op 3337 B/op 5 allocs/op
66-
BenchmarkTimes/decode-12 202978 6026 ns/op 8219 B/op 2 allocs/op
65+
BenchmarkTimes/encode-12 17698 76181 ns/op 30893 B/op 6 allocs/op
66+
BenchmarkTimes/decode-12 22126 54699 ns/op 81977 B/op 2 allocs/op
6767
*/
6868
func BenchmarkTimes(b *testing.B) {
6969
var times Timestamps
70-
for i := uint64(0); i < 1000; i++ {
70+
for i := uint64(0); i < 10000; i++ {
7171
times = append(times, uint64(time.Now().Unix())+i)
7272
}
7373
enc, _ := binary.Marshal(&times)

‎sorted/types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,5 @@ type Timestamps []uint64
100100

101101
// GetBinaryCodec retrieves a custom binary codec.
102102
func (ts *Timestamps) GetBinaryCodec() binary.Codec {
103-
return TimestampCodec(true)
103+
return timestampCodec{}
104104
}

0 commit comments

Comments
 (0)
Please sign in to comment.