From 36fcacac440cba790b49925a94b584191b13a03b Mon Sep 17 00:00:00 2001 From: Angelo Probst Date: Wed, 18 Sep 2024 21:14:41 -0300 Subject: [PATCH] wip --- hyperon_das_atomdb_cpp/CMakeLists.txt | 19 +++------- hyperon_das_atomdb_cpp/src/document_types.hpp | 4 +-- hyperon_das_atomdb_cpp/src/nanobind.cpp | 10 +++--- hyperon_das_atomdb_cpp/src/type_aliases.hpp | 1 + .../src/utils/expression_hasher.hpp | 36 +++++++++++++------ 5 files changed, 38 insertions(+), 32 deletions(-) diff --git a/hyperon_das_atomdb_cpp/CMakeLists.txt b/hyperon_das_atomdb_cpp/CMakeLists.txt index 0419eb39..892d032d 100644 --- a/hyperon_das_atomdb_cpp/CMakeLists.txt +++ b/hyperon_das_atomdb_cpp/CMakeLists.txt @@ -4,10 +4,10 @@ project(hyperon_das_atomdb) # Set C++ standard and optimization flags set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -flto -w") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Ofast -flto") set(CMAKE_VERBOSE_MAKEFILE ON) -if (CMAKE_VERSION VERSION_LESS 3.18) +if(CMAKE_VERSION VERSION_LESS 3.18) set(DEV_MODULE Development) else() set(DEV_MODULE Development.Module) @@ -18,9 +18,6 @@ find_package(Python 3.10 COMPONENTS Interpreter ${DEV_MODULE} REQUIRED) # Find OpenSSL package find_package(OpenSSL REQUIRED) -# Set the module path for custom CMake modules -set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) - # Include directories include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/src @@ -30,7 +27,7 @@ include_directories( file(GLOB_RECURSE headers ${CMAKE_CURRENT_SOURCE_DIR}/*.h ${CMAKE_CURRENT_SOURCE_DIR}/*.hpp) -file(GLOB_RECURSE sources +file(GLOB_RECURSE sources ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cc ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp) @@ -42,16 +39,10 @@ execute_process( find_package(nanobind CONFIG REQUIRED) -# Print the source files for debugging purposes -message(STATUS "Source files: ${sources}") - -# Add the executable target -# add_executable(${PROJECT_NAME} ${sources} ${headers}) - -nanobind_add_module(${PROJECT_NAME} MODULE ${cpp} ${sources} ${includes}) +nanobind_add_module(${PROJECT_NAME} MODULE ${sources} ${headers}) # Include OpenSSL directories target_include_directories(${PROJECT_NAME} PRIVATE ${OpenSSL_INCLUDE_DIR}) # Link OpenSSL libraries -target_link_libraries(${PROJECT_NAME} PRIVATE OpenSSL::Crypto) \ No newline at end of file +target_link_libraries(${PROJECT_NAME} PRIVATE OpenSSL::Crypto) diff --git a/hyperon_das_atomdb_cpp/src/document_types.hpp b/hyperon_das_atomdb_cpp/src/document_types.hpp index c6556e67..23bd673a 100644 --- a/hyperon_das_atomdb_cpp/src/document_types.hpp +++ b/hyperon_das_atomdb_cpp/src/document_types.hpp @@ -162,7 +162,7 @@ class Link : public Atom { } } - const string to_string() const noexcept { + const string to_string() const { string result = "Link(" + Atom::to_string(); result += ", composite_type: " + composite_type_list_to_string(composite_type); result += ", named_type_hash: '" + named_type_hash + "'"; @@ -204,7 +204,7 @@ class Link : public Atom { return move(result); } - const string composite_type_list_to_string(const ListOfAny& composite_type) const noexcept { + const string composite_type_list_to_string(const ListOfAny& composite_type) const { string result = "["; for (const auto& element : composite_type) { if (auto str = any_cast(&element)) { diff --git a/hyperon_das_atomdb_cpp/src/nanobind.cpp b/hyperon_das_atomdb_cpp/src/nanobind.cpp index be3bdc65..725ed145 100644 --- a/hyperon_das_atomdb_cpp/src/nanobind.cpp +++ b/hyperon_das_atomdb_cpp/src/nanobind.cpp @@ -477,6 +477,11 @@ NB_MODULE(hyperon_das_atomdb, m) { "buffer"_a = nullopt ); // --------------------------------------------------------------------------------------------- + // adapters submodule -------------------------------------------------------------------------- + nb::module_ adapters = m.def_submodule("adapters"); + nb::class_(adapters, "InMemoryDB") + .def(nb::init<>()); + // --------------------------------------------------------------------------------------------- // exceptions submodule ------------------------------------------------------------------------ nb::module_ exceptions = m.def_submodule("exceptions"); nb::exception(exceptions, "AtomDoesNotExist"); @@ -545,10 +550,5 @@ NB_MODULE(hyperon_das_atomdb, m) { } ); // --------------------------------------------------------------------------------------------- - // adapters submodule -------------------------------------------------------------------------- - nb::module_ adapters = m.def_submodule("adapters"); - nb::class_(adapters, "InMemoryDB") - .def(nb::init<>()); - // --------------------------------------------------------------------------------------------- } diff --git a/hyperon_das_atomdb_cpp/src/type_aliases.hpp b/hyperon_das_atomdb_cpp/src/type_aliases.hpp index 13d57f0b..b7051bc2 100644 --- a/hyperon_das_atomdb_cpp/src/type_aliases.hpp +++ b/hyperon_das_atomdb_cpp/src/type_aliases.hpp @@ -34,6 +34,7 @@ using Pattern_or_Template_List = vector; using ListOfAny = vector; /** + * NOTE: * The following type alias was commented out because std::unordered_map performs * poorly, and it was kept here just as a reminder of the performance implications. * diff --git a/hyperon_das_atomdb_cpp/src/utils/expression_hasher.hpp b/hyperon_das_atomdb_cpp/src/utils/expression_hasher.hpp index e6a5d757..1b18b8a0 100644 --- a/hyperon_das_atomdb_cpp/src/utils/expression_hasher.hpp +++ b/hyperon_das_atomdb_cpp/src/utils/expression_hasher.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include @@ -16,6 +16,16 @@ namespace atomdb { class ExpressionHasher { public: + struct EVP_MD_CTX_RAII { + EVP_MD_CTX* ctx; + EVP_MD_CTX_RAII() : ctx(EVP_MD_CTX_new()) { + if (not ctx) throw std::runtime_error("Failed to create EVP_MD_CTX"); + } + ~EVP_MD_CTX_RAII() { + if (ctx) EVP_MD_CTX_free(ctx); + } + }; + /** * @brief Computes the MD5 hash of the given input string. * @@ -23,20 +33,24 @@ class ExpressionHasher { * @return A string representing the MD5 hash of the input. */ static const string compute_hash(const string& input) { - MD5_CTX ctx; - unsigned char MD5_BUFFER[MD5_DIGEST_LENGTH]; - char HASH[2 * MD5_DIGEST_LENGTH + 1]; + EVP_MD_CTX_RAII ctx_raii; - MD5_Init(&ctx); - MD5_Update(&ctx, input.c_str(), input.length()); - MD5_Final(MD5_BUFFER, &ctx); + unsigned char md5_buffer[EVP_MAX_MD_SIZE]; + unsigned int md5_length = 0; + + if (EVP_DigestInit_ex(ctx_raii.ctx, EVP_md5(), nullptr) != 1 || + EVP_DigestUpdate(ctx_raii.ctx, input.c_str(), input.length()) != 1 || + EVP_DigestFinal_ex(ctx_raii.ctx, md5_buffer, &md5_length) != 1) { + throw std::runtime_error("Failed to compute MD5 hash"); + } - for (unsigned int i = 0; i < MD5_DIGEST_LENGTH; i++) { - sprintf(HASH + 2 * i, "%02x", MD5_BUFFER[i]); + char hash[2 * md5_length + 1]; + for (unsigned int i = 0; i < md5_length; i++) { + sprintf(hash + 2 * i, "%02x", md5_buffer[i]); } - HASH[2 * MD5_DIGEST_LENGTH] = '\0'; + hash[2 * md5_length] = '\0'; - return move(string(HASH)); + return move(std::string(hash)); } /**