Skip to content

Commit

Permalink
Apply optimizations from JetBrains Rider.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShikiSuen committed Feb 12, 2025
1 parent 422c610 commit b780380
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 34 deletions.
49 changes: 24 additions & 25 deletions Megrez/src/0_CSharpExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ public class HybridPriorityQueue<T> : IDisposable
private const int ArrayThreshold = 12; // 快取大小。
private const int InitialCapacity = 16; // 預設容量設為 2 的冪次以優化記憶體對齊。
private T[] _storage; // 改用陣列以減少記憶體間接引用。
private int _count; // 追蹤實際元素數量。
private readonly bool _isReversed;
private bool _usingArray;
private readonly object _syncRoot = new();
Expand All @@ -166,76 +165,76 @@ public class HybridPriorityQueue<T> : IDisposable
public HybridPriorityQueue(bool reversed = false) {
_isReversed = reversed;
_storage = new T[InitialCapacity];
_count = 0;
Count = 0;
_usingArray = true;
}

/// <summary>
/// 取得佇列中的元素數量。
/// </summary>
public int Count => _count;
public int Count { get; private set; }

/// <summary>
/// 檢查佇列是否為空。
/// </summary>
public bool IsEmpty => _count == 0;
public bool IsEmpty => Count == 0;

public void Enqueue(T element) {
lock (_syncRoot) {
// 確保容量足夠
if (_count == _storage.Length) {
if (Count == _storage.Length) {
Array.Resize(ref _storage, _storage.Length * 2);
}

if (_usingArray) {
if (_count >= ArrayThreshold) {
if (Count >= ArrayThreshold) {
SwitchToHeap();
_storage[_count++] = element;
HeapifyUp(_count - 1);
_storage[Count++] = element;
HeapifyUp(Count - 1);
return;
}

// 使用二分搜尋找到插入點。
int insertIndex = FindInsertionPoint(element);
// 手動移動元素以避免使用 Array.Copy(減少函數呼叫開銷)。
for (int i = _count; i > insertIndex; i--) {
for (int i = Count; i > insertIndex; i--) {
_storage[i] = _storage[i - 1];
}
_storage[insertIndex] = element;
_count++;
Count++;
} else {
_storage[_count] = element;
HeapifyUp(_count++);
_storage[Count] = element;
HeapifyUp(Count++);
}
}
}

public T? Dequeue() {
lock (_syncRoot) {
if (_count == 0) return default;
if (Count == 0) return default;

T result = _storage[0];
_count--;
Count--;

if (_usingArray) {
// 手動移動元素以避免使用 Array.Copy。
for (int i = 0; i < _count; i++) {
for (int i = 0; i < Count; i++) {
_storage[i] = _storage[i + 1];
}
return result;
}

// 堆積模式。
_storage[0] = _storage[_count];
if (_count > 0) HeapifyDown(0);
_storage[0] = _storage[Count];
if (Count > 0) HeapifyDown(0);
return result;
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int FindInsertionPoint(T element) {
int left = 0;
int right = _count;
int right = Count;

while (left < right) {
int mid = left + ((right - left) >> 1);
Expand All @@ -259,16 +258,16 @@ private int FindInsertionPoint(T element) {

private void SwitchToHeap() {
var backupStorage = (T[])_storage.Clone();
var backupCount = _count;
var backupCount = Count;

try {
_usingArray = false;
for (int i = (_count >> 1) - 1; i >= 0; i--) {
for (int i = (Count >> 1) - 1; i >= 0; i--) {
HeapifyDown(i);
}
} catch (Exception) {
_storage = backupStorage;
_count = backupCount;
Count = backupCount;
_usingArray = true;
throw;
}
Expand All @@ -290,7 +289,7 @@ private void HeapifyUp(int index) {
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void HeapifyDown(int index) {
T item = _storage[index];
int half = _count >> 1;
int half = Count >> 1;

while (index < half) {
int leftChild = (index << 1) + 1;
Expand All @@ -299,7 +298,7 @@ private void HeapifyDown(int index) {

T leftChildItem = _storage[leftChild];

if (rightChild < _count) {
if (rightChild < Count) {
T rightChildItem = _storage[rightChild];
if (Compare(rightChildItem, leftChildItem) < 0) {
bestChild = rightChild;
Expand Down Expand Up @@ -328,7 +327,7 @@ public void Clear() {
_storage = new T[InitialCapacity];
}
}
_count = 0;
Count = 0;
_usingArray = true;
}

Expand All @@ -354,7 +353,7 @@ public override int GetHashCode() {
unchecked {
int hash = 17;
hash = hash * 23 + _storage.GetHashCode();
hash = hash * 23 + _count.GetHashCode();
hash = hash * 23 + Count.GetHashCode();
hash = hash * 23 + _isReversed.GetHashCode();
hash = hash * 23 + _usingArray.GetHashCode();
return hash;
Expand Down
15 changes: 6 additions & 9 deletions Megrez/src/1_Compositor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public enum ResizeBehavior {
/// <summary>
/// 組字器的組態設定。
/// </summary>
public CompositorConfig Config = new();
public CompositorConfig Config;
/// <summary>
/// 一個幅位單元內所能接受的最長的節點幅位長度。
/// </summary>
Expand Down Expand Up @@ -178,6 +178,7 @@ public LangModelRanked TheLangModel {
public Compositor(LangModelProtocol langModel, string separator = "-") {
_theLangModel = new(ref langModel);
Separator = separator;
Config = new();
}

/// <summary>
Expand Down Expand Up @@ -431,7 +432,7 @@ private List<string> GetJoinedKeyArray(BRange range) =>
/// <returns>拿取的節點。拿不到的話就會是 null。</returns>
private Node? GetNodeAt(int location, int length, List<string> keyArray) {
location = Math.Max(Math.Min(location, Spans.Count - 1), 0); // 防呆。
if (location < 0 || location >= Spans.Count) return null;
// if (location < 0 || location >= Spans.Count) return null;
if (Spans[location].NodeOf(length) is not {} node) return null;
return node.KeyArray.SequenceEqual(keyArray) ? node : null;
}
Expand Down Expand Up @@ -491,7 +492,7 @@ public CompositorConfig(List<Node>? walkedNodes = null, List<string>? keys = nul
_cursor = cursor;
_maxSpanLength = maxSpanLength;
_marker = marker;
_separator = separator;
Separator = separator;
}

/// <summary>
Expand Down Expand Up @@ -538,17 +539,13 @@ public int Marker {
set => _marker = Math.Max(0, Math.Min(value, Length));
}

private string _separator;
/// <summary>
/// 在生成索引鍵字串時用來分割每個索引鍵單位。最好是鍵盤無法直接敲的 ASCII 字元。
/// </summary>
/// <remarks>
/// 更新該內容會同步更新 <see cref="Compositor.TheSeparator"/>;每次初期化一個新的組字器的時候都會覆寫之。
/// </remarks>
public string Separator {
get => _separator;
set => _separator = value;
}
public string Separator { get; set; }

/// <summary>
/// 公開:該組字器的長度。<para/>
Expand Down Expand Up @@ -603,7 +600,7 @@ public override int GetHashCode() {
hash = hash * 23 + Cursor.GetHashCode();
hash = hash * 23 + Marker.GetHashCode();
hash = hash * 23 + MaxSpanLength.GetHashCode();
hash = hash * 23 + (Separator?.GetHashCode() ?? 0);
hash = hash * 23 + (Separator.GetHashCode());
hash = hash * 23 + Keys.GetHashCode();
hash = hash * 23 + Spans.GetHashCode();
hash = hash * 23 + WalkedNodes.GetHashCode();
Expand Down

0 comments on commit b780380

Please sign in to comment.