Skip to content

Commit 24f1020

Browse files
author
pioner921227
committed
optimized hash fn
1 parent 3cd2109 commit 24f1020

File tree

2 files changed

+19
-35
lines changed

2 files changed

+19
-35
lines changed

cpp/react-native-xxhash.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ void xxhash::install(jsi::Runtime* rt_ptr) {
88
[](jsi::Runtime& rt, const jsi::Value& thisVal, const jsi::Value* args,
99
size_t count) {
1010
const jsi::Value& arg = args[0];
11-
11+
1212
if (!arg.isString()) [[unlikely]] {
1313
throw jsi::JSError(rt, "Argument is not a 'string'");
1414
}
1515

16-
std::stringstream ss;
16+
char result[33];
1717

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

20-
return jsi::String::createFromUtf8(rt, ss.str());
20+
return jsi::String::createFromUtf8(rt, result);
2121
});
2222

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

33-
std::stringstream ss;
33+
char result[17];
3434

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

37-
return jsi::String::createFromUtf8(rt, ss.str());
37+
return jsi::String::createFromUtf8(rt, result);
3838
});
3939

4040
runtime.global().setProperty(runtime, "__xxhash128", std::move(hash128));

cpp/react-native-xxhash.h

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,26 @@
22
#ifndef XXHASH_H
33
#define XXHASH_H
44
#include <jsi/jsi.h>
5-
#include "xxhash.h"
65
#include <iomanip>
76
#include <sstream>
7+
#include "xxhash.h"
88

99
using namespace facebook;
1010

1111
namespace xxhash {
12-
enum class HashSize {
13-
BITS_64,
14-
BITS_128,
15-
};
16-
17-
template <HashSize T>
18-
inline void make_hash(const std::string_view str, std::stringstream& ss) noexcept;
19-
20-
template <>
21-
inline void make_hash<HashSize::BITS_64>(const std::string_view str,
22-
std::stringstream& ss) noexcept {
23-
XXH64_hash_t hash = XXH3_64bits(str.data(), str.size());
24-
25-
ss << std::hex << std::setfill('0') << std::setw(16) << hash;
26-
};
27-
28-
template <>
29-
inline void make_hash<HashSize::BITS_128>(const std::string_view str,
30-
std::stringstream& ss) noexcept {
31-
XXH128_hash_t hash = XXH3_128bits(str.data(), str.size());
32-
33-
ss << std::hex << std::setfill('0') << std::setw(16) << hash.high64
34-
<< std::setw(16) << hash.low64;
35-
};
36-
37-
void install(jsi::Runtime* rt);
38-
}
3912

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

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

24+
void install(jsi::Runtime* rt);
25+
} // namespace xxhash
4226

4327
#endif /* XXHASH_H */

0 commit comments

Comments
 (0)