Skip to content

Commit

Permalink
refactor: slimtrie: use quick creating impl; add slimtrie building be…
Browse files Browse the repository at this point in the history
…nchmark
  • Loading branch information
drmingdrmer committed May 29, 2019
1 parent 39580b9 commit 089c8ea
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 42 deletions.
29 changes: 29 additions & 0 deletions trie/bench_slimtrie_new_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package trie_test

import (
"testing"

"github.com/openacid/slim/encode"
"github.com/openacid/slim/trie"
)

var Output int

func BenchmarkNewSlimTrie(b *testing.B) {
keys := words2
values := make([]uint32, len(keys))
for i := 0; i < len(keys); i++ {
values[i] = uint32(i)
}
b.ResetTimer()
var s int
for i := 0; i < b.N; i++ {
st, err := trie.NewSlimTrie(encode.U32{}, keys, values)
if err != nil {
panic(err)
}
s += int(st.Children.Cnt)
}

Output = s
}
43 changes: 1 addition & 42 deletions trie/slimtrie.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ type SlimTrie struct {
Leaves array.Array
}

const (
newVer = true
)

// NewSlimTrie create an SlimTrie.
// Argument e implements a encode.Encoder to convert user data to serialized
// bytes and back.
Expand All @@ -91,22 +87,7 @@ const (
//
// Since 0.2.0
func NewSlimTrie(e encode.Encoder, keys []string, values interface{}) (*SlimTrie, error) {

if newVer {
return newSlimTrie(e, keys, values)
}

st := &SlimTrie{
Steps: array.U16{},
Leaves: array.Array{},
}
st.Leaves.EltEncoder = e

if keys != nil {
return st, st.load(keys, values)
}

return st, nil
return newSlimTrie(e, keys, values)
}

type subset struct {
Expand Down Expand Up @@ -324,28 +305,6 @@ func getLabels(keys []string, from int, tokeep []bool) ([]byte, uint16) {
return labels, bitmap
}

// load Loads keys and values and builds a SlimTrie.
//
// values must be a slice of data-type of fixed size or compatible with
// SlimTrie.Leaves.Encoder.
func (st *SlimTrie) load(keys []string, values interface{}) (err error) {
ks := bw4.FromStrs(keys)
return st.load4bitWords(ks, values)
}

func (st *SlimTrie) load4bitWords(keys [][]byte, values interface{}) (err error) {

trie, err := NewTrie(keys, values, true)
if err != nil {
return err
}

trie.removeSameLeaf()

err = st.LoadTrie(trie)
return err
}

// LoadTrie compress a standard Trie and store compressed data in it.
//
// Since 0.2.0
Expand Down

0 comments on commit 089c8ea

Please sign in to comment.