Skip to content

Commit

Permalink
Merge pull request #11 from go-faster/feat/optimize-encoding
Browse files Browse the repository at this point in the history
feat(proto): optimize encoding
  • Loading branch information
ernado authored Dec 31, 2021
2 parents 35fccb5 + cbe7d25 commit 5eb8f3a
Show file tree
Hide file tree
Showing 79 changed files with 797 additions and 380 deletions.
33 changes: 0 additions & 33 deletions proto/cmd/ch-gen-col/main.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ package proto

import (
"encoding/binary"
{{- if .IsFloat }}
"math"
{{- end }}
)

var _ = binary.LittleEndian // clickHouse uses LittleEndian
Expand Down Expand Up @@ -60,33 +57,3 @@ func (c *ColArr) Append{{ .Name }}(data []{{ .ElemType }}) {
*d = append(*d, data...)
c.Offsets = append(c.Offsets, uint64(len(*d)))
}

// EncodeColumn encodes {{ .Name }} rows to *Buffer.
func (c {{ .Type }}) EncodeColumn(b *Buffer) {
{{- if .Byte }}
b.Buf = append(b.Buf, c...)
{{- else if .SingleByte }}
start := len(b.Buf)
b.Buf = append(b.Buf, make([]byte, len(c))...)
for i := range c {
b.Buf[i + start] = {{ .UnsignedType }}(c[i])
}
{{- else }}
const size = {{ .Bits }} / 8
offset := len(b.Buf)
b.Buf = append(b.Buf, make([]byte, size * len(c))...)
for _, v := range c {
{{ .BinPut }}(
b.Buf[offset:offset+size],
{{- if .IsFloat }}
math.{{ .Name }}bits(v),
{{- else if .Cast }}
{{ .UnsignedType }}(v),
{{- else }}
v,
{{- end }}
)
offset += size
}
{{- end }}
}
30 changes: 30 additions & 0 deletions proto/cmd/ch-gen-col/safe.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,33 @@ func (c *{{ .Type }}) DecodeColumn(r *Reader, rows int) error {
{{- end }}
return nil
}

// EncodeColumn encodes {{ .Name }} rows to *Buffer.
func (c {{ .Type }}) EncodeColumn(b *Buffer) {
{{- if .Byte }}
b.Buf = append(b.Buf, c...)
{{- else if .SingleByte }}
start := len(b.Buf)
b.Buf = append(b.Buf, make([]byte, len(c))...)
for i := range c {
b.Buf[i + start] = {{ .UnsignedType }}(c[i])
}
{{- else }}
const size = {{ .Bits }} / 8
offset := len(b.Buf)
b.Buf = append(b.Buf, make([]byte, size * len(c))...)
for _, v := range c {
{{ .BinPut }}(
b.Buf[offset:offset+size],
{{- if .IsFloat }}
math.{{ .Name }}bits(v),
{{- else if .Cast }}
{{ .UnsignedType }}(v),
{{- else }}
v,
{{- end }}
)
offset += size
}
{{- end }}
}
22 changes: 22 additions & 0 deletions proto/cmd/ch-gen-col/unsafe.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,25 @@ func (c *{{ .Type }}) DecodeColumn(r *Reader, rows int) error {
}
return nil
}

// EncodeColumn encodes {{ .Name }} rows to *Buffer.
func (c {{ .Type }}) EncodeColumn(b *Buffer) {
if len(c) == 0 {
return
}
offset := len(b.Buf)
{{- if .SingleByte }}
b.Buf = append(b.Buf, make([]byte, len(c))...)
{{- else }}
const size = {{ .Bits }} / 8
b.Buf = append(b.Buf, make([]byte, size * len(c))...)
{{ end }}
s := *(*slice)(unsafe.Pointer(&c))
{{- if not .SingleByte }}
s.Len *= {{ .Bytes }}
s.Cap *= {{ .Bytes }}
{{- end }}
src := *(*[]byte)(unsafe.Pointer(&s))
dst := b.Buf[offset:]
copy(dst, src)
}
14 changes: 0 additions & 14 deletions proto/col_date32_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions proto/col_date32_safe_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions proto/col_date32_unsafe_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 0 additions & 14 deletions proto/col_date_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions proto/col_date_safe_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions proto/col_date_unsafe_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 0 additions & 14 deletions proto/col_datetime64_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions proto/col_datetime64_safe_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions proto/col_datetime64_unsafe_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 0 additions & 14 deletions proto/col_datetime_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions proto/col_datetime_safe_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions proto/col_datetime_unsafe_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 0 additions & 14 deletions proto/col_decimal128_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions proto/col_decimal128_safe_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions proto/col_decimal128_unsafe_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5eb8f3a

Please sign in to comment.