From ffda10be865132ea93e1949d93480aecb80bf706 Mon Sep 17 00:00:00 2001
From: GeWuYou <95328647+GeWuYou@users.noreply.github.com>
Date: Wed, 11 Mar 2026 08:58:33 +0800
Subject: [PATCH 1/3] =?UTF-8?q?refactor(core):=20=E4=BC=98=E5=8C=96ScopedA?=
=?UTF-8?q?rray=E7=BB=93=E6=9E=84=E4=BD=93=E5=AE=9E=E7=8E=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 将ScopedArray从readonly struct改为ref struct以提高性能
- 添加_array字段存储数组引用并移除公共Array属性
- 在Dispose方法中添加空值检查避免重复释放
- 添加Span属性和索引器支持直接访问数组元素
- 使用#pragma warning禁用CA1819警告并优化代码风格
---
.../Extensions/ArrayPoolExtensions.cs | 31 ++++++++++++++-----
1 file changed, 23 insertions(+), 8 deletions(-)
diff --git a/GFramework.Core/Extensions/ArrayPoolExtensions.cs b/GFramework.Core/Extensions/ArrayPoolExtensions.cs
index ab3e7ea..64d6fd5 100644
--- a/GFramework.Core/Extensions/ArrayPoolExtensions.cs
+++ b/GFramework.Core/Extensions/ArrayPoolExtensions.cs
@@ -89,26 +89,29 @@ public static class ArrayPoolExtensions
/// 可自动释放的数组包装器
///
/// 数组元素类型
- public readonly struct ScopedArray : IDisposable
+ public ref struct ScopedArray
{
private readonly ArrayPool _pool;
private readonly bool _clearOnReturn;
+ private T[]? _array;
+#pragma warning disable CA1819
///
/// 获取租用的数组
///
- public T[] Array { get; }
+ public T[] Array => _array!;
+#pragma warning restore CA1819
///
/// 获取数组的长度
///
- public int Length => Array.Length;
+ public int Length => _array!.Length;
internal ScopedArray(ArrayPool pool, int minimumLength, bool clearOnReturn)
{
_pool = pool;
_clearOnReturn = clearOnReturn;
- Array = pool.Rent(minimumLength);
+ _array = pool.Rent(minimumLength);
}
///
@@ -116,14 +119,18 @@ public static class ArrayPoolExtensions
///
public void Dispose()
{
- _pool.Return(Array, _clearOnReturn);
+ if (_array is null)
+ return;
+
+ _pool.Return(_array, _clearOnReturn);
+ _array = null;
}
///
/// 获取数组的 Span 视图
///
/// 数组的 Span
- public Span AsSpan() => Array.AsSpan();
+ public Span AsSpan() => _array!;
///
/// 获取数组指定范围的 Span 视图
@@ -131,6 +138,14 @@ public static class ArrayPoolExtensions
/// 起始索引
/// 长度
/// 数组指定范围的 Span
- public Span AsSpan(int start, int length) => Array.AsSpan(start, length);
+ public Span AsSpan(int start, int length)
+ => _array!.AsSpan(start, length);
+
+ ///
+ /// 获取数组指定索引处的引用
+ ///
+ /// 要获取引用的索引位置
+ /// 指定索引处元素的引用
+ public ref T this[int index] => ref _array![index];
}
-}
+}
\ No newline at end of file
From ea79df232bacbec320b77d55c13b63ea40852c68 Mon Sep 17 00:00:00 2001
From: GeWuYou <95328647+GeWuYou@users.noreply.github.com>
Date: Wed, 11 Mar 2026 12:34:08 +0800
Subject: [PATCH 2/3] =?UTF-8?q?refactor(GFramework.Core):=20=E4=BC=98?=
=?UTF-8?q?=E5=8C=96ScopedArray=E5=AE=9E=E7=8E=B0=E5=B9=B6=E6=B7=BB?=
=?UTF-8?q?=E5=8A=A0=E8=B5=84=E6=BA=90=E6=B8=85=E7=90=86=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 为ScopedArray实现IDisposable接口以支持资源清理
- 修改Array属性访问方式,通过GetArray方法确保对象未被释放
- 更新Length属性实现,依赖Array属性而非直接访问私有字段
- 调整AsSpan方法实现,统一通过Array属性获取数组实例
- 重写索引器实现,使用Array属性替代直接字段访问
- 新增GetArray私有方法,用于检查对象状态并返回内部数组
- 添加ObjectDisposedException异常处理,防止访问已释放对象
---
.../Extensions/ArrayPoolExtensions.cs | 22 ++++++++++++++-----
1 file changed, 16 insertions(+), 6 deletions(-)
diff --git a/GFramework.Core/Extensions/ArrayPoolExtensions.cs b/GFramework.Core/Extensions/ArrayPoolExtensions.cs
index 64d6fd5..80a070a 100644
--- a/GFramework.Core/Extensions/ArrayPoolExtensions.cs
+++ b/GFramework.Core/Extensions/ArrayPoolExtensions.cs
@@ -89,7 +89,7 @@ public static class ArrayPoolExtensions
/// 可自动释放的数组包装器
///
/// 数组元素类型
- public ref struct ScopedArray
+ public ref struct ScopedArray : IDisposable
{
private readonly ArrayPool _pool;
private readonly bool _clearOnReturn;
@@ -99,13 +99,13 @@ public static class ArrayPoolExtensions
///
/// 获取租用的数组
///
- public T[] Array => _array!;
+ public T[] Array => GetArray();
#pragma warning restore CA1819
///
/// 获取数组的长度
///
- public int Length => _array!.Length;
+ public int Length => Array.Length;
internal ScopedArray(ArrayPool pool, int minimumLength, bool clearOnReturn)
{
@@ -130,7 +130,7 @@ public static class ArrayPoolExtensions
/// 获取数组的 Span 视图
///
/// 数组的 Span
- public Span AsSpan() => _array!;
+ public Span AsSpan() => Array.AsSpan();
///
/// 获取数组指定范围的 Span 视图
@@ -139,13 +139,23 @@ public static class ArrayPoolExtensions
/// 长度
/// 数组指定范围的 Span
public Span AsSpan(int start, int length)
- => _array!.AsSpan(start, length);
+ => Array.AsSpan(start, length);
///
/// 获取数组指定索引处的引用
///
/// 要获取引用的索引位置
/// 指定索引处元素的引用
- public ref T this[int index] => ref _array![index];
+ public ref T this[int index] => ref Array[index];
+
+ ///
+ /// 获取内部数组实例
+ ///
+ /// 内部数组实例
+ /// 当对象已被丢弃时抛出
+ private T[] GetArray()
+ {
+ return _array ?? throw new ObjectDisposedException(nameof(ScopedArray));
+ }
}
}
\ No newline at end of file
From b6c13088cd9ddecf8ed52d30586bd7b429041d01 Mon Sep 17 00:00:00 2001
From: GeWuYou <95328647+GeWuYou@users.noreply.github.com>
Date: Wed, 11 Mar 2026 12:44:32 +0800
Subject: [PATCH 3/3] =?UTF-8?q?refactor(GFramework.Core):=20=E7=A7=BB?=
=?UTF-8?q?=E9=99=A4ScopedArray=E7=9A=84IDisposable=E6=8E=A5=E5=8F=A3?=
=?UTF-8?q?=E5=AE=9E=E7=8E=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 从ScopedArray结构体中移除IDisposable接口
- 保留数组池管理和自动释放功能
- 简化类型定义以提高性能表现
---
GFramework.Core/Extensions/ArrayPoolExtensions.cs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/GFramework.Core/Extensions/ArrayPoolExtensions.cs b/GFramework.Core/Extensions/ArrayPoolExtensions.cs
index 80a070a..6bb62f0 100644
--- a/GFramework.Core/Extensions/ArrayPoolExtensions.cs
+++ b/GFramework.Core/Extensions/ArrayPoolExtensions.cs
@@ -89,7 +89,7 @@ public static class ArrayPoolExtensions
/// 可自动释放的数组包装器
///
/// 数组元素类型
- public ref struct ScopedArray : IDisposable
+ public ref struct ScopedArray
{
private readonly ArrayPool _pool;
private readonly bool _clearOnReturn;