WIP: Attributes: avoid unnecessary data copies by generalizing attribute unshare policy #119588

Draft
Jacques Lucke wants to merge 4 commits from JacquesLucke/blender:attribute-unshare into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
Member

Currently, we sometimes do unnecessary data copies when getting write-access to an attributes. If the attribute was implicitly shared, it has to be copied internally before write-access can be provided. However, if the caller just overwrites all of the values without looking at the old values, the copy was unnecessary. A slightly more complex case is where the caller overwrites most, but not all values. In this case, it should only be necessary to copy a few old attribute values instead of all.

This patch solves a similar problem to #117179 but in a more general way. In some existing code we use *_for_write_only methods to indicate that the old values are not needed anymore. Both approaches don't support the more complex case where the attribute is partially overwritten.

This patch adds a new concept called "unshare policy". It is a function that makes a mutable version of shared data. The mutable version may be a full copy of the original data, a partial copy, or just uninitialized memory.

There are two related but separate unshare policy base types currently. One is specific to CustomData and one is more generally for arrays. This separation feels reasonable, because we want to use these unshare policies outside independent of CustomData. However, CustomData also has some less trivial storage formats (like for vertex groups) that can't easily be generalized currently.


The patch is not quite ready yet. It's still missing some documentation and open questions have to be resolved.

Open Questions:

  • Should we keep the *_for_write_only methods? Technically, they are redundant now but they seem to make the API usage more concise. Maybe it could be renamed to *_for_init_all to make the behavior more clear.
Currently, we sometimes do unnecessary data copies when getting write-access to an attributes. If the attribute was implicitly shared, it has to be copied internally before write-access can be provided. However, if the caller just overwrites all of the values without looking at the old values, the copy was unnecessary. A slightly more complex case is where the caller overwrites most, but not all values. In this case, it should only be necessary to copy a few old attribute values instead of all. This patch solves a similar problem to #117179 but in a more general way. In some existing code we use `*_for_write_only` methods to indicate that the old values are not needed anymore. Both approaches don't support the more complex case where the attribute is partially overwritten. This patch adds a new concept called "unshare policy". It is a function that makes a mutable version of shared data. The mutable version may be a full copy of the original data, a partial copy, or just uninitialized memory. There are two related but separate unshare policy base types currently. One is specific to `CustomData` and one is more generally for arrays. This separation feels reasonable, because we want to use these unshare policies outside independent of `CustomData`. However, `CustomData` also has some less trivial storage formats (like for vertex groups) that can't easily be generalized currently. ----- The patch is not quite ready yet. It's still missing some documentation and open questions have to be resolved. Open Questions: * Should we keep the `*_for_write_only` methods? Technically, they are redundant now but they seem to make the API usage more concise. Maybe it could be renamed to `*_for_init_all` to make the behavior more clear.
Jacques Lucke added 4 commits 2024-03-17 11:46:38 +01:00
Jacques Lucke changed title from Attributes: avoid unnecessary data copies by generalizing attribute unshare policy to WIP: Attributes: avoid unnecessary data copies by generalizing attribute unshare policy 2024-03-17 11:52:48 +01:00
Jacques Lucke requested review from Hans Goudey 2024-03-17 12:06:04 +01:00

General solution for #117910 cases?

General solution for https://projects.blender.org/blender/blender/pulls/117910 cases?
Author
Member

Not quite. In your case you still need access to the old array.

Not quite. In your case you still need access to the old array.
This pull request has changes conflicting with the target branch.
  • source/blender/blenkernel/intern/attribute_access.cc

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u attribute-unshare:JacquesLucke-attribute-unshare
git checkout JacquesLucke-attribute-unshare
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#119588
No description provided.