From daab270ff71646ded0dc8337021aa2fa8814079d Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Tue, 20 Jun 2017 17:02:28 +0200 Subject: [PATCH] First obvious speedup --- keys/wordcodec.go | 14 +++++++------- keys/wordcodec_test.go | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/keys/wordcodec.go b/keys/wordcodec.go index 5ac32f812..d4c7bce2a 100644 --- a/keys/wordcodec.go +++ b/keys/wordcodec.go @@ -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{} diff --git a/keys/wordcodec_test.go b/keys/wordcodec_test.go index c3a5a2cac..3bdec110f 100644 --- a/keys/wordcodec_test.go +++ b/keys/wordcodec_test.go @@ -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) + } + } }