]> git.sesse.net Git - remoteglot-book/blobdiff - binloader.cpp
Speed up binloader by ~10-15%.
[remoteglot-book] / binloader.cpp
index ab3e8aeb71646700bc941546d6df875ebf1dfb64..3877ffcaf827aa87267ff4237cc45796e1490641 100644 (file)
 
 using namespace std;
 
-static int memcmp_different_len(const void *s1, size_t n1, const void *s2, size_t n2)
+static inline int memcmp_different_len(const void *s1, size_t n1, const void *s2, size_t n2)
 {
-       int shared_len = min(n1, n2);
+       size_t shared_len = min(n1, n2);
+       if (shared_len >= 8) {
+               uint64_t a1 = *(const uint64_t *)s1;
+               uint64_t a2 = *(const uint64_t *)s2;
+               if (a1 != a2) {
+                       a1 = __builtin_bswap64(a1);
+                       a2 = __builtin_bswap64(a2);
+                       return (a1 < a2) ? -1 : 1;
+               }
+       }
+
        int s = memcmp(s1, s2, shared_len);
        if (s != 0) {
                return s;
        }
 
-       if (n1 < n2) {
-               return -1;
-       } else if (n1 > n2) {
-               return 1;
-       } else {
-               return 0;
-       }
+       return n2 - n1;
 }
 
 enum Result { WHITE = 0, DRAW, BLACK };
 struct Element {
-       char *bpfen;
+       char *bpfen;  // includes prev_board_hash
        int bpfen_len;
        char move[8];   // Na1xc3+
        Result result;
@@ -104,9 +108,10 @@ void write_subshard(const char *basename, ShardData* shard, int bucket)
                    e.bpfen_len != shard->elems[i + 1].bpfen_len ||
                    memcmp(e.bpfen, shard->elems[i + 1].bpfen, e.bpfen_len) != 0) {
                        c.SerializeToString(&buf);
-                       mtbl_writer_add(mtbl,
+                       mtbl_res res = mtbl_writer_add(mtbl,
                                (const uint8_t *)e.bpfen, e.bpfen_len,
                                (const uint8_t *)buf.data(), buf.size());
+                       assert(res == mtbl_res_success);
                        c = Count();
                        moves.clear();
                }