Search Unity

Hash collisions in ContentCatalogData

Discussion in 'Addressables' started by UWE_Dushan, May 1, 2019.

  1. UWE_Dushan

    UWE_Dushan

    Joined:
    Jan 7, 2015
    Posts:
    3
    Hi!

    I'm working on converting our project to Addressables and I think there's an issue with how the content catalog hashes lists of dependencies.

    ContentCatalogData.SetData runs through all content entries and tries to find identical sets of dependencies by combining their hashes:

    Code (CSharp):
    1. //seed and and factor values taken from https://stackoverflow.com/questions/1646807/quick-and-simple-hash-code-combinations
    2. int hashCode = 1009;
    3. foreach (var dep in entry.Dependencies)
    4.     hashCode = hashCode * 9176 + dep.GetHashCode();
    5.  
    The dependencies are then replaced with a single virtual one, which is retrieved using this hashCode. Any collision completely breaks the dependency graph serialized into 'addressables_content_state.bin' (I'm seeing ~10% collision rate on just over 300 entries). As a result, Addressable.LoadAsset fails for affected assets as it's trying to load it from wrong asset bundle.

    My quick fix is to increment hashCode until it maps to the correct list of dependencies:

    Code (CSharp):
    1.  
    2. bool isNew = false;
    3. keys.Add(hashCode, ref isNew);
    4. var deps = entry.Dependencies.Select(d => keyIndexToEntries[d][0]).ToList();
    5. while (!isNew)
    6. {
    7.     // Check for collision
    8.     var depsForHashCode = keyIndexToEntries[hashCode];
    9.     if (deps.Equals(depsForHashCode))
    10.         break;
    11.     hashCode++;
    12.     keys.Add(hashCode, ref isNew);
    13. }
    14.  
    15. if (isNew)
    16. {
    17.     //if this combination of dependencies is new, add a new entry and add its key to all contained entries
    18.     keyIndexToEntries.Add(hashCode, deps);
    19.     foreach (var dep in deps)
    20.         dep.Keys.Add(hashCode);
    21. }
    22.  
    I'm on Addressables 0.7.5 and Unity 2019.1.0f2.
    Hope this helps,

    Dushan
     
  2. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    We haven't run into this sort of hash collision. Could you create a unity bug with a repro project? It would really help us understand what specifically will cause collisions.