This repository has been archived on 2023-10-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
blender-archive/source/blender/blenkernel/intern/compute_contexts.cc
Jacques Lucke a09accb496 Geometry Nodes: speedup compute context hash generation
Whenever a node group is entered during evaluation, a new compute
context is entered which has a corresponding hash. When node groups
are entered and exited a lot, this can have some overhead. In my test
file with ~100.000 node group invocations, this patch improves performance
by about 7%.

The speedup is achieved in two ways:
* Avoid computing the same hash twice by caching it.
* Invoke the hashing algorithm (md5 currently) only once instead of twice.
2022-12-29 20:46:05 +01:00

64 lines
1.9 KiB
C++

/* SPDX-License-Identifier: GPL-2.0-or-later */
#include "DNA_node_types.h"
#include "BKE_compute_contexts.hh"
namespace blender::bke {
ModifierComputeContext::ModifierComputeContext(const ComputeContext *parent,
std::string modifier_name)
: ComputeContext(s_static_type, parent), modifier_name_(std::move(modifier_name))
{
hash_.mix_in(s_static_type, strlen(s_static_type));
hash_.mix_in(modifier_name_.data(), modifier_name_.size());
}
void ModifierComputeContext::print_current_in_line(std::ostream &stream) const
{
stream << "Modifier: " << modifier_name_;
}
NodeGroupComputeContext::NodeGroupComputeContext(
const ComputeContext *parent,
const int node_id,
const std::optional<ComputeContextHash> &cached_hash)
: ComputeContext(s_static_type, parent), node_id_(node_id)
{
if (cached_hash.has_value()) {
hash_ = *cached_hash;
}
else {
/* Mix static type and node id into a single buffer so that only a single call to #mix_in is
* necessary. */
const int type_size = strlen(s_static_type);
const int buffer_size = type_size + 1 + sizeof(int32_t);
DynamicStackBuffer<64, 8> buffer_owner(buffer_size, 8);
char *buffer = static_cast<char *>(buffer_owner.buffer());
memcpy(buffer, s_static_type, type_size + 1);
memcpy(buffer + type_size + 1, &node_id_, sizeof(int32_t));
hash_.mix_in(buffer, buffer_size);
}
}
NodeGroupComputeContext::NodeGroupComputeContext(const ComputeContext *parent, const bNode &node)
: NodeGroupComputeContext(parent, node.identifier)
{
#ifdef DEBUG
debug_node_name_ = node.name;
#endif
}
void NodeGroupComputeContext::print_current_in_line(std::ostream &stream) const
{
#ifdef DEBUG
if (!debug_node_name_.empty()) {
stream << "Node: " << debug_node_name_;
return;
}
#endif
stream << "Node ID: " << node_id_;
}
} // namespace blender::bke