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
This commit is contained in:
Bastien Montagne
2025-09-22 16:40:38 +02:00
committed by Bastien Montagne
parent f8d579d153
commit a6d2b21f3e
4 changed files with 10 additions and 11 deletions

View File

@@ -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<ID *>(

View File

@@ -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

View File

@@ -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;

View File

@@ -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();