From fc8dc3dbe425204732bad7146210b2ecb7ec072a Mon Sep 17 00:00:00 2001 From: yeqown Date: Mon, 29 Nov 2021 10:30:23 +0800 Subject: [PATCH] refactor(qrcode): optimise loadVersion again, reduce function call in limitation scope --- version.go | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/version.go b/version.go index 9906c5f..2c019e3 100644 --- a/version.go +++ b/version.go @@ -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) }