Skip to content

Commit

Permalink
Clean up deprecated slice header usage and unused code (#13880)
Browse files Browse the repository at this point in the history
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
  • Loading branch information
dbussink authored Aug 29, 2023
1 parent 2b60c33 commit 99271d4
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 58 deletions.
8 changes: 1 addition & 7 deletions go/hack/hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ limitations under the License.
package hack

import (
"reflect"
"unsafe"
)

Expand All @@ -37,10 +36,5 @@ func String(b []byte) (s string) {
// StringBytes returns the underlying bytes for a string. Modifying this byte slice
// will lead to undefined behavior.
func StringBytes(s string) []byte {
var b []byte
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&b))
hdr.Data = (*reflect.StringHeader)(unsafe.Pointer(&s)).Data
hdr.Cap = len(s)
hdr.Len = len(s)
return b
return unsafe.Slice(unsafe.StringData(s), len(s))
}
4 changes: 1 addition & 3 deletions go/hack/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ limitations under the License.
package hack

import (
"reflect"
"unsafe"
)

Expand All @@ -35,8 +34,7 @@ func strhash(p unsafe.Pointer, h uintptr) uintptr
// This is an optimal hash function which takes an input seed and is potentially implemented in hardware
// for most architectures. This is the same hash function that the language's `map` uses.
func RuntimeMemhash(b []byte, seed uint64) uint64 {
pstring := (*reflect.SliceHeader)(unsafe.Pointer(&b))
return uint64(memhash(unsafe.Pointer(pstring.Data), uintptr(seed), uintptr(pstring.Len)))
return uint64(memhash(unsafe.Pointer(unsafe.SliceData(b)), uintptr(seed), uintptr(len(b))))
}

// RuntimeStrhash provides access to the Go runtime's default hash function for strings.
Expand Down
2 changes: 0 additions & 2 deletions go/mysql/collations/charset/korean/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -17056,8 +17056,6 @@ var decode = [...]uint16{
17629: 0x8A70,
}

const numEncodeTables = 7

// encodeX are the encoding tables from Unicode to EUC-KR code,
// sorted by decreasing length.
// encode0: 20893 entries for runes in [19968, 40861).
Expand Down
2 changes: 0 additions & 2 deletions go/mysql/collations/charset/simplifiedchinese/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -22091,8 +22091,6 @@ var decode = [...]uint16{
23844: 0x4DAE,
}

const numEncodeTables = 5

// encodeX are the encoding tables from Unicode to GBK code,
// sorted by decreasing length.
// encode0: 28965 entries for runes in [11905, 40870).
Expand Down
3 changes: 1 addition & 2 deletions go/mysql/collations/colldata/mysqlucadata.go

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

9 changes: 4 additions & 5 deletions go/mysql/collations/colldata/uca_tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"encoding/json"
"fmt"
"os"
"reflect"
"strconv"
"testing"
"unsafe"
Expand Down Expand Up @@ -95,12 +94,12 @@ func TestWeightsForAllCodepoints(t *testing.T) {
}

func TestWeightTablesAreDeduplicated(t *testing.T) {
sliceptr := func(table uca.Weights) uintptr {
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&table))
return hdr.Data
sliceptr := func(table uca.Weights) unsafe.Pointer {
data := unsafe.SliceData(table)
return unsafe.Pointer(data)
}

uniqueTables := make(map[uintptr]int)
uniqueTables := make(map[unsafe.Pointer]int)
for _, col := range testall() {
var weights uca.Weights
switch col := col.(type) {
Expand Down
14 changes: 0 additions & 14 deletions go/mysql/collations/internal/uca/contractions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package uca

import (
"fmt"
"unicode/utf8"

"vitess.io/vitess/go/mysql/collations/charset"
)
Expand All @@ -28,19 +27,6 @@ type trie struct {
weights []uint16
}

func (t *trie) walkUTF8(remainder []byte) ([]uint16, []byte) {
if len(remainder) > 0 {
cp, width := utf8.DecodeRune(remainder)
if cp == utf8.RuneError && width < 3 {
return nil, nil
}
if ch := t.children[cp]; ch != nil {
return ch.walkUTF8(remainder[width:])
}
}
return t.weights, remainder
}

func (t *trie) walkCharset(cs charset.Charset, remainder []byte, depth int) ([]uint16, []byte, int) {
if len(remainder) > 0 {
cp, width := cs.DecodeRune(remainder)
Expand Down
17 changes: 8 additions & 9 deletions go/mysql/collations/internal/uca/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package uca

import (
"reflect"
"sync"
"unsafe"
)
Expand Down Expand Up @@ -287,29 +286,29 @@ func (Layout_uca_legacy) applyPatches(page []uint16, offset int, weights []uint1
}

type tableWithPatch struct {
tableptr uintptr
patchptr uintptr
tableptr unsafe.Pointer
patchptr unsafe.Pointer
}

var cachedTables = make(map[tableWithPatch]Weights)
var cachedTablesMu sync.Mutex

func lookupCachedTable(table Weights, patch []Patch) (Weights, bool) {
hdr1 := (*reflect.SliceHeader)(unsafe.Pointer(&table))
hdr2 := (*reflect.SliceHeader)(unsafe.Pointer(&patch))
data1 := unsafe.Pointer(unsafe.SliceData(table))
data2 := unsafe.Pointer(unsafe.SliceData(patch))

cachedTablesMu.Lock()
defer cachedTablesMu.Unlock()
tbl, ok := cachedTables[tableWithPatch{hdr1.Data, hdr2.Data}]
tbl, ok := cachedTables[tableWithPatch{tableptr: data1, patchptr: data2}]
return tbl, ok
}

func storeCachedTable(table Weights, patch []Patch, result Weights) {
hdr1 := (*reflect.SliceHeader)(unsafe.Pointer(&table))
hdr2 := (*reflect.SliceHeader)(unsafe.Pointer(&patch))
data1 := unsafe.Pointer(unsafe.SliceData(table))
data2 := unsafe.Pointer(unsafe.SliceData(patch))

cachedTablesMu.Lock()
cachedTables[tableWithPatch{hdr1.Data, hdr2.Data}] = result
cachedTables[tableWithPatch{tableptr: data1, patchptr: data2}] = result
cachedTablesMu.Unlock()
}

Expand Down
15 changes: 1 addition & 14 deletions go/mysql/collations/tools/makecolldata/codegen/tablegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"log"
"math/bits"
"os"
"reflect"

"vitess.io/vitess/go/mysql/collations/internal/uca"
)
Expand Down Expand Up @@ -91,15 +90,14 @@ func (pg *EmbedPageGenerator) WritePage16(g *Generator, varname string, values [

func (pg *EmbedPageGenerator) WriteTrailer(g *Generator, embedfile string) {
unsafe := Package("unsafe")
reflect := Package("reflect")
g.UsePackage("embed")

g.P()
g.P("//go:embed ", embedfile)
g.P("var weightsUCA_embed_data string")
g.P()
g.P("func weightsUCA_embed(pos, length int) []uint16 {")
g.P("return (*[0x7fff0000]uint16)(", unsafe, ".Pointer((*", reflect, ".StringHeader)(", unsafe, ".Pointer(&weightsUCA_embed_data)).Data))[pos:pos+length]")
g.P("return (*[0x7fff0000]uint16)(", unsafe, ".Pointer(", unsafe, ".StringData(weightsUCA_embed_data)))[pos:pos+length]")
g.P("}")
}

Expand All @@ -126,23 +124,12 @@ type entry struct {
weights []uint16
}

func (e *entry) adjustHangulWeights(tb *TableGenerator, jamos []rune) {
for _, jamo := range jamos {
_, entry := tb.entryForCodepoint(jamo)
e.weights = append(e.weights, entry.weights[0], entry.weights[1], entry.weights[2]+1)
}
}

type page struct {
n int
entryCount int
entries [uca.CodepointsPerPage]entry
}

func (p *page) equals(other *page) bool {
return reflect.DeepEqual(p, other)
}

func (p *page) name(uca string) string {
if p.entryCount == 0 {
panic("cannot name empty page")
Expand Down

0 comments on commit 99271d4

Please sign in to comment.