Skip to content

Commit

Permalink
update to 3.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastiano Mandala committed Sep 17, 2023
1 parent 409d48a commit 28346d8
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 100 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog
All notable changes to this project will be documented in this file. Changes are listed in random order of importance.

## [3.5.0] - 09-2023

* Refactor: split NB/MB struct from their internal logic that must be used only by the framework. Eventually NB and MB structs must be ref, as they are not supposed to be held (they may become invalid over the time). However due to the current DOTS patterns this is not possible. In future a sentinel pattern will allow to lease these buffers with the assumption that they can't be modified while held (and if a modification happens an exception will throw)

## [3.4.3] - 05-2023

* fix platform profiler compilation bugs
Expand Down
23 changes: 11 additions & 12 deletions DataStructures/Dictionaries/SveltoDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,34 +140,33 @@ public IBuffer<TValue> UnsafeGetValues(out uint count)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Add(TKey key, in TValue value)
{
var ret = AddValue(key, out var index);
var itemAdded = AddValue(key, out var index);

#if DEBUG && !PROFILE_SVELTO
if (ret == false)
if (itemAdded == false)
throw new SveltoDictionaryException("Key already present");
#endif

_values[index] = value;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryAdd(TKey key, in TValue value, out uint index)
{
var ret = AddValue(key, out index);
var itemAdded = AddValue(key, out index);

if (ret == true)
if (itemAdded == true)
_values[index] = value;

return ret;
return itemAdded;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Set(TKey key, in TValue value)
{
var ret = AddValue(key, out var index);
var itemAdded = AddValue(key, out var index);

#if DEBUG && !PROFILE_SVELTO
if (ret == true)
if (itemAdded == true)
throw new SveltoDictionaryException("trying to set a value on a not existing key");
#endif

Expand Down Expand Up @@ -393,7 +392,7 @@ public bool Remove(TKey key)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Remove(TKey key, out int index, out TValue value)
public bool Remove(TKey key, out uint index, out TValue value)
{
int hash = key.GetHashCode();
uint bucketIndex = Reduce((uint)hash, (uint)_buckets.capacity, _fastModBucketsMultiplier);
Expand Down Expand Up @@ -444,7 +443,7 @@ public bool Remove(TKey key, out int index, out TValue value)
return false; //not found!
}

index = indexToValueToRemove; //index is a out variable, for internal use we want to know the index of the element to remove
index = (uint)indexToValueToRemove; //index is a out variable, for internal use we want to know the index of the element to remove

_freeValueCellIndex--; //one less value to iterate
value = _values[indexToValueToRemove]; //value is a out variable, we want to know the value of the element to remove
Expand Down Expand Up @@ -877,11 +876,11 @@ public void SetRange(uint startIndex, uint count)
where TValueStrategy : struct,
IBufferStrategy<TValue>
{
public KeyValuePairFast(in TKey keys, in TValueStrategy dicValues, int index)
public KeyValuePairFast(in TKey key, in TValueStrategy dicValues, int index)
{
_dicValues = dicValues;
_index = index;
_key = keys;
_key = key;
}

public TKey key => _key;
Expand Down
79 changes: 75 additions & 4 deletions DataStructures/DualMemorySupport/MB.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Svelto.ECS")]

namespace Svelto.DataStructures
{
/// <summary>
Expand All @@ -15,9 +17,9 @@ namespace Svelto.DataStructures
/// but the count will stay zero. It's not the MB responsibility to track the count
/// </summary>
/// <typeparam name="T"></typeparam>
public struct MB<T>:IBuffer<T>
internal struct MBInternal<T>:IBuffer<T>
{
internal MB(T[] array) : this()
MBInternal(T[] array) : this()
{
_buffer = array;
}
Expand Down Expand Up @@ -50,7 +52,10 @@ public T[] ToManagedArray()
{
return _buffer;
}


public static implicit operator MB<T>(MBInternal<T> proxy) => new MB<T>(proxy);
public static implicit operator MBInternal<T>(MB<T> proxy) => new MBInternal<T>(proxy.ToManagedArray());

public int capacity
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -85,7 +90,73 @@ public int capacity
return ref _buffer[index];
}
}

T[] _buffer;
}

public ref struct MB<T>
{
MBInternal<T> _bufferImplementation;

internal MB(MBInternal<T> mbInternal)
{
_bufferImplementation = mbInternal;
}

public void CopyTo(uint sourceStartIndex, T[] destination, uint destinationStartIndex, uint count)
{
_bufferImplementation.CopyTo(sourceStartIndex, destination, destinationStartIndex, count);
}

public void Clear()
{
_bufferImplementation.Clear();
}

public int capacity => _bufferImplementation.capacity;

public bool isValid => _bufferImplementation.isValid;

public void Set(T[] array)
{
_bufferImplementation.Set(array);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CopyFrom(T[] collection, uint actualSize)
{
_bufferImplementation.CopyFrom(collection, actualSize);
}

/// <summary>
/// todo: this must go away, it's not safe. it must become internal and only used by the framework
/// externally should use the AsReader, AsWriter, AsReadOnly, AsParallelReader, AsParallelWriter pattern
/// </summary>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T[] ToManagedArray()
{
return _bufferImplementation.ToManagedArray();
}

public ref T this[uint index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => ref _bufferImplementation[index];
}

public ref T this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
#if DEBUG && ENABLE_PARANOID_CHECKS
if (index >= _buffer.Length)
throw new IndexOutOfRangeException("Paranoid check failed!");
#endif

return ref _bufferImplementation[index];
}
}
}
}
8 changes: 4 additions & 4 deletions DataStructures/DualMemorySupport/ManagedStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public struct ManagedStrategy<T> : IBufferStrategy<T>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void Alloc(uint size)
{
var b = default(MB<T>);
var b = default(MBInternal<T>);
b.Set(new T[size]);
_realBuffer = b;
_buffer = _realBuffer;
Expand All @@ -31,7 +31,7 @@ void Alloc(uint size)
#endif
public void Alloc(uint size, Allocator allocator, bool memClear)
{
var b = default(MB<T>);
var b = default(MBInternal<T>);
var array = new T[size];
#if (NEW_C_SHARP && !UNITY_5_3_OR_NEWER)
if (memClear)
Expand Down Expand Up @@ -60,7 +60,7 @@ public void Resize(uint newSize, bool copyContent = true, bool memClear = true)
if (memClear)
Array.Clear(realBuffer, 0, realBuffer.Length);
#endif
var b = default(MB<T>);
var b = default(MBInternal<T>);
b.Set(realBuffer);
_realBuffer = b;
_buffer = _realBuffer;
Expand Down Expand Up @@ -151,6 +151,6 @@ IBuffer<T> IBufferStrategy<T>.ToBuffer()
public void Dispose() {}

IBuffer<T> _buffer;
MB<T> _realBuffer;
MBInternal<T> _realBuffer;
}
}

0 comments on commit 28346d8

Please sign in to comment.