From 9b392c42e10e4b1d314c0dfd52b1fb2c51929195 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 15 Jan 2023 22:38:18 +0800 Subject: [PATCH] perf: speed up tiny decoder --- src/all_cases/all_cases.cc | 2 +- src/all_cases/all_cases.h | 5 ++++- src/short_code/data_loader.cc | 42 ++++++++++++++++++++++++----------- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/all_cases/all_cases.cc b/src/all_cases/all_cases.cc index 5a0e2c1..27b52ba 100644 --- a/src/all_cases/all_cases.cc +++ b/src/all_cases/all_cases.cc @@ -47,7 +47,7 @@ void AllCases::build_data() { // find all cases uint32_t broken = check_case(head, basic_ranges[index]); // check and get broken address auto range_rev = Common::range_reverse(basic_ranges[index]); // reversed range if (broken) { // invalid case - auto delta = (uint32_t)1 << (32 - broken * 2); // this <--delta--> next possible range + auto delta = (uint32_t)1 << (32 - broken * 2); // this --delta--> next possible range auto next_min = (range_rev & ~(delta - 1)) + delta; while (Common::range_reverse(basic_ranges[++index]) < next_min); // located next range --index; diff --git a/src/all_cases/all_cases.h b/src/all_cases/all_cases.h index 7f2915e..4904717 100644 --- a/src/all_cases/all_cases.h +++ b/src/all_cases/all_cases.h @@ -18,11 +18,14 @@ public: static enum Status status(); static const std::vector (&fetch())[16]; + // TODO: only for test + static int check_case(uint32_t head, uint32_t range); + private: static bool available; static std::mutex building; static std::vector data[16]; static void build_data(); - static int check_case(uint32_t head, uint32_t range); +// static int check_case(uint32_t head, uint32_t range); }; diff --git a/src/short_code/data_loader.cc b/src/short_code/data_loader.cc index 4c07e8d..bd1d4d7 100644 --- a/src/short_code/data_loader.cc +++ b/src/short_code/data_loader.cc @@ -88,27 +88,43 @@ uint64_t ShortCode::tiny_decode_demo(uint32_t short_code) { uint32_t prefix = offset - RANGE_PREFIX_OFFSET_[head]; // range prefix short_code -= *offset; - uint32_t start_point = BASIC_RANGES_OFFSET_[prefix]; + /// search target range + const auto &basic_ranges = BasicRanges::fetch(); + for (auto index = BASIC_RANGES_OFFSET_[prefix]; index < basic_ranges.size(); ++index) { - printf("prefix: %X\n", prefix); - printf("start at %d\n", start_point); - printf("next %d valid case\n", short_code); + uint32_t range = basic_ranges[index]; // traverse basic ranges - uint32_t range; + uint32_t broken = AllCases::check_case(head, basic_ranges[index]); // check and get broken address - for (auto index = start_point; index < BasicRanges::fetch().size(); ++index) { // traverse basic ranges - range = BasicRanges::fetch()[index]; - if (Common::check_case(head, range)) { // search for valid cases + auto range_rev = Common::range_reverse(basic_ranges[index]); // reversed range + + if (broken) { // invalid case + auto delta = (uint32_t)1 << (32 - broken * 2); // this --delta--> next possible range + auto next_min = (range_rev & ~(delta - 1)) + delta; + while (Common::range_reverse(basic_ranges[++index]) < next_min); // located next range + --index; + } else { if (!short_code--) { // short code approximate - break; // found target range + + /// found target range + return head << 32 | range_rev; + } } - } - - printf("range -> %08X\n", Common::range_reverse(range)); - return (uint64_t)head << 32 | Common::range_reverse(range); +// if (Common::check_case(head, range)) { // search for valid cases +// +// if (!short_code--) { // short code approximate +// +// /// found target range +// return head << 32 | Common::range_reverse(range); +// +// } +// +// } + } + return 0; // never reach when input valid }