Daily update on 20251110-002339 by Nitload Youmu. Additional Message : Dynamic wrapping is ok, going on test.

Signed-off-by: Nitload <YoumuLiu_NSI@whv0t.onmicrosoft.com>
This commit is contained in:
Nitload
2025-11-10 00:23:39 +08:00
parent 1479cd7c9f
commit 1dee467220
3 changed files with 68 additions and 74 deletions
@@ -2,8 +2,8 @@
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="ConcurrentCapacityFixedStack" // file="ConcurrentCapacityFixedStack"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload Space
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload Space. All rights reserved.
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal // of this software and associated documentation files (the "Software"), to deal
@@ -37,21 +37,18 @@ namespace StgSharp.Mathematics.Numeric
{ {
public static partial class MatrixParallel public static partial class MatrixParallel
{ {
/*
* Because caller of this class has its own sync object
* this queue will use no sync object internally.
*/
private class AwaitingQueue private class AwaitingQueue
{ {
private readonly ConcurrentQueue<WaitHandle> _fallback = [];
private Element _quickElements = new(); private Element _quickElements = new();
private volatile int _count; private volatile int _count;
private volatile int _lastGeneration; private volatile int _lastGeneration;
private readonly private readonly Queue<WaitHandle> _fallback = [];
#if NET9_0_OR_GREATER
Lock
#else
object
#endif
sync = new();
public AwaitingQueue() { } public AwaitingQueue() { }
@@ -63,17 +60,8 @@ namespace StgSharp.Mathematics.Numeric
Interlocked.Increment(ref _count); Interlocked.Increment(ref _count);
return; return;
} }
lock (sync) _quickElements[_count] = handle;
{ Interlocked.Increment(ref _count);
if (_count > 7)
{
_fallback.Enqueue(handle);
Interlocked.Increment(ref _count);
return;
}
_quickElements[_count] = handle;
Interlocked.Increment(ref _count);
}
} }
public bool TryDequeue(int generation, out WaitHandle handle) public bool TryDequeue(int generation, out WaitHandle handle)
@@ -84,25 +72,22 @@ namespace StgSharp.Mathematics.Numeric
handle = null!; handle = null!;
return false; return false;
} }
lock (sync) if (_lastGeneration == generation)
{ {
if (_lastGeneration == generation) for (int i = 0; i < _count; i++) {
{ _quickElements[i].UpdatePriority(generation);
for (int i = 0; i < _count; i++) {
_quickElements[i].UpdatePriority(generation);
}
Element.Sort(_quickElements);
} else
{
_lastGeneration = generation;
} }
handle = _quickElements[0]; Element.Sort(_quickElements);
_count--; } else
if (_count > 7) { {
_ = _fallback.TryDequeue(out _quickElements[0]!); _lastGeneration = generation;
}
return true;
} }
handle = _quickElements[0];
_count--;
if (_count > 7) {
_ = _fallback.TryDequeue(out _quickElements[0]!);
}
return true;
} }
public bool TryPeek(int generation, out WaitHandle handle) public bool TryPeek(int generation, out WaitHandle handle)
@@ -113,20 +98,17 @@ namespace StgSharp.Mathematics.Numeric
handle = null!; handle = null!;
return false; return false;
} }
lock (sync) for (int i = 0; i < _count; i++) {
{ _quickElements[i].UpdatePriority(generation);
for (int i = 0; i < _count; i++) {
_quickElements[i].UpdatePriority(generation);
}
Element.Sort(_quickElements);
_lastGeneration = generation;
handle = _quickElements[0];
_count--;
if (_count > 7) {
_ = _fallback.TryDequeue(out _quickElements[0]!);
}
return true;
} }
Element.Sort(_quickElements);
_lastGeneration = generation;
handle = _quickElements[0];
_count--;
if (_count > 7) {
_ = _fallback.TryDequeue(out _quickElements[0]!);
}
return true;
} }
public bool TryPopIfRefEqual(int generation, ref WaitHandle handle) public bool TryPopIfRefEqual(int generation, ref WaitHandle handle)
@@ -136,24 +118,21 @@ namespace StgSharp.Mathematics.Numeric
handle = null!; handle = null!;
return false; return false;
} }
lock (sync) for (int i = 0; i < _count; i++) {
{ _quickElements[i].UpdatePriority(generation);
for (int i = 0; i < _count; i++) {
_quickElements[i].UpdatePriority(generation);
}
Element.Sort(_quickElements);
_lastGeneration = generation;
handle = _quickElements[0];
_count--;
if (!ReferenceEquals(_quickElements[0], handle)) {
return false;
}
_quickElements[0] = null!;
if (_count > 7) {
_ = _fallback.TryDequeue(out _quickElements[0]!);
}
return true;
} }
Element.Sort(ref _quickElements);
_lastGeneration = generation;
handle = _quickElements[0];
_count--;
if (!ReferenceEquals(_quickElements[0], handle)) {
return false;
}
_quickElements[0] = null!;
if (_count > 7) {
_ = _fallback.TryDequeue(out _quickElements[0]!);
}
return true;
} }
} }
@@ -164,11 +143,26 @@ namespace StgSharp.Mathematics.Numeric
private WaitHandle value; private WaitHandle value;
public static void Sort(Element e) public static void Sort(ref Element e)
{ {
/* int maxIdx = 0;
* yet to do WaitHandle first = e[0];
*/ int maxPr = first?.Priority ?? int.MaxValue;
for (int i = 1; i < 8; i++)
{
WaitHandle cur = e[i];
int p = cur?.Priority ?? int.MinValue;
if (p > maxPr)
{
maxPr = p;
maxIdx = i;
}
}
if (maxIdx != 0) {
(e[0], e[maxIdx]) = (e[maxIdx], e[0]);
}
} }
} }
@@ -2,8 +2,8 @@
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="MatrixParallel" // file="MatrixParallel"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload Space
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload Space. All rights reserved.
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal // of this software and associated documentation files (the "Software"), to deal
@@ -75,7 +75,7 @@ namespace StgSharp.Mathematics.Numeric
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
_threads.Push(new()); _threads.Push(new());
} }
_awaitingLeaderHandle = new ConcurrentQueue<WaitHandle>(); _awaitingLeaderHandle = new AwaitingQueue();
IsInitialized = true; IsInitialized = true;
} }