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

Add Pipeline Types to System.ClientModel library #41085

Merged
merged 22 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 28 additions & 44 deletions sdk/core/Azure.Core/src/Internal/ArrayBackedPropertyBag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

using System;
using System.Buffers;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace Azure.Core
Expand All @@ -16,31 +14,17 @@ namespace Azure.Core
/// </summary>
internal struct ArrayBackedPropertyBag<TKey, TValue> where TKey : struct, IEquatable<TKey>
{
private Kvp _first;
private Kvp _second;
private Kvp[]? _rest;
private (TKey Key, TValue Value) _first;
private (TKey Key, TValue Value) _second;
private (TKey Key, TValue Value)[]? _rest;
private int _count;
#if DEBUG
private bool _disposed;
#endif
private readonly struct Kvp
{
public readonly TKey Key;
public readonly TValue Value;

public Kvp(TKey key, TValue value)
{
Key = key;
Value = value;
}

public void Deconstruct(out TKey key, out TValue value)
{
key = Key;
value = Value;
}
private readonly object _lock = new();
annelo-msft marked this conversation as resolved.
Show resolved Hide resolved
annelo-msft marked this conversation as resolved.
Show resolved Hide resolved

public override string ToString() => $"[{Key}, {Value?.ToString() ?? "<null>"}]";
public ArrayBackedPropertyBag()
{
}

public int Count
Expand Down Expand Up @@ -72,13 +56,13 @@ public void GetAt(int index, out TKey key, out TValue value)
};
}

public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
public bool TryGetValue(TKey key, /*[MaybeNullWhen(false)]*/ out TValue value)
annelo-msft marked this conversation as resolved.
Show resolved Hide resolved
{
CheckDisposed();
var index = GetIndex(key);
if (index < 0)
{
value = default;
value = default!;
return false;
}

Expand Down Expand Up @@ -108,7 +92,7 @@ public void Set(TKey key, TValue value)
if (index < 0)
AddInternal(key, value);
else
SetAt(index, new Kvp(key, value));
SetAt(index, new(key, value));
}

public bool TryRemove(TKey key)
Expand Down Expand Up @@ -146,7 +130,7 @@ public bool TryRemove(TKey key)

return false;
default:
Kvp[] rest = GetRest();
(TKey Key, TValue Value)[] rest = GetRest();
if (IsFirst(key))
{
_first = _second;
Expand Down Expand Up @@ -193,44 +177,44 @@ private void AddInternal(TKey key, TValue value)
switch (_count)
{
case 0:
_first = new Kvp(key, value);
_first = new(key, value);
annelo-msft marked this conversation as resolved.
Show resolved Hide resolved
_count = 1;
return;
case 1:
if (IsFirst(key))
{
_first = new Kvp(_first.Key, value);
_first = new(_first.Key, value);
}
else
{
_second = new Kvp(key, value);
_second = new(key, value);
_count = 2;
}

return;
default:
if (_rest == null)
{
_rest = ArrayPool<Kvp>.Shared.Rent(8);
_rest[_count++ - 2] = new Kvp(key, value);
_rest = ArrayPool<(TKey Key, TValue Value)>.Shared.Rent(8);
_rest[_count++ - 2] = new(key, value);
return;
}

if (_rest.Length <= _count)
{
var larger = ArrayPool<Kvp>.Shared.Rent(_rest.Length << 1);
var larger = ArrayPool<(TKey Key, TValue Value)>.Shared.Rent(_rest.Length << 1);
_rest.CopyTo(larger, 0);
var old = _rest;
_rest = larger;
ArrayPool<Kvp>.Shared.Return(old, true);
ArrayPool<(TKey Key, TValue Value)>.Shared.Return(old, true);
}
_rest[_count++ - 2] = new Kvp(key, value);
_rest[_count++ - 2] = new(key, value);
return;
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void SetAt(int index, Kvp value)
private void SetAt(int index, (TKey Key, TValue Value) value)
{
if (index == 0)
_first = value;
Expand Down Expand Up @@ -261,7 +245,7 @@ private int GetIndex(TKey key)
if (_count <= 2)
return -1;

Kvp[] rest = GetRest();
(TKey Key, TValue Value)[] rest = GetRest();
int max = _count - 2;
for (var i = 0; i < max; i++)
{
Expand All @@ -271,7 +255,7 @@ private int GetIndex(TKey key)
return -1;
}

internal void Dispose()
public void Dispose()
{
#if DEBUG
if (_disposed)
Expand All @@ -288,15 +272,16 @@ internal void Dispose()
return;
}

var rest = _rest;
_rest = default;
ArrayPool<Kvp>.Shared.Return(rest, true);
lock (_lock)
{
var rest = _rest;
_rest = default;
ArrayPool<(TKey Key, TValue Value)>.Shared.Return(rest, true);
}
}

private Kvp[] GetRest() => _rest ?? throw new InvalidOperationException($"{nameof(_rest)} field is null while {nameof(_count)} == {_count}");
private (TKey Key, TValue Value)[] GetRest() => _rest ?? throw new InvalidOperationException($"{nameof(_rest)} field is null while {nameof(_count)} == {_count}");

#pragma warning disable CA1822
[Conditional("DEBUG")]
private void CheckDisposed()
{
#if DEBUG
Expand All @@ -306,6 +291,5 @@ private void CheckDisposed()
}
#endif
}
#pragma warning restore CA1822
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

using System.Buffers;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace System.ClientModel.Internal;
Expand All @@ -14,31 +13,17 @@ namespace System.ClientModel.Internal;
/// </summary>
internal struct ArrayBackedPropertyBag<TKey, TValue> where TKey : struct, IEquatable<TKey>
annelo-msft marked this conversation as resolved.
Show resolved Hide resolved
{
private Kvp _first;
private Kvp _second;
private Kvp[]? _rest;
private (TKey Key, TValue Value) _first;
private (TKey Key, TValue Value) _second;
private (TKey Key, TValue Value)[]? _rest;
private int _count;
#if DEBUG
private bool _disposed;
#endif
private readonly struct Kvp
{
public readonly TKey Key;
public readonly TValue Value;

public Kvp(TKey key, TValue value)
{
Key = key;
Value = value;
}

public void Deconstruct(out TKey key, out TValue value)
{
key = Key;
value = Value;
}
private readonly object _lock = new();

public override string ToString() => $"[{Key}, {Value?.ToString() ?? "<null>"}]";
public ArrayBackedPropertyBag()
{
}

public int Count
Expand Down Expand Up @@ -106,7 +91,7 @@ public void Set(TKey key, TValue value)
if (index < 0)
AddInternal(key, value);
else
SetAt(index, new Kvp(key, value));
SetAt(index, new(key, value));
}

public bool TryRemove(TKey key)
Expand Down Expand Up @@ -144,7 +129,7 @@ public bool TryRemove(TKey key)

return false;
default:
Kvp[] rest = GetRest();
(TKey Key, TValue Value)[] rest = GetRest();
if (IsFirst(key))
{
_first = _second;
Expand Down Expand Up @@ -191,44 +176,44 @@ private void AddInternal(TKey key, TValue value)
switch (_count)
{
case 0:
_first = new Kvp(key, value);
_first = new(key, value);
_count = 1;
return;
case 1:
if (IsFirst(key))
{
_first = new Kvp(_first.Key, value);
_first = new(_first.Key, value);
}
else
{
_second = new Kvp(key, value);
_second = new(key, value);
_count = 2;
}

return;
default:
if (_rest == null)
{
_rest = ArrayPool<Kvp>.Shared.Rent(8);
_rest[_count++ - 2] = new Kvp(key, value);
_rest = ArrayPool<(TKey Key, TValue Value)>.Shared.Rent(8);
_rest[_count++ - 2] = new(key, value);
return;
}

if (_rest.Length <= _count)
{
var larger = ArrayPool<Kvp>.Shared.Rent(_rest.Length << 1);
var larger = ArrayPool<(TKey Key, TValue Value)>.Shared.Rent(_rest.Length << 1);
_rest.CopyTo(larger, 0);
var old = _rest;
_rest = larger;
ArrayPool<Kvp>.Shared.Return(old, true);
ArrayPool<(TKey Key, TValue Value)>.Shared.Return(old, true);
}
_rest[_count++ - 2] = new Kvp(key, value);
_rest[_count++ - 2] = new(key, value);
return;
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void SetAt(int index, Kvp value)
private void SetAt(int index, (TKey Key, TValue Value) value)
{
if (index == 0)
_first = value;
Expand Down Expand Up @@ -259,7 +244,7 @@ private int GetIndex(TKey key)
if (_count <= 2)
return -1;

Kvp[] rest = GetRest();
(TKey Key, TValue Value)[] rest = GetRest();
int max = _count - 2;
for (var i = 0; i < max; i++)
{
Expand All @@ -286,15 +271,16 @@ public void Dispose()
return;
}

var rest = _rest;
_rest = default;
ArrayPool<Kvp>.Shared.Return(rest, true);
lock (_lock)
{
var rest = _rest;
_rest = default;
ArrayPool<(TKey Key, TValue Value)>.Shared.Return(rest, true);
}
}

private Kvp[] GetRest() => _rest ?? throw new InvalidOperationException($"{nameof(_rest)} field is null while {nameof(_count)} == {_count}");
private (TKey Key, TValue Value)[] GetRest() => _rest ?? throw new InvalidOperationException($"{nameof(_rest)} field is null while {nameof(_count)} == {_count}");

#pragma warning disable CA1822
[Conditional("DEBUG")]
private void CheckDisposed()
annelo-msft marked this conversation as resolved.
Show resolved Hide resolved
{
#if DEBUG
annelo-msft marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -304,5 +290,4 @@ private void CheckDisposed()
}
#endif
}
#pragma warning restore CA1822
}
Loading