From 8896def814571d549ecb6c412182e27686926702 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 15 Jan 2023 11:48:00 +0800 Subject: [PATCH] perf: short code fast decode and encode --- src/main.cc | 25 ------------------------- src/short_code/data_loader.cc | 26 +++++++------------------- 2 files changed, 7 insertions(+), 44 deletions(-) diff --git a/src/main.cc b/src/main.cc index 3a0ec5c..86f286e 100644 --- a/src/main.cc +++ b/src/main.cc @@ -262,32 +262,7 @@ int main() { // } // } - - const uint32_t ALL_CASES_OFFSET[16] = { - /// 0 1 2 3 - 0, 2942906, 5203298, 8146204, - /// 4 5 6 7 - 8146204, 10468254, 12345199, 14667249, - /// 8 9 10 11 - 14667249, 16989299, 18866244, 21188294, - /// 12 13 14 15 - 21188294, 24131200, 26391592, 29334498, - }; - - /// 2942905 -> 0 - /// 2942906 -> 1 - /// 8146203 -> 2 - /// 8146204 -> 4 - /// 8146205 -> 4 - /// 29334497 -> 14 - -// std::cout << std::upper_bound(ALL_CASES_OFFSET, ALL_CASES_OFFSET + 16, 8146203) - ALL_CASES_OFFSET - 1 << std::endl; - - for (uint32_t i = 0; i < 29334498; ++i) { -// std::cout << ShortCode(i).to_common_code().to_string() << std::endl; -// if (CommonCode::unsafe_create(ShortCode::fast_decode(i)).to_short_code().unwrap() != i) { -// if (ShortCode(i).to_common_code().to_short_code().unwrap() != i) { if (ShortCode::fast_encode(ShortCode::fast_decode(i)) != i) { std::cout << "error" << std::endl; } diff --git a/src/short_code/data_loader.cc b/src/short_code/data_loader.cc index f367288..41b598e 100644 --- a/src/short_code/data_loader.cc +++ b/src/short_code/data_loader.cc @@ -52,29 +52,17 @@ void ShortCode::build_mappings() { // build fast search mappings map_building.unlock(); } -#include #include uint64_t ShortCode::fast_decode(uint32_t short_code) { -// std::cout << "short code: " << short_code << std::endl; - - uint32_t head = std::upper_bound(ALL_CASES_OFFSET, ALL_CASES_OFFSET + 16, short_code) - ALL_CASES_OFFSET - 1; -// std::cout << head << std::endl; - -// printf("%08X\n", AllCases::fetch()[head][ short_code - ALL_CASES_OFFSET[head] ]); - - return ((uint64_t)head << 32) | AllCases::fetch()[head][short_code - ALL_CASES_OFFSET[head]]; + auto offset = std::upper_bound(ALL_CASES_OFFSET, ALL_CASES_OFFSET + 16, short_code) - 1; // binary search + uint64_t head = offset - ALL_CASES_OFFSET; // head index + return (head << 32) | AllCases::fetch()[head][short_code - *offset]; // release common code } uint32_t ShortCode::fast_encode(uint64_t common_code) { -// printf("common code: %09lX\n", common_code); - - uint32_t head = common_code >> 32; - -// std::cout << "head = " << head << std::endl; - - const auto &a = AllCases::fetch(); - - return std::lower_bound(a[head].begin(), a[head].end(), (uint32_t)common_code) - a[head].begin() + ALL_CASES_OFFSET[head]; - + auto head = common_code >> 32; // head index + const auto &ranges = AllCases::fetch()[head]; // available ranges + auto offset = std::lower_bound(ranges.begin(), ranges.end(), (uint32_t)common_code) - ranges.begin(); + return ALL_CASES_OFFSET[head] + offset; // release short code }