Skip to content

Commit

Permalink
Merge pull request #4 from blackwer/optimize-encoder
Browse files Browse the repository at this point in the history
Fix `unique` bug and add IPO
  • Loading branch information
magland authored Jan 24, 2025
2 parents 5796de5 + 19a753e commit a159e3e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ project(simple_ans VERSION 0.1.0 LANGUAGES CXX)

set(CMAKE_CXX_RELEASE_FLAGS "-O3 -march=native")

include(CheckIPOSupported)
check_ipo_supported(RESULT ipo_supported OUTPUT ipo_error)
if (ipo_supported)
message(STATUS "IPO/LTO is supported")
else()
message(WARNING "IPO/LTO is not supported: ${ipo_error}")
endif()

# Find pybind11
find_package(pybind11 REQUIRED)

Expand Down Expand Up @@ -47,6 +55,11 @@ else()
target_compile_options(_simple_ans PRIVATE -Wall -Wextra -Wpedantic)
endif()

if(ipo_supported AND (CMAKE_BUILD_TYPE STREQUAL "Release"))
set_target_properties(_simple_ans PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE)
endif()


# Install rules
include(GNUInstallDirs)
install(TARGETS _simple_ans
Expand Down
7 changes: 4 additions & 3 deletions simple_ans/cpp/simple_ans.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ void ans_decode_t(T* output,

namespace simple_ans
{
constexpr int lookup_array_threshold = std::numeric_limits<uint16_t>::max() + 1;
constexpr int unique_array_threshold = static_cast<int>(std::numeric_limits<uint16_t>::max()) + 1;
constexpr int lookup_array_threshold = unique_array_threshold;

template <typename T>
std::tuple<std::vector<T>, std::vector<uint64_t>> unique_with_counts(const T* values, size_t n)
Expand All @@ -75,15 +76,15 @@ std::tuple<std::vector<T>, std::vector<uint64_t>> unique_with_counts(const T* va
max_value = std::max(max_value, static_cast<int64_t>(values[i]));
}

if ((max_value - min_value + 1) <= lookup_array_threshold)
if ((max_value - min_value + 1) <= unique_array_threshold)
{
std::vector<uint64_t> raw_counts(max_value - min_value + 1);
for (size_t i = 0; i < n; ++i)
{
raw_counts[values[i] - min_value]++;
}

for (size_t i = 0; i < counts.size(); ++i)
for (size_t i = 0; i < raw_counts.size(); ++i)
{
if (raw_counts[i])
{
Expand Down

0 comments on commit a159e3e

Please sign in to comment.