Browse Source

feat: ShortCode convert with low-memory in fast mode

legacy
Dnomd343 2 years ago
parent
commit
65b25a6bac
  1. 2
      src/common_code/CMakeLists.txt
  2. 18
      src/main.cc
  3. 28
      src/short_code/data_loader.cc
  4. 3
      src/short_code/short_code.h

2
src/common_code/CMakeLists.txt

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.0)
add_library(common_code common_code.cc) add_library(common_code common_code.cc)
target_link_libraries(common_code utils) target_link_libraries(common_code utils raw_code)

18
src/main.cc

@ -173,6 +173,9 @@ int main() {
// } // }
// } // }
// ShortCode::speed_up(ShortCode::FAST);
AllCases::build();
// std::cout << "start benchmark" << std::endl; // std::cout << "start benchmark" << std::endl;
auto start_time = clock(); auto start_time = clock();
@ -278,7 +281,20 @@ int main() {
/// 8146205 -> 4 /// 8146205 -> 4
/// 29334497 -> 14 /// 29334497 -> 14
std::cout << std::upper_bound(ALL_CASES_OFFSET, ALL_CASES_OFFSET + 16, 8146203) - ALL_CASES_OFFSET - 1 << std::endl; // 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;
}
}
// printf("%09lX\n", ShortCode::fast_decode(14323231));
// std::cout << ShortCode::fast_encode(0x6EC0F8800) << std::endl;
std::cerr << (clock() - start_time) * 1000 / CLOCKS_PER_SEC << "ms" << std::endl; std::cerr << (clock() - start_time) * 1000 / CLOCKS_PER_SEC << "ms" << std::endl;
// std::cerr << (clock() - start_time) * 1000000 / CLOCKS_PER_SEC << "us" << std::endl; // std::cerr << (clock() - start_time) * 1000000 / CLOCKS_PER_SEC << "us" << std::endl;

28
src/short_code/data_loader.cc

@ -1,6 +1,7 @@
#include "all_cases.h" #include "all_cases.h"
#include "short_code.h" #include "short_code.h"
#include "basic_ranges.h" #include "basic_ranges.h"
#include "short_code_mark.h"
std::mutex ShortCode::map_building; std::mutex ShortCode::map_building;
bool ShortCode::fast_mode_available = false; bool ShortCode::fast_mode_available = false;
@ -50,3 +51,30 @@ void ShortCode::build_mappings() { // build fast search mappings
} }
map_building.unlock(); map_building.unlock();
} }
#include <iostream>
#include <algorithm>
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]];
}
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];
}

3
src/short_code/short_code.h

@ -34,6 +34,9 @@ public:
speed_up(mode); speed_up(mode);
} }
static uint64_t fast_decode(uint32_t short_code);
static uint32_t fast_encode(uint64_t common_code);
private: private:
uint32_t code; uint32_t code;
static std::mutex map_building; static std::mutex map_building;

Loading…
Cancel
Save