Skip to content

Commit

Permalink
optimized hash fn
Browse files Browse the repository at this point in the history
  • Loading branch information
pioner921227 committed Jan 18, 2025
1 parent 3cd2109 commit 24f1020
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 35 deletions.
14 changes: 7 additions & 7 deletions cpp/react-native-xxhash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ void xxhash::install(jsi::Runtime* rt_ptr) {
[](jsi::Runtime& rt, const jsi::Value& thisVal, const jsi::Value* args,
size_t count) {
const jsi::Value& arg = args[0];

if (!arg.isString()) [[unlikely]] {
throw jsi::JSError(rt, "Argument is not a 'string'");
}

std::stringstream ss;
char result[33];

xxhash::make_hash<HashSize::BITS_128>(arg.asString(rt).utf8(rt), ss);
xxhash::make_hash_128(arg.asString(rt).utf8(rt), result);

return jsi::String::createFromUtf8(rt, ss.str());
return jsi::String::createFromUtf8(rt, result);
});

jsi::Function hash64 = jsi::Function::createFromHostFunction(
Expand All @@ -30,11 +30,11 @@ void xxhash::install(jsi::Runtime* rt_ptr) {
throw jsi::JSError(rt, "Argument is not a 'string'");
}

std::stringstream ss;
char result[17];

xxhash::make_hash<HashSize::BITS_64>(arg.asString(rt).utf8(rt), ss);
xxhash::make_hash_64(arg.asString(rt).utf8(rt), result);

return jsi::String::createFromUtf8(rt, ss.str());
return jsi::String::createFromUtf8(rt, result);
});

runtime.global().setProperty(runtime, "__xxhash128", std::move(hash128));
Expand Down
40 changes: 12 additions & 28 deletions cpp/react-native-xxhash.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,26 @@
#ifndef XXHASH_H
#define XXHASH_H
#include <jsi/jsi.h>
#include "xxhash.h"
#include <iomanip>
#include <sstream>
#include "xxhash.h"

using namespace facebook;

namespace xxhash {
enum class HashSize {
BITS_64,
BITS_128,
};

template <HashSize T>
inline void make_hash(const std::string_view str, std::stringstream& ss) noexcept;

template <>
inline void make_hash<HashSize::BITS_64>(const std::string_view str,
std::stringstream& ss) noexcept {
XXH64_hash_t hash = XXH3_64bits(str.data(), str.size());

ss << std::hex << std::setfill('0') << std::setw(16) << hash;
};

template <>
inline void make_hash<HashSize::BITS_128>(const std::string_view str,
std::stringstream& ss) noexcept {
XXH128_hash_t hash = XXH3_128bits(str.data(), str.size());

ss << std::hex << std::setfill('0') << std::setw(16) << hash.high64
<< std::setw(16) << hash.low64;
};

void install(jsi::Runtime* rt);
}

inline void make_hash_64(const std::string_view str, char result[17]) noexcept {
XXH64_hash_t hash = XXH3_64bits(str.data(), str.size());
std::snprintf(result, 17, "%016llx", hash);
};

inline void make_hash_128(const std::string_view str,
char result[33]) noexcept {
XXH128_hash_t hash = XXH3_128bits(str.data(), str.size());
std::snprintf(result, 33, "%016llx%016llx", hash.high64, hash.low64);
};

void install(jsi::Runtime* rt);
} // namespace xxhash

#endif /* XXHASH_H */

0 comments on commit 24f1020

Please sign in to comment.