Replace rust's BLAKE3 with llvm's implementation

This commit is contained in:
Raúl Marín 2023-12-18 15:28:54 +01:00
parent c4f2511596
commit 99d1659b4c
9 changed files with 346 additions and 428 deletions

View File

@ -1,3 +0,0 @@
clickhouse_import_crate(MANIFEST_PATH Cargo.toml)
target_include_directories(_ch_rust_blake3 INTERFACE include)
add_library(ch_rust::blake3 ALIAS _ch_rust_blake3)

View File

@ -1,20 +0,0 @@
[package]
name = "_ch_rust_blake3"
version = "0.1.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
blake3 = "1.2.0"
libc = "0.2.132"
[lib]
crate-type = ["staticlib"]
[profile.release]
debug = true
[profile.release-thinlto]
inherits = "release"
# BLAKE3 module requires "full" LTO (not "thin") to get additional 10% performance benefit
lto = true

View File

@ -1,15 +0,0 @@
#ifndef BLAKE3_H
#define BLAKE3_H
#include <cstdint>
extern "C" {
char *blake3_apply_shim(const char *begin, uint32_t _size, uint8_t *out_char_data);
void blake3_free_char_pointer(char *ptr_to_free);
} // extern "C"
#endif /* BLAKE3_H */

View File

@ -1,31 +0,0 @@
extern crate blake3;
extern crate libc;
use std::ffi::{CString};
use std::slice;
use std::os::raw::c_char;
#[no_mangle]
pub unsafe extern "C" fn blake3_apply_shim(
begin: *const c_char,
size: u32,
out_char_data: *mut u8,
) -> *mut c_char {
if begin.is_null() {
let err_str = CString::new("input was a null pointer").unwrap();
return err_str.into_raw();
}
let input_res = slice::from_raw_parts(begin as *const u8, size as usize);
let mut hasher = blake3::Hasher::new();
hasher.update(input_res);
let mut reader = hasher.finalize_xof();
reader.fill(std::slice::from_raw_parts_mut(out_char_data, blake3::OUT_LEN));
std::ptr::null_mut()
}
// Freeing memory according to docs: https://doc.rust-lang.org/std/ffi/struct.CString.html#method.into_raw
#[no_mangle]
pub unsafe extern "C" fn blake3_free_char_pointer(ptr_to_free: *mut c_char) {
std::mem::drop(CString::from_raw(ptr_to_free));
}

View File

@ -99,6 +99,5 @@ function(add_rust_subdirectory src)
VERBATIM)
endfunction()
add_rust_subdirectory (BLAKE3)
add_rust_subdirectory (skim)
add_rust_subdirectory (prql)

683
rust/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,6 @@
# workspace is required to vendor crates for all packages.
[workspace]
members = [
"BLAKE3",
"skim",
"prql",
]

View File

@ -16,7 +16,7 @@
#include <xxhash.h>
#if USE_BLAKE3
# include <blake3.h>
# include <llvm/Support/BLAKE3.h>
#endif
#include <Common/SipHash.h>
@ -833,13 +833,13 @@ struct ImplBLAKE3
#else
static void apply(const char * begin, const size_t size, unsigned char* out_char_data)
{
auto err_msg = blake3_apply_shim(begin, safe_cast<uint32_t>(size), out_char_data);
if (err_msg != nullptr)
{
auto err_st = std::string(err_msg);
blake3_free_char_pointer(err_msg);
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Function returned error message: {}", err_st);
}
static_assert(LLVM_BLAKE3_OUT_LEN == ImplBLAKE3::length);
auto & result = *reinterpret_cast<std::array<uint8_t, LLVM_BLAKE3_OUT_LEN> *>(out_char_data);
llvm::BLAKE3 hasher;
if (size > 0)
hasher.update(llvm::StringRef(begin, size));
hasher.final(result);
}
#endif
};

View File

@ -19,9 +19,6 @@ endif()
if (TARGET ch_contrib::rdkafka)
set(USE_RDKAFKA 1)
endif()
if (TARGET ch_rust::blake3)
set(USE_BLAKE3 1)
endif()
if (TARGET ch_rust::skim)
set(USE_SKIM 1)
endif()
@ -103,6 +100,7 @@ endif()
if (TARGET ch_contrib::llvm)
set(USE_EMBEDDED_COMPILER ${ENABLE_EMBEDDED_COMPILER})
set(USE_DWARF_PARSER ${ENABLE_DWARF_PARSER})
set(USE_BLAKE3 1)
endif()
if (TARGET ch_contrib::unixodbc)
set(USE_ODBC 1)