Blender V5.0
profiler.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_timeit.hh"
6
7#include "DNA_node_types.h"
8
9#include "BKE_node.hh"
10
12
13#include "COM_profiler.hh"
14
15namespace blender::compositor {
16
21
24{
25 nodes_evaluation_times_.lookup_or_add(node_instance_key, timeit::Nanoseconds::zero()) += time;
26}
27
28timeit::Nanoseconds Profiler::accumulate_node_group_times(const bNodeTree &node_tree,
29 bNodeInstanceKey instance_key)
30{
31 timeit::Nanoseconds tree_evaluation_time = timeit::Nanoseconds::zero();
32
33 for (const bNode *node : node_tree.all_nodes()) {
34 const bNodeInstanceKey node_instance_key = bke::node_instance_key(
35 instance_key, &node_tree, node);
36 if (!node->is_group()) {
37 /* Non-group node, no need to recurse into. Simply accumulate the node's evaluation time to
38 * the current tree's evaluation time. Note that not every node might have an evaluation
39 * time stored, so default to zero. See the documentation on nodes_evaluation_times_ for more
40 * information. */
41 tree_evaluation_time += nodes_evaluation_times_.lookup_default(node_instance_key,
42 timeit::Nanoseconds::zero());
43 continue;
44 }
45
46 const bNodeTree *child_tree = reinterpret_cast<bNodeTree *>(node->id);
47 if (child_tree == nullptr) {
48 /* Node group has lost link to its node tree. For example, due to missing linked file. */
49 continue;
50 }
51
52 const timeit::Nanoseconds group_execution_time = this->accumulate_node_group_times(
53 *child_tree, node_instance_key);
54
55 /* Set evaluation time of the group node. */
56 this->set_node_evaluation_time(node_instance_key, group_execution_time);
57
58 /* Add group evaluation time to the overall tree execution time. */
59 tree_evaluation_time += group_execution_time;
60 }
61
62 return tree_evaluation_time;
63}
64
65void Profiler::finalize(const bNodeTree &node_tree)
66{
67 /* Compute the evaluation time of all node groups starting from the root tree. */
68 this->accumulate_node_group_times(node_tree, bke::NODE_INSTANCE_KEY_BASE);
69}
70
71} // namespace blender::compositor
Value lookup_default(const Key &key, const Value &default_value) const
Definition BLI_map.hh:570
Value & lookup_or_add(const Key &key, const Value &value)
Definition BLI_map.hh:588
void finalize(const bNodeTree &node_tree)
Definition profiler.cc:65
void set_node_evaluation_time(bNodeInstanceKey node_instance_key, timeit::Nanoseconds time)
Definition profiler.cc:22
Map< bNodeInstanceKey, timeit::Nanoseconds > & get_nodes_evaluation_times()
Definition profiler.cc:17
const bNodeInstanceKey NODE_INSTANCE_KEY_BASE
Definition node.cc:4846
bNodeInstanceKey node_instance_key(bNodeInstanceKey parent_key, const bNodeTree *ntree, const bNode *node)
Definition node.cc:4867
std::chrono::nanoseconds Nanoseconds
Definition BLI_timeit.hh:21