Commit 0d7d371c authored by Daniel Cheng's avatar Daniel Cheng Committed by Commit Bot

Use base::FastHash() in //components/viz

base::Hash() is deprecated and is being replaced by base::FastHash().
The output of base::FastHash() is only fixed within the lifetime of a
single process, so if the code depends on the mapping of inputs to hash
outputs not changing over time, base::PersistentHash() must be used
instead.

Notes:
- Add a base::PersistentHash() overload that takes a base::span to
  match base::FastHash().
- Fixes tracing ID generation to use base::PersistentHash(), since
  the tracing IDs are not always generated in the same process.

Bug: 1025358, 1026837
Change-Id: Ibdfe5185c0a8fdb9ab2fd2ee7408ea73e842801b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1923499
Auto-Submit: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarSadrul Chowdhury <sadrul@chromium.org>
Reviewed-by: default avatarAlex Moshchuk <alexmos@chromium.org>
Reviewed-by: default avatardanakj <danakj@chromium.org>
Reviewed-by: default avatarenne <enne@chromium.org>
Commit-Queue: Alex Moshchuk <alexmos@chromium.org>
Cr-Commit-Position: refs/heads/master@{#718400}
parent a5741729
...@@ -125,22 +125,26 @@ uint32_t Hash(const void* data, size_t length) { ...@@ -125,22 +125,26 @@ uint32_t Hash(const void* data, size_t length) {
} }
uint32_t Hash(const std::string& str) { uint32_t Hash(const std::string& str) {
return PersistentHash(str.data(), str.size()); return PersistentHash(as_bytes(make_span(str)));
} }
uint32_t Hash(const string16& str) { uint32_t Hash(const string16& str) {
return PersistentHash(str.data(), str.size() * sizeof(char16)); return PersistentHash(as_bytes(make_span(str)));
} }
uint32_t PersistentHash(const void* data, size_t length) { uint32_t PersistentHash(span<const uint8_t> data) {
// This hash function must not change, since it is designed to be persistable // This hash function must not change, since it is designed to be persistable
// to disk. // to disk.
if (length > static_cast<size_t>(std::numeric_limits<int>::max())) { if (data.size() > static_cast<size_t>(std::numeric_limits<int>::max())) {
NOTREACHED(); NOTREACHED();
return 0; return 0;
} }
return ::SuperFastHash(reinterpret_cast<const char*>(data), return ::SuperFastHash(reinterpret_cast<const char*>(data.data()),
static_cast<int>(length)); static_cast<int>(data.size()));
}
uint32_t PersistentHash(const void* data, size_t length) {
return PersistentHash(make_span(static_cast<const uint8_t*>(data), length));
} }
uint32_t PersistentHash(const std::string& str) { uint32_t PersistentHash(const std::string& str) {
......
...@@ -48,6 +48,7 @@ inline size_t FastHash(StringPiece str) { ...@@ -48,6 +48,7 @@ inline size_t FastHash(StringPiece str) {
// new version will have to be added in addition. // new version will have to be added in addition.
// //
// WARNING: This hash function should not be used for any cryptographic purpose. // WARNING: This hash function should not be used for any cryptographic purpose.
BASE_EXPORT uint32_t PersistentHash(base::span<const uint8_t> data);
BASE_EXPORT uint32_t PersistentHash(const void* data, size_t length); BASE_EXPORT uint32_t PersistentHash(const void* data, size_t length);
BASE_EXPORT uint32_t PersistentHash(const std::string& str); BASE_EXPORT uint32_t PersistentHash(const std::string& str);
......
...@@ -21,7 +21,7 @@ using SharedBitmapId = gpu::Mailbox; ...@@ -21,7 +21,7 @@ using SharedBitmapId = gpu::Mailbox;
struct SharedBitmapIdHash { struct SharedBitmapIdHash {
size_t operator()(const SharedBitmapId& id) const { size_t operator()(const SharedBitmapId& id) const {
return base::Hash(id.name, sizeof(id.name)); return base::FastHash(base::as_bytes(base::make_span(id.name)));
} }
}; };
......
...@@ -279,7 +279,9 @@ uint64_t HostGpuMemoryBufferManager::ClientIdToTracingId(int client_id) const { ...@@ -279,7 +279,9 @@ uint64_t HostGpuMemoryBufferManager::ClientIdToTracingId(int client_id) const {
// TODO(sad|ssid): Find a better way once https://crbug.com/661257 is // TODO(sad|ssid): Find a better way once https://crbug.com/661257 is
// resolved. The hash value is incremented so that the tracing id is never // resolved. The hash value is incremented so that the tracing id is never
// equal to MemoryDumpManager::kInvalidTracingProcessId. // equal to MemoryDumpManager::kInvalidTracingProcessId.
return static_cast<uint64_t>(base::Hash(&client_id, sizeof(client_id))) + 1; return static_cast<uint64_t>(base::PersistentHash(
base::as_bytes(base::make_span(&client_id, 1)))) +
1;
} }
void HostGpuMemoryBufferManager::OnGpuMemoryBufferAllocated( void HostGpuMemoryBufferManager::OnGpuMemoryBufferAllocated(
......
...@@ -254,7 +254,7 @@ uint64_t ChildProcessHostImpl::ChildProcessUniqueIdToTracingProcessId( ...@@ -254,7 +254,7 @@ uint64_t ChildProcessHostImpl::ChildProcessUniqueIdToTracingProcessId(
// The hash value is incremented so that the tracing id is never equal to // The hash value is incremented so that the tracing id is never equal to
// MemoryDumpManager::kInvalidTracingProcessId. // MemoryDumpManager::kInvalidTracingProcessId.
return static_cast<uint64_t>(base::FastHash( return static_cast<uint64_t>(base::PersistentHash(
base::as_bytes(base::make_span(&child_process_id, 1)))) + base::as_bytes(base::make_span(&child_process_id, 1)))) +
1; 1;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment