Anim: Graph Editor - use Map to update TransInfo pointers instead of searching #120816
No reviewers
Labels
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
4 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender#120816
Loading…
Reference in New Issue
No description provided.
Delete Branch "ChrisLend/blender:optimize_beztmap_to_data"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This is a performance improvement when moving a bunch of keys on the same
FCurve
in heavy scenes.When moving keys in such a way that the
BezTriple
array of the FCurve has to be sorted,the pointers of
TransInfo
also have to be updated (deeper explanation below).This used to happen by doing a nested loop over all
BeztMap
and allTransData2D
.There was a bit of optimization with the
blender::Vector<bool> adjusted
which stored if aTransData2D
hasbeen fixed yet. But in general the complexity was still
BeztMap.size() * TransData.size()
.There are two optimizations that can be done here.
old_index == new_index
. If the Key is not going to move any pointers to it will still be valid.Map<float *, int>
built beforehand to quickly get theTransData2D
that needs updating instead of searching. Theint
in this case is the index to theTransData2D
array.Doing this reduces the complexity to
BeztMap.size() + TransData.size()
. (if I am not mistaken)Measurements of
beztmap_to_data
Measurements of
remake_graph_transdata
Test file used
https://download.blender.org/ftp/sybren/animation-rigging/heavy_mocap_test.blend
The deeper technical explanation.
TransInfo
has an array ofTransData
.TransData
has pointers to the float arrays of aBezTriple
.The
BezTriple
array is sorted by swapping data, meaning theTransData
will now point to the wrong data in the array.This has to be updated and we can do that by using the
BeztMap
. This is all under the assumption thatBeztMap
is sorted in the exact same way asBezTriple
otherwise this method will fail.But by doing it the same way, we can know at which index the
BezTriple
is before and after sorting.Now we just need to find the corresponding
TransData
. That can be done by comparing pointers. TheBeztMap
stores theBezTriple
it represents and from it we can get the pointers to itsvec
0, 1 and 2. (key and handles)@ -809,3 +823,1 @@
/* Used to mark whether an TransData's pointers have been fixed already, so that we don't
* override ones that are already done. */
blender::Vector<bool> adjusted(tc->data_len, false);
blender::Map<float *, int> trans_info_map;
Not sure if this is correct to use
float *
key for array of floats.tc->data_2d[i].loc2d
is a float pointer and thei
is what i need to find again later.Yeah, sorry, i just get deep in to that...
While testing, I immediately ran into an assert when trying to move keys in the linked heavy mocap test file:
I reliably run into this every time.
(Although if I comment out the assert, everything seems to work fine. Nevertheless, this suggests there's a bug somewhere, whether it be in this PR or in the BLI_map code.)
Probably this is the thing that i wrote about, but not sure\
thanks for catching that, not quite sure why that happens though. It doesn't happen right away for me though which is even weirder
This is a huge speedup, congrats!
@ -803,0 +801,4 @@
static inline void update_trans_data(TransData *td,
const FCurve *fcu,
const int new_index,
const int swap_handles)
Since
swap_handles
comes fromBeztMap::swap_handles
, it should be ashort
. Then again, since this function only deals with the1
and "not1
" values, maybe this should beconst bool do_swap_handles
and then the call site can putswap_handles == 1
in there? Or have aBLI_assert(swap_handles >= 0)
or something along those lines. To me it's too unclear, at this function, what the possible values forswap_handles
would be.But also see my note about this further down.
For another PR: it would be good to get rid of that
short
type altogether, and just use a 3-valuedenum
for instead.@ -803,3 +805,2 @@
{
TransData2D *td2d;
TransData *td;
if (td->flag & TD_BEZTRIPLE && td->hdata) {
In which case would
td->flag & TD_BEZTRIPLE
be true andtd->hdata
false? Is that a valid, expected case? If not, then I think something like this would be better:I don't know honestly. I copied this code as is from
beztmap_to_data
. Ideally I'd like to avoid changing it with this PR for fear of introducing bugs and keeping the PR scope narrow.@ -806,2 +816,3 @@
}
TransDataContainer *tc = TRANS_DATA_CONTAINER_FIRST_SINGLE(t);
/* This function firstly adjusts the pointers that the transdata has to each BezTriple. */
"firstly" implies a "secondly", but that seems to be missing. Also the "this function ..." can be removed, and shortened to something like "Adjust the pointers that ..."
@ -808,0 +824,4 @@
/* At this point, beztmaps are already sorted, so their current index is assumed to be what the
* BezTriple index will be after sorting. */
for (const int new_index : bezms.index_range()) {
const BeztMap *bezm = &bezms[new_index];
Does this have to be a pointer? Or could it be a reference?
@ -808,0 +827,4 @@
const BeztMap *bezm = &bezms[new_index];
if (new_index == bezm->oldIndex) {
/* If the index is the same,
* any pointers to BezTriple will still point to the correct data. */
This comment can be rewrapped.
@ -809,3 +834,1 @@
/* Used to mark whether an TransData's pointers have been fixed already, so that we don't
* override ones that are already done. */
blender::Vector<bool> adjusted(tc->data_len, false);
/* For the handles (vec[0]/vec[2]), we must also check if they need to be swapped. */
Isn't this also necessary when the bezm isn't moved index-wise?
hmmmm maybe.
Since I am not sure I will let any BeztMap pass where
swap_handles
is set.Given that just means 3 more Map lookups and
swap_handles
is rarely set this shouldn't be a performance issue@ -825,0 +838,4 @@
if (trans_data_map.contains(bezm->bezt->vec[0])) {
const int trans_data_index = trans_data_map.lookup(bezm->bezt->vec[0]);
td2d = &tc->data_2d[trans_data_index];
if (bezm->swap_handles == 1) {
Since this is effectively going over the values of an enum, I think a
switch
with threecase
s might be a nice approach here.It then becomes much more explicit that the
0
and-1
cases are handled equally. Or maybe they do get a separatecase
with aBLI_assert_unreachable()
in the-1
one?Seconded.
given that swap handles is a bool after #121076: Refactor: swap handle logic in Graph Editor transform code I think this will stay an
if
@ -846,0 +847,4 @@
td = &tc->data[trans_data_index];
update_trans_data(td, fcu, new_index, bezm->swap_handles);
}
if (trans_data_map.contains(bezm->bezt->vec[2])) {
This seems to be a copy of the above code. Probably better to extract that into a small function that takes the index into
vec
, and when swapping is necessary, uses2 - that_vec_index
.I think I agree, but with the following caveats:
So I'd suggest making it a lambda defined just above its usage inside this function. That would make it clear that it's only called here, and would alleviate the importance of the naming issue (since you immediately see the context where it's used). And as a bonus, you can see all the relevant code for what this function does in one place, without having to jump around the file.
that's how that would look as a lambda. IMO this is less readable than having a bit of code duplication.
I agree. Thanks @ChrisLend for checking this out, but it indeed isn't as nice as it was in my head ;-)
@ -885,0 +891,4 @@
/* Build a map from the data that is being modified to its index. This is used to quickly update
* the pointers to where the data ends up after sorting. */
blender::Map<float *, int> trans_data_map;
for (int i = 0; i < tc->data_len; i++) {
It might be nice to extract this into a
data_to_beztmap()
function, and have that defined close to its counterpartbeztmap_to_data()
.I'm not sure I agree.
beztmap_to_data()
being split out makes sense to me because of the size of its code, so splitting it out makes the over-all flow of the code where it's called easier to see. But conceptually it's actually part of this function: it's only called from one place and only in this function, and it's highly specific to its call site. If it were significantly smaller, I would actually prefer it to be inlined.This code has the same single-call-site specificity of
beztmap_to_data()
, but without the size to justify splitting it out. IMO splitting this out into a separate function would just force me to jump to another part of the file to see what's going on, without much benefit.also not sure about that.
beztmap_to_data
just uses the map andBeztMap
to update the pointerswhile this loop creates the map in the first place (without using a
BeztMap
).imo that isn't symmetrical like
data_to_beztmap
suggestsFair enough, never mind.
I agree with most of Sybren's nits, but otherwise your changes look good to me!
As an aside, the over-all structure of the code in this area (nothing to do with your PR) feels unfortunate to me:
It feels both fragile and inefficient.
In any case, I don't think it's worth doing a larger refactor to change that. But I wanted to vent about it a bit anyway, ha ha.
@ -806,2 +816,3 @@
}
TransDataContainer *tc = TRANS_DATA_CONTAINER_FIRST_SINGLE(t);
/* This function firstly adjusts the pointers that the transdata has to each BezTriple. */
I had the same comment here as @dr.sybren, but he got to it before I finished my review. ;-) "Firstly" feels like the description is incomplete.
Something else that occurred to me: it might be worth taking this opportunity to rename
beztmap_to_data()
. The current name IMO doesn't reflect what it does at all. It sounds like it's creating completely new data from the beztmap, when in fact it's just updating the bezt pointers in the existing transform data. And the beztmap itself is almost incidental (it's just the means by which the actual bezt pointers get updated), rather than the purpose of the function.Maybe something along the lines of
update_transdata_bezt_pointers()
would be better.@nathanvegdahl I 100% agree. Should that be a refactor PR or is it ok to do that in this one?
I guess technically it should be a separate refactor PR. But I don't feel strongly about it.
landed in a separate PR and merged
🚀
@ -809,3 +822,1 @@
/* Skip item if already marked. */
if (adjusted[j]) {
continue;
if (trans_data_map.contains(bezm.bezt->vec[0])) {
To avoid
contains
andlookup
for the same key:Aside from @mod_moder 's comment (good catch!) about avoiding redundant
contains()
+lookup()
, looks good to me!@blender-bot build