Files
test/source/blender/blenlib/intern/compute_context.cc
Jacques Lucke 5a449439ef Refactor: BLI: simplify compute context hash generation and make it lazy
Currently, there was a lot of boilerplate to compute the compute context hash.
Now, the complexity is abstracted away to make it a simple function call.

Furthermore, this makes the compute context hash generation lazy. The goal here
is to make it very cheap to construct the compute context hash in the first,
while making it a little bit more expensive (still quite cheap overall) to
access the hash when any data has to be logged. This trade-off makes sense when
we want to pass the compute context to more lower-level places in order to be
able to create better error messages with more contextual information. For
example, we'd want to create error messages during multi-function evaluation
which happens during field evaluation within a node.

Pull Request: https://projects.blender.org/blender/blender/pulls/138912
2025-05-15 21:18:23 +02:00

58 lines
1.5 KiB
C++

/* SPDX-FileCopyrightText: 2023 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup bli
*/
#include "BLI_compute_context.hh"
#include "BLI_stack.hh"
#include <sstream>
#include <xxhash.h>
namespace blender {
ComputeContextHash ComputeContextHash::from_bytes(const void *data, const int64_t len)
{
ComputeContextHash final_hash;
const XXH128_hash_t hash = XXH3_128bits(data, len);
/* Cast to void * to avoid warnings. */
memcpy(static_cast<void *>(&final_hash), &hash, sizeof(hash));
static_assert(sizeof(ComputeContextHash) == sizeof(hash));
return final_hash;
}
std::ostream &operator<<(std::ostream &stream, const ComputeContextHash &hash)
{
std::stringstream ss;
ss << "0x" << std::hex << hash.v1 << hash.v2;
stream << ss.str();
return stream;
}
void ComputeContext::print_stack(std::ostream &stream, StringRef name) const
{
Stack<const ComputeContext *> stack;
for (const ComputeContext *current = this; current; current = current->parent_) {
stack.push(current);
}
stream << "Context Stack: " << name << "\n";
while (!stack.is_empty()) {
const ComputeContext *current = stack.pop();
stream << "-> ";
current->print_current_in_line(stream);
const ComputeContextHash &current_hash = current->hash_;
stream << " \t(hash: " << current_hash << ")\n";
}
}
std::ostream &operator<<(std::ostream &stream, const ComputeContext &compute_context)
{
compute_context.print_stack(stream, "");
return stream;
}
} // namespace blender