From a6d2b21f3eb64e812764645a23351b046ff7d760 Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Mon, 22 Sep 2025 16:40:38 +0200 Subject: [PATCH] Refactor: Minor changes to 'non-allocated' ID copying and related Depsgraph code. Mainly: - Move 're-used' ID reset from `BKE_id_copy_in_lib` to lower-level `BKE_libblock_copy_in_lib`. - Remove call to `deg_tag_eval_copy_id` in `IDNode::init_copy_on_write`, as this is already done in `deg_expand_eval_copy_datablock` for all CoW IDs anyway (both re-used and newly allocated ones). This is extracted from !146046 'making ID::runtime an allocated struct'. Pull Request: https://projects.blender.org/blender/blender/pulls/146593 --- source/blender/blenkernel/intern/lib_id.cc | 12 ++++++------ source/blender/depsgraph/intern/depsgraph.cc | 2 +- source/blender/depsgraph/intern/node/deg_node_id.cc | 5 ++--- source/blender/depsgraph/intern/node/deg_node_id.hh | 2 +- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/source/blender/blenkernel/intern/lib_id.cc b/source/blender/blenkernel/intern/lib_id.cc index 5a61359c04f..82f1a2afa96 100644 --- a/source/blender/blenkernel/intern/lib_id.cc +++ b/source/blender/blenkernel/intern/lib_id.cc @@ -680,9 +680,6 @@ ID *BKE_id_copy_in_lib(Main *bmain, /* Invalid case, already caught by the assert above. */ return nullptr; } - /* Allow some garbage non-initialized memory to go in, and clean it up here. */ - const size_t size = BKE_libblock_get_alloc_info(GS(id->name), nullptr); - memset(newid, 0, size); } /* Early output if source is nullptr. */ @@ -1549,13 +1546,16 @@ void BKE_libblock_copy_in_lib(Main *bmain, ((owner_library && *owner_library) ? (ID_TAG_EXTERN | ID_TAG_INDIRECT) : 0); if ((flag & LIB_ID_CREATE_NO_ALLOCATE) != 0) { - /* `new_id_p` already contains pointer to allocated memory. */ - /* TODO: do we want to memset(0) whole mem before filling it? */ + /* `new_id_p` already contains pointer to allocated memory. + * Clear and initialize it similar to BKE_libblock_alloc_in_lib. */ + const size_t size = BKE_libblock_get_alloc_info(GS(id->name), nullptr); + memset(new_id, 0, size); STRNCPY(new_id->name, id->name); new_id->us = 0; new_id->tag |= ID_TAG_NOT_ALLOCATED | ID_TAG_NO_MAIN | ID_TAG_NO_USER_REFCOUNT; new_id->lib = owner_library ? *owner_library : id->lib; - /* TODO: Do we want/need to copy more from ID struct itself? */ + /* TODO: Is this entirely consistent with BKE_libblock_alloc_in_lib, and can we + * deduplicate the initialization code? */ } else { new_id = static_cast( diff --git a/source/blender/depsgraph/intern/depsgraph.cc b/source/blender/depsgraph/intern/depsgraph.cc index c953719c26c..98ca9c85562 100644 --- a/source/blender/depsgraph/intern/depsgraph.cc +++ b/source/blender/depsgraph/intern/depsgraph.cc @@ -110,7 +110,7 @@ IDNode *Depsgraph::add_id_node(ID *id, ID *id_cow_hint) if (!id_node) { DepsNodeFactory *factory = type_get_factory(NodeType::ID_REF); id_node = (IDNode *)factory->create_node(id, "", id->name); - id_node->init_copy_on_write(*this, id_cow_hint); + id_node->init_copy_on_write(id_cow_hint); /* Register node in ID hash. * * NOTE: We address ID nodes by the original ID pointer they are diff --git a/source/blender/depsgraph/intern/node/deg_node_id.cc b/source/blender/depsgraph/intern/node/deg_node_id.cc index 3954e2cfc85..ddcb7c883d7 100644 --- a/source/blender/depsgraph/intern/node/deg_node_id.cc +++ b/source/blender/depsgraph/intern/node/deg_node_id.cc @@ -58,7 +58,7 @@ void IDNode::init(const ID *id, const char * /*subdata*/) previously_visible_components_mask = 0; } -void IDNode::init_copy_on_write(Depsgraph &depsgraph, ID *id_cow_hint) +void IDNode::init_copy_on_write(ID *id_cow_hint) { /* Create pointer as early as possible, so we can use it for function * bindings. Rest of data we'll be copying to the new datablock when @@ -82,8 +82,7 @@ void IDNode::init_copy_on_write(Depsgraph &depsgraph, ID *id_cow_hint) else if (deg_eval_copy_is_needed(id_orig)) { id_cow = BKE_libblock_alloc_notest(GS(id_orig->name)); DEG_COW_PRINT( - "Create shallow copy for %s: id_orig=%p id_cow=%p\n", id_orig->name, id_orig, id_cow); - deg_tag_eval_copy_id(depsgraph, id_cow, id_orig); + "Allocate memory for %s: id_orig=%p id_cow=%p\n", id_orig->name, id_orig, id_cow); } else { id_cow = id_orig; diff --git a/source/blender/depsgraph/intern/node/deg_node_id.hh b/source/blender/depsgraph/intern/node/deg_node_id.hh index ccc93fd7b92..9b7f25c503d 100644 --- a/source/blender/depsgraph/intern/node/deg_node_id.hh +++ b/source/blender/depsgraph/intern/node/deg_node_id.hh @@ -52,7 +52,7 @@ struct IDNode : public Node { /** Initialize 'id' node - from pointer data given. */ void init(const ID *id, const char *subdata) override; - void init_copy_on_write(Depsgraph &depsgraph, ID *id_cow_hint = nullptr); + void init_copy_on_write(ID *id_cow_hint = nullptr); ~IDNode() override; void destroy();