Skip to content

Commit 38616a8

Browse files
authored
Merge pull request apache#133 from proost/refactor-remove-useless-conversion
refactor: remove useless conversion in StringSerde
2 parents d5f6e04 + 4ba2400 commit 38616a8

2 files changed

Lines changed: 361 additions & 16 deletions

File tree

common/item_sketch_string.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ func (f ItemSketchStringHasher) Hash(item string) uint64 {
4343
}
4444

4545
func (f ItemSketchStringSerDe) SizeOf(item string) int {
46-
if len(item) == 0 {
47-
return int(unsafe.Sizeof(uint32(0)))
48-
}
49-
return len(item) + int(unsafe.Sizeof(uint32(0)))
46+
return len(item) + 4
5047
}
5148

5249
func (f ItemSketchStringSerDe) SizeOfMany(mem []byte, offsetBytes int, numItems int) (int, error) {
@@ -77,28 +74,25 @@ func (f ItemSketchStringSerDe) SerializeOneToSlice(item string) []byte {
7774
utf8len := len(item)
7875
bytesOut := make([]byte, utf8len+4)
7976
binary.LittleEndian.PutUint32(bytesOut, uint32(utf8len))
80-
copy(bytesOut[4:], []byte(item))
77+
copy(bytesOut[4:], item)
8178
return bytesOut
8279
}
8380

84-
func (f ItemSketchStringSerDe) SerializeManyToSlice(item []string) []byte {
85-
if len(item) == 0 {
81+
func (f ItemSketchStringSerDe) SerializeManyToSlice(items []string) []byte {
82+
if len(items) == 0 {
8683
return []byte{}
8784
}
8885
totalBytes := 0
89-
numItems := len(item)
90-
serialized2DArray := make([][]byte, numItems)
91-
for i := 0; i < numItems; i++ {
92-
serialized2DArray[i] = []byte(item[i])
93-
totalBytes += len(serialized2DArray[i]) + 4
86+
for _, item := range items {
87+
totalBytes += len(item) + 4
9488
}
9589
bytesOut := make([]byte, totalBytes)
9690
offset := 0
97-
for i := 0; i < numItems; i++ {
98-
utf8len := len(serialized2DArray[i])
91+
for _, item := range items {
92+
utf8len := len(item)
9993
binary.LittleEndian.PutUint32(bytesOut[offset:], uint32(utf8len))
10094
offset += 4
101-
copy(bytesOut[offset:], serialized2DArray[i])
95+
copy(bytesOut[offset:], item)
10296
offset += utf8len
10397
}
10498
return bytesOut
@@ -110,7 +104,7 @@ func (f ItemSketchStringSerDe) DeserializeManyFromSlice(mem []byte, offsetBytes
110104
}
111105
array := make([]string, numItems)
112106
offset := offsetBytes
113-
intSize := int(unsafe.Sizeof(uint32(0)))
107+
intSize := 4
114108
memCap := len(mem)
115109
for i := 0; i < numItems; i++ {
116110
if !checkBounds(offset, intSize, memCap) {

0 commit comments

Comments
 (0)