From 08de3fa0b67ef862455fbce166b997ab0eee35e4 Mon Sep 17 00:00:00 2001 From: Christoph Lendenfeld Date: Tue, 30 Apr 2024 15:14:49 +0200 Subject: [PATCH] Anim: extract constant variables out of sorting loop No functional changes expected. This PR extracts the `IndexRange` of the `BeztMap` span and its `size()` call out of the sorting loop. Doing this removes one `if` condition within the loop and gives a small performance boost. (Before: 0.93ms, After: 0.65ms average run of `sort_time_beztmaps`) Pull Request: https://projects.blender.org/blender/blender/pulls/121267 --- .../transform/transform_convert_graph.cc | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/source/blender/editors/transform/transform_convert_graph.cc b/source/blender/editors/transform/transform_convert_graph.cc index edba4b85dc4..5e4cd726deb 100644 --- a/source/blender/editors/transform/transform_convert_graph.cc +++ b/source/blender/editors/transform/transform_convert_graph.cc @@ -755,8 +755,6 @@ static blender::Vector bezt_to_beztmaps(BezTriple *bezts, const int tot /* This function copies the code of sort_time_ipocurve, but acts on BeztMap structs instead. */ static void sort_time_beztmaps(const blender::MutableSpan bezms) { - BeztMap *bezm; - /* Check if handles need to be swapped. */ for (BeztMap &bezm : bezms) { /* Handles are only swapped if they are both on the wrong side of the key. Otherwise the one @@ -766,18 +764,22 @@ static void sort_time_beztmaps(const blender::MutableSpan bezms) } bool ok = true; + const int bezms_size = bezms.size(); + if (bezms_size < 2) { + /* No sorting is needed with only 0 or 1 entries. */ + return; + } + const blender::IndexRange bezm_range = bezms.index_range().drop_back(1); + /* Keep repeating the process until nothing is out of place anymore. */ while (ok) { ok = false; - - for (const int i : bezms.index_range()) { - bezm = &bezms[i]; + for (const int i : bezm_range) { + BeztMap *bezm = &bezms[i]; /* Is current bezm out of order (i.e. occurs later than next)? */ - if (i < bezms.size() - 1) { - if (bezm->bezt->vec[1][0] > (bezm + 1)->bezt->vec[1][0]) { - std::swap(*bezm, *(bezm + 1)); - ok = true; - } + if (bezm->bezt->vec[1][0] > (bezm + 1)->bezt->vec[1][0]) { + std::swap(*bezm, *(bezm + 1)); + ok = true; } } }