Merge "ui: significantly improve loading query performance on large traces" into main
diff --git a/BUILD b/BUILD
index c796a9f..7f7ff58 100644
--- a/BUILD
+++ b/BUILD
@@ -931,9 +931,9 @@
         "include/perfetto/tracing/interceptor.h",
         "include/perfetto/tracing/internal/basic_types.h",
         "include/perfetto/tracing/internal/checked_scope.h",
-        "include/perfetto/tracing/internal/compile_time_hash.h",
         "include/perfetto/tracing/internal/data_source_internal.h",
         "include/perfetto/tracing/internal/data_source_type.h",
+        "include/perfetto/tracing/internal/fnv1a.h",
         "include/perfetto/tracing/internal/in_process_tracing_backend.h",
         "include/perfetto/tracing/internal/interceptor_trace_writer.h",
         "include/perfetto/tracing/internal/system_tracing_backend.h",
diff --git a/include/perfetto/tracing/BUILD.gn b/include/perfetto/tracing/BUILD.gn
index 3e0e9f2..0fe022b 100644
--- a/include/perfetto/tracing/BUILD.gn
+++ b/include/perfetto/tracing/BUILD.gn
@@ -38,9 +38,9 @@
     "interceptor.h",
     "internal/basic_types.h",
     "internal/checked_scope.h",
-    "internal/compile_time_hash.h",
     "internal/data_source_internal.h",
     "internal/data_source_type.h",
+    "internal/fnv1a.h",
     "internal/in_process_tracing_backend.h",
     "internal/interceptor_trace_writer.h",
     "internal/system_tracing_backend.h",
diff --git a/include/perfetto/tracing/internal/compile_time_hash.h b/include/perfetto/tracing/internal/compile_time_hash.h
deleted file mode 100644
index da6b081..0000000
--- a/include/perfetto/tracing/internal/compile_time_hash.h
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * Copyright (C) 2021 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef INCLUDE_PERFETTO_TRACING_INTERNAL_COMPILE_TIME_HASH_H_
-#define INCLUDE_PERFETTO_TRACING_INTERNAL_COMPILE_TIME_HASH_H_
-
-#include <stddef.h>
-#include <stdint.h>
-
-namespace perfetto {
-namespace internal {
-
-// A helper class which computes a 64-bit hash of the input data at compile
-// time. The algorithm used is FNV-1a as it is fast and easy to implement and
-// has relatively few collisions.
-// WARNING: This hash function should not be used for any cryptographic purpose.
-class CompileTimeHash {
- public:
-  // Creates an empty hash object
-  constexpr inline CompileTimeHash() {}
-
-  // Hashes a byte array.
-  constexpr inline CompileTimeHash Update(const char* data, size_t size) const {
-    return CompileTimeHash(HashRecursively(kFnv1a64OffsetBasis, data, size));
-  }
-
-  constexpr inline uint64_t digest() const { return result_; }
-
- private:
-  constexpr inline CompileTimeHash(uint64_t result) : result_(result) {}
-
-  static constexpr inline uint64_t HashRecursively(uint64_t value,
-                                                   const char* data,
-                                                   size_t size) {
-    return !size ? value
-                 : HashRecursively(
-                       (value ^ static_cast<uint8_t>(*data)) * kFnv1a64Prime,
-                       data + 1, size - 1);
-  }
-
-  static constexpr uint64_t kFnv1a64OffsetBasis = 0xcbf29ce484222325;
-  static constexpr uint64_t kFnv1a64Prime = 0x100000001b3;
-
-  uint64_t result_ = kFnv1a64OffsetBasis;
-};
-
-}  // namespace internal
-}  // namespace perfetto
-
-#endif  // INCLUDE_PERFETTO_TRACING_INTERNAL_COMPILE_TIME_HASH_H_
diff --git a/include/perfetto/tracing/internal/fnv1a.h b/include/perfetto/tracing/internal/fnv1a.h
new file mode 100644
index 0000000..1f2daae
--- /dev/null
+++ b/include/perfetto/tracing/internal/fnv1a.h
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef INCLUDE_PERFETTO_TRACING_INTERNAL_FNV1A_H_
+#define INCLUDE_PERFETTO_TRACING_INTERNAL_FNV1A_H_
+
+#include <cstddef>
+#include <cstdint>
+
+namespace perfetto {
+namespace internal {
+
+// Constexpr functions to compute a 64-bit hash of the input data. The algorithm
+// used is FNV-1a as it is fast and easy to implement and has relatively few
+// collisions.
+//
+// WARNING: This hash function should not be used for any cryptographic purpose.
+
+static constexpr uint64_t kFnv1a64OffsetBasis = 0xcbf29ce484222325;
+static constexpr uint64_t kFnv1a64Prime = 0x100000001b3;
+
+static constexpr inline uint64_t Fnv1a(const char* s) {
+  uint64_t ret = kFnv1a64OffsetBasis;
+  for (; *s; s++) {
+    ret = ret ^ static_cast<uint8_t>(*s);
+    ret *= kFnv1a64Prime;
+  }
+  return ret;
+}
+
+static constexpr inline uint64_t Fnv1a(const void* data, size_t size) {
+  uint64_t ret = kFnv1a64OffsetBasis;
+  const uint8_t* s = static_cast<const uint8_t*>(data);
+  for (size_t i = 0; i < size; i++) {
+    ret = ret ^ s[i];
+    ret *= kFnv1a64Prime;
+  }
+  return ret;
+}
+
+}  // namespace internal
+}  // namespace perfetto
+
+#endif  // INCLUDE_PERFETTO_TRACING_INTERNAL_FNV1A_H_
diff --git a/include/perfetto/tracing/track.h b/include/perfetto/tracing/track.h
index 1a6e4d0..0d5a364 100644
--- a/include/perfetto/tracing/track.h
+++ b/include/perfetto/tracing/track.h
@@ -22,7 +22,7 @@
 #include "perfetto/base/thread_utils.h"
 #include "perfetto/protozero/message_handle.h"
 #include "perfetto/protozero/scattered_heap_buffer.h"
-#include "perfetto/tracing/internal/compile_time_hash.h"
+#include "perfetto/tracing/internal/fnv1a.h"
 #include "perfetto/tracing/internal/tracing_muxer.h"
 #include "perfetto/tracing/platform.h"
 #include "protos/perfetto/trace/trace_packet.pbzero.h"
@@ -140,9 +140,7 @@
   static Track MakeProcessTrack() { return Track(process_uuid, Track()); }
 
   static constexpr inline uint64_t CompileTimeHash(const char* string) {
-    return internal::CompileTimeHash()
-        .Update(string, static_cast<size_t>(base::StrEnd(string) - string))
-        .digest();
+    return internal::Fnv1a(string);
   }
 
  private:
@@ -209,7 +207,7 @@
   // |name| must be a string with static lifetime.
   constexpr explicit CounterTrack(const char* name,
                                   Track parent = MakeProcessTrack())
-      : Track(CompileTimeHash(name) ^ kCounterMagic, parent),
+      : Track(internal::Fnv1a(name) ^ kCounterMagic, parent),
         name_(name),
         category_(nullptr) {}
 
@@ -218,7 +216,7 @@
   constexpr CounterTrack(const char* name,
                          const char* unit_name,
                          Track parent = MakeProcessTrack())
-      : Track(CompileTimeHash(name) ^ kCounterMagic, parent),
+      : Track(internal::Fnv1a(name) ^ kCounterMagic, parent),
         name_(name),
         category_(nullptr),
         unit_name_(unit_name) {}
@@ -226,7 +224,7 @@
   constexpr CounterTrack(const char* name,
                          Unit unit,
                          Track parent = MakeProcessTrack())
-      : Track(CompileTimeHash(name) ^ kCounterMagic, parent),
+      : Track(internal::Fnv1a(name) ^ kCounterMagic, parent),
         name_(name),
         category_(nullptr),
         unit_(unit) {}