mirror of
https://github.com/tendermint/tendermint.git
synced 2026-05-02 05:15:44 +00:00
First obvious speedup
This commit is contained in:
@@ -24,14 +24,14 @@ type WordCodec struct {
|
||||
check ECC
|
||||
}
|
||||
|
||||
var _ Codec = WordCodec{}
|
||||
var _ Codec = &WordCodec{}
|
||||
|
||||
func NewCodec(words []string) (codec WordCodec, err error) {
|
||||
func NewCodec(words []string) (codec *WordCodec, err error) {
|
||||
if len(words) != BankSize {
|
||||
return codec, errors.Errorf("Bank must have %d words, found %d", BankSize, len(words))
|
||||
}
|
||||
|
||||
res := WordCodec{
|
||||
res := &WordCodec{
|
||||
words: words,
|
||||
// TODO: configure this outside???
|
||||
check: NewIEEECRC32(),
|
||||
@@ -40,7 +40,7 @@ func NewCodec(words []string) (codec WordCodec, err error) {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func LoadCodec(bank string) (codec WordCodec, err error) {
|
||||
func LoadCodec(bank string) (codec *WordCodec, err error) {
|
||||
words, err := loadBank(bank)
|
||||
if err != nil {
|
||||
return codec, err
|
||||
@@ -96,7 +96,7 @@ func bytelenFromWords(numWords int) (length int, maybeShorter bool) {
|
||||
}
|
||||
|
||||
// TODO: add checksum
|
||||
func (c WordCodec) BytesToWords(raw []byte) (words []string, err error) {
|
||||
func (c *WordCodec) BytesToWords(raw []byte) (words []string, err error) {
|
||||
// always add a checksum to the data
|
||||
data := c.check.AddECC(raw)
|
||||
numWords := wordlenFromBytes(len(data))
|
||||
@@ -120,7 +120,7 @@ func (c WordCodec) BytesToWords(raw []byte) (words []string, err error) {
|
||||
return words, nil
|
||||
}
|
||||
|
||||
func (c WordCodec) WordsToBytes(words []string) ([]byte, error) {
|
||||
func (c *WordCodec) WordsToBytes(words []string) ([]byte, error) {
|
||||
l := len(words)
|
||||
|
||||
if l == 0 {
|
||||
@@ -167,7 +167,7 @@ func (c WordCodec) WordsToBytes(words []string) ([]byte, error) {
|
||||
// GetIndex finds the index of the words to create bytes
|
||||
// Generates a map the first time it is loaded, to avoid needless
|
||||
// computation when list is not used.
|
||||
func (c WordCodec) GetIndex(word string) (int, error) {
|
||||
func (c *WordCodec) GetIndex(word string) (int, error) {
|
||||
// generate the first time
|
||||
if c.bytes == nil {
|
||||
b := map[string]int{}
|
||||
|
||||
@@ -130,12 +130,12 @@ func TestCheckInvalidLists(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func getRandWord(c WordCodec) string {
|
||||
func getRandWord(c *WordCodec) string {
|
||||
idx := cmn.RandInt() % BankSize
|
||||
return c.words[idx]
|
||||
}
|
||||
|
||||
func getDiffWord(c WordCodec, not string) string {
|
||||
func getDiffWord(c *WordCodec, not string) string {
|
||||
w := getRandWord(c)
|
||||
if w == not {
|
||||
w = getRandWord(c)
|
||||
@@ -151,7 +151,7 @@ func TestCheckTypoDetection(t *testing.T) {
|
||||
for _, bank := range banks {
|
||||
codec, err := LoadCodec(bank)
|
||||
require.Nil(err, "%s: %+v", bank, err)
|
||||
for i := 0; i < 10; i++ {
|
||||
for i := 0; i < 1000; i++ {
|
||||
numBytes := cmn.RandInt()%60 + 1
|
||||
data := cmn.RandBytes(numBytes)
|
||||
|
||||
@@ -177,5 +177,34 @@ func TestCheckTypoDetection(t *testing.T) {
|
||||
assert.NotNil(err, "%s: %s", bank, words)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func warmupCodec(bank string) *WordCodec {
|
||||
codec, err := LoadCodec(bank)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
_, err = codec.GetIndex(codec.words[123])
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return codec
|
||||
}
|
||||
|
||||
func BenchmarkWordGeneration(b *testing.B) {
|
||||
// banks := []string{"english", "spanish", "japanese", "chinese_simplified"}
|
||||
bank := "english"
|
||||
|
||||
codec := warmupCodec(bank)
|
||||
b.ResetTimer()
|
||||
|
||||
numBytes := 32
|
||||
data := cmn.RandBytes(numBytes)
|
||||
|
||||
for i := 1; i <= b.N; i++ {
|
||||
_, err := codec.BytesToWords(data)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user