Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Use bit twiddling rather than union type punning for encoding int64.

Kishore Nallanc27580a419fe
diff --git a/TODO.md b/TODO.md
index a18b2448..a4dc7486 100644
--- a/TODO.md
+++ b/TODO.md
@@ -91,7 +91,7 @@
- ~~Proper logging~~
- ~~Handle store-get() not finding a key~~
- ~~Deprecate converting integer to string verbatim~~
-- Deprecate union type punning
+- ~~Deprecate union type punning~~
- NOT operator support
- > INT32_MAX validation for float field
- highlight of string arrays?
diff --git a/src/art.cpp b/src/art.cpp
index d391ab8d..2f099704 100644
--- a/src/art.cpp
+++ b/src/art.cpp
@@ -1401,16 +1401,14 @@ void encode_int32(int32_t n, unsigned char *chars) {
}
void encode_int64(int64_t n, unsigned char *chars) {
- union {
- int64_t l;
- unsigned char bytes[8];
- } container;
-
- container.l = n;
-
- for(uint32_t i = 0; i < 8; i++) {
- chars[7-i] = container.bytes[i];
- }
+ chars[0] = (unsigned char) ((n >> 56) & 0xFF);
+ chars[1] = (unsigned char) ((n >> 48) & 0xFF);
+ chars[2] = (unsigned char) ((n >> 40) & 0xFF);
+ chars[3] = (unsigned char) ((n >> 32) & 0xFF);
+ chars[4] = (unsigned char) ((n >> 24) & 0xFF);
+ chars[5] = (unsigned char) ((n >> 16) & 0xFF);
+ chars[6] = (unsigned char) ((n >> 8) & 0xFF);
+ chars[7] = (unsigned char) (n & 0xFF);
}
// See: https://github.com/apache/hbase/blob/master/hbase-common/src/main/java/org/apache/hadoop/hbase/util/OrderedBytes.java#L1372