Skip to content

Commit

Permalink
refactor(qrcode): optimise loadVersion again, reduce function call in…
Browse files Browse the repository at this point in the history
… limitation scope
  • Loading branch information
yeqown committed Nov 29, 2021
1 parent 548b58f commit fc8dc3d
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,26 +228,19 @@ func binarySearchVersion(initVer int, compare func(*version) int) (hit version,
return hit, found
}

// loadVersion get version config by specified version indicator and error correction level.
// we can speed up this process, by shrink the range to search.
func loadVersion(lv int, ec ecLevel) version {
find := func(v int, ec ecLevel) func(cursor *version) int {
return func(cursor *version) int {
if cursor.Ver == v && cursor.ECLevel == ec {
return 0
}

if cursor.Ver > v || cursor.ECLevel > ec {
return -1
}

return 1
// each version only has 4 items in versions array,
// and them are ordered[ASC] already.
high := lv*4 - 1
low := (lv - 1) * 4

for i := low; i <= high; i++ {
if versions[i].ECLevel == ec {
return versions[i]
}
}

hit, found := binarySearchVersion(lv, find(lv, ec))
if found {
return hit
}

panic(errMissMatchedVersion)
}

Expand Down

0 comments on commit fc8dc3d

Please sign in to comment.