Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(proto): use memmove on amd64 for int columns #9

Merged
merged 4 commits into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions proto/cmd/ch-gen-col/infer.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ package proto
// inferNumeric infers t as numeric type, otherwise returns false.
func (c *ColAuto) inferNumeric(t ColumnType) bool {
switch t {
{{- range . }}
{{- range . }}{{- if not .Unsafe }}
case {{ .ColumnType }}:
c.Data = new({{ .Type }})
{{- end }}
{{- end }}{{- end }}
default:
return false
}
Expand Down
26 changes: 22 additions & 4 deletions proto/cmd/ch-gen-col/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ const (
)

type Variant struct {
Kind Kind
Signed bool
Bits int
Kind Kind
Signed bool
Bits int
GenerateUnsafe bool
Unsafe bool
}

type Variants []Variant
Expand Down Expand Up @@ -304,8 +306,24 @@ func run() error {
})
}
}
for i, v := range variants {
if v.Bits <= 64 && !v.Byte() {
variants[i].GenerateUnsafe = true
variants = append(variants, Variant{
Kind: v.Kind,
Signed: v.Signed,
Bits: v.Bits,
GenerateUnsafe: true,
Unsafe: true,
})
}
}
for _, v := range variants {
base := "col_" + v.ElemLower() + "_gen"
base := "col_" + v.ElemLower()
if v.Unsafe {
base += "_unsafe"
}
base += "_gen"
if err := write(base, v, tpl); err != nil {
return errors.Wrap(err, "write")
}
Expand Down
40 changes: 33 additions & 7 deletions proto/cmd/ch-gen-col/main.tpl
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
{{- /*gotype: github.com/go-faster/ch/proto/cmd/ch-gen-col.Variant*/ -}}
{{- if .GenerateUnsafe }}
//go:build {{ if .Unsafe }}amd64 && !nounsafe{{ else }}!amd64 || nounsafe{{ end }}
{{- end }}
// Code generated by ./cmd/ch-gen-int, DO NOT EDIT.

package proto

import (
"encoding/binary"
{{- if .IsFloat }}
"math"
"math"
{{- end }}
{{- if .Unsafe }}
"unsafe"
{{- end }}
"github.com/go-faster/errors"

"github.com/go-faster/errors"
)

// ClickHouse uses LittleEndian.
Expand Down Expand Up @@ -112,17 +119,35 @@ func (c *{{ .Type }}) DecodeColumn(r *Reader, rows int) error {
{{- else if .SingleByte }}
v := *c
v = append(v, make([]{{ .ElemType }}, rows)...)
{{- if .Unsafe }}
s := *(*struct {
ernado marked this conversation as resolved.
Show resolved Hide resolved
Data unsafe.Pointer
Len uintptr
Cap uintptr
})(unsafe.Pointer(&v))
dst := *(*[]byte)(unsafe.Pointer(&s))
copy(dst, data)
{{- else }}
for i := range data {
v[i] = {{ .ElemType }}(data[i])
}
{{- end }}
*c = v
{{- else }}
v := *c
// Move bound check out of loop.
//
// See https://github.com/golang/go/issues/30945.
_ = data[len(data)-size]
for i := 0; i <= len(data)-size; i += size {
{{- if .Unsafe }}
v = append(v, make([]{{ .ElemType }}, rows)...)
s := *(*struct {
Data unsafe.Pointer
Len uintptr
Cap uintptr
})(unsafe.Pointer(&v))
s.Len *= size
s.Cap *= size
dst := *(*[]byte)(unsafe.Pointer(&s))
copy(dst, data)
{{- else }}
for i := 0; i < len(data); i += size {
v = append(v,
{{- if .IsFloat }}
math.{{ .Name }}frombits(bin.{{ .BinFunc }}(data[i:i+size])),
Expand All @@ -133,6 +158,7 @@ func (c *{{ .Type }}) DecodeColumn(r *Reader, rows int) error {
{{- end }}
)
}
{{- end }}
*c = v
{{- end }}
return nil
Expand Down
3 changes: 3 additions & 0 deletions proto/cmd/ch-gen-col/test.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{{- /*gotype: github.com/go-faster/ch/proto/cmd/ch-gen-col.Variant*/ -}}
{{- if .GenerateUnsafe }}
//go:build {{ if .Unsafe }}amd64 && !nounsafe{{ else }}nounsafe{{ end }}
{{- end }}
// Code generated by ./cmd/ch-gen-int, DO NOT EDIT.

package proto
Expand Down
9 changes: 4 additions & 5 deletions proto/col_date32_gen.go

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

2 changes: 2 additions & 0 deletions proto/col_date32_gen_test.go

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

103 changes: 103 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.

Loading