Browse Source

First obvious speedup

pull/1782/head
Ethan Frey 8 years ago
parent
commit
daab270ff7
2 changed files with 39 additions and 10 deletions
  1. +7
    -7
      keys/wordcodec.go
  2. +32
    -3
      keys/wordcodec_test.go

+ 7
- 7
keys/wordcodec.go View File

@ -24,14 +24,14 @@ type WordCodec struct {
check ECC 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 { if len(words) != BankSize {
return codec, errors.Errorf("Bank must have %d words, found %d", BankSize, len(words)) return codec, errors.Errorf("Bank must have %d words, found %d", BankSize, len(words))
} }
res := WordCodec{
res := &WordCodec{
words: words, words: words,
// TODO: configure this outside??? // TODO: configure this outside???
check: NewIEEECRC32(), check: NewIEEECRC32(),
@ -40,7 +40,7 @@ func NewCodec(words []string) (codec WordCodec, err error) {
return res, nil return res, nil
} }
func LoadCodec(bank string) (codec WordCodec, err error) {
func LoadCodec(bank string) (codec *WordCodec, err error) {
words, err := loadBank(bank) words, err := loadBank(bank)
if err != nil { if err != nil {
return codec, err return codec, err
@ -96,7 +96,7 @@ func bytelenFromWords(numWords int) (length int, maybeShorter bool) {
} }
// TODO: add checksum // 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 // always add a checksum to the data
data := c.check.AddECC(raw) data := c.check.AddECC(raw)
numWords := wordlenFromBytes(len(data)) numWords := wordlenFromBytes(len(data))
@ -120,7 +120,7 @@ func (c WordCodec) BytesToWords(raw []byte) (words []string, err error) {
return words, nil return words, nil
} }
func (c WordCodec) WordsToBytes(words []string) ([]byte, error) {
func (c *WordCodec) WordsToBytes(words []string) ([]byte, error) {
l := len(words) l := len(words)
if l == 0 { 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 // GetIndex finds the index of the words to create bytes
// Generates a map the first time it is loaded, to avoid needless // Generates a map the first time it is loaded, to avoid needless
// computation when list is not used. // 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 // generate the first time
if c.bytes == nil { if c.bytes == nil {
b := map[string]int{} b := map[string]int{}


+ 32
- 3
keys/wordcodec_test.go View File

@ -130,12 +130,12 @@ func TestCheckInvalidLists(t *testing.T) {
} }
func getRandWord(c WordCodec) string {
func getRandWord(c *WordCodec) string {
idx := cmn.RandInt() % BankSize idx := cmn.RandInt() % BankSize
return c.words[idx] return c.words[idx]
} }
func getDiffWord(c WordCodec, not string) string {
func getDiffWord(c *WordCodec, not string) string {
w := getRandWord(c) w := getRandWord(c)
if w == not { if w == not {
w = getRandWord(c) w = getRandWord(c)
@ -151,7 +151,7 @@ func TestCheckTypoDetection(t *testing.T) {
for _, bank := range banks { for _, bank := range banks {
codec, err := LoadCodec(bank) codec, err := LoadCodec(bank)
require.Nil(err, "%s: %+v", bank, err) require.Nil(err, "%s: %+v", bank, err)
for i := 0; i < 10; i++ {
for i := 0; i < 1000; i++ {
numBytes := cmn.RandInt()%60 + 1 numBytes := cmn.RandInt()%60 + 1
data := cmn.RandBytes(numBytes) data := cmn.RandBytes(numBytes)
@ -177,5 +177,34 @@ func TestCheckTypoDetection(t *testing.T) {
assert.NotNil(err, "%s: %s", bank, words) 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)
}
}
} }

Loading…
Cancel
Save