Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unneccessary copy and query in animatior.h #407

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

syby119
Copy link

@syby119 syby119 commented Dec 3, 2024

The auto keyword in C++ will drop the reference and top level const in the type deduction.

The following code at animator.h line 57 will cause boneInfoMap be implicitly copied from the animation's member variable.

// animator.h
// boneInfoMap will be deduced to 
// std::map<std::string,BoneInfo> boneInfoMap
auto boneInfoMap = m_CurrentAnimation->GetBoneIDMap();

// animation.h Animation::GetBoneIDMap()
inline const std::map<std::string,BoneInfo>& GetBoneIDMap()  { 
    return m_BoneInfoMap;
}

Since the CalculateBoneTransform is implemented in the recursive fashion, every access to the animation node in the node tree hierarchy will implicitly copy the boneInfoMap, which may cause performance issue.

Besides, we can save the boneInfoMap query result, instead of querying it three times

// original code
auto boneInfoMap = m_CurrentAnimation->GetBoneIDMap();
if (boneInfoMap.find(nodeName) != boneInfoMap.end())  {
    int index = boneInfoMap[nodeName].id;
    glm::mat4 offset = boneInfoMap[nodeName].offset;
    m_FinalBoneMatrices[index] = globalTransformation * offset;
}

// modified code
auto& boneInfoMap = m_CurrentAnimation->GetBoneIDMap();
auto boneInfo = boneInfoMap.find(nodeName);
if (boneInfo != boneInfoMap.end()) {
    int index = boneInfo->second.id;
    glm::mat4 offset = boneInfo->second.offset;
    m_FinalBoneMatrices[index] = globalTransformation * offset;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant