修复 hash_combine 中最后一个参数被忽略的 Bug#515
Open
yuanjay07 wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
修复的问题描述
在
engine/source/runtime/core/base/hash.h中,多参数版本的hash_combine函数实现如下:递归的终止条件检查为
sizeof...(Ts) > 1。这会导致一个严重的 Bug:sizeof...(Ts)的值为 1)时,条件判断1 > 1的结果为false。hash_combine合并到seed中,从而被静默忽略。具体场景举例
在
MaterialSourceDesc::getHashValue()中,哈希计算传入了 5 个参数:hash_combine(hash, m_base_color_file, m_metallic_roughness_file, m_normal_file, m_occlusion_file, m_emissive_file);在递归处理到第 4 个参数
m_occlusion_file时,剩余的变参包Ts...中只包含m_emissive_file(此时sizeof...(Ts)为 1)。由于
1 > 1为假,m_emissive_file不会被递归调用,导致最终算出的hash根本没有包含m_emissive_file的哈希值。解决方案
将递归终止条件修改为
sizeof...(Ts) > 0:改进后的逻辑
sizeof...(Ts)大于 0(即还有剩余参数)时,条件继续成立,调用hash_combine(seed, rest...)。sizeof...(Ts)为 1,条件1 > 0为true,触发调用hash_combine(seed, rest...)(此时只有两个实际参数:seed和最后一个元素)。影响范围
hash_combine处理多参数时的潜在哈希冲突与不准确性,保证了哈希合并的完整性。