public void Renew() { if (_perCorePropertiesCache.TryPull(out _cachedSorts) == false) { _cachedSorts = new CachedSort[CachedSortsSize]; // size is fixed and used in GetPropertiesHashedIndex } }
private void UnlikelySortProperties(FastList <BlittableJsonDocumentBuilder.PropertyTag> properties) { _hasDuplicates = false; var index = GetPropertiesHashedIndex(properties); if (_cachedSorts[index] == null) { _cachedSorts[index] = new CachedSort(); } _cachedSorts[index].Sorting.Clear(); for (int i = 0; i < properties.Count; i++) { _cachedSorts[index].Sorting.Add(new PropertyPosition { Property = properties[i].Property, SortedPosition = -1 }); } _cachedSorts[index].FinalCount = properties.Count; properties.Sort(ref _sorter); // The item comparison method has a side effect, which can modify the _hasDuplicates field. // This can either be true or false at any given time. if (_hasDuplicates) { // leave just the latest for (int i = 0; i < properties.Count - 1; i++) { if (properties[i].Property.Equals(properties[i + 1].Property)) { _cachedSorts[index].FinalCount--; _cachedSorts[index].Sorting[i + 1] = new PropertyPosition { Property = properties[i + 1].Property, // set it to the previous value, so it'll just overwrite // this saves us a check and more complex code SortedPosition = i }; properties.RemoveAt(i + 1); i--; } } } for (int i = 0; i < _cachedSorts[index].Sorting.Count; i++) { var propPos = _cachedSorts[index].Sorting[i]; propPos.SortedPosition = -1; for (int j = 0; j < properties.Count; j++) { if (properties[j].Property == propPos.Property) { propPos.SortedPosition = j; break; } } } }