From 6684d0a928dfe0e7f29f02aa9760d3bda772f688 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Fri, 21 Nov 2025 12:24:32 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IP2Region.Net/Internal/ContentCacheStrategy.cs | 14 +++++++------- .../Internal/VectorIndexCacheStrategy.cs | 12 +++++------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/binding/csharp/IP2Region.Net/Internal/ContentCacheStrategy.cs b/binding/csharp/IP2Region.Net/Internal/ContentCacheStrategy.cs index 94e7f51..ad3e2ba 100644 --- a/binding/csharp/IP2Region.Net/Internal/ContentCacheStrategy.cs +++ b/binding/csharp/IP2Region.Net/Internal/ContentCacheStrategy.cs @@ -8,18 +8,18 @@ using IP2Region.Net.Internal.Abstractions; namespace IP2Region.Net.Internal; -class ContentCacheStrategy(string xdbPath) : AbstractCacheStrategy(xdbPath) +class ContentCacheStrategy : AbstractCacheStrategy { ReadOnlyMemory _cacheData = default; + public ContentCacheStrategy(string xdbPath) : base(xdbPath) + { + using var reader = base.GetXdbFileStream(); + _cacheData = base.GetData(0, (int)reader.Length); + } + public override ReadOnlyMemory GetData(int offset, int length) { - if (_cacheData.IsEmpty) - { - using var reader = base.GetXdbFileStream(); - _cacheData = base.GetData(0, (int)reader.Length); - } - return _cacheData.Slice(offset, length); } } diff --git a/binding/csharp/IP2Region.Net/Internal/VectorIndexCacheStrategy.cs b/binding/csharp/IP2Region.Net/Internal/VectorIndexCacheStrategy.cs index 4dea3fe..f34b1b2 100644 --- a/binding/csharp/IP2Region.Net/Internal/VectorIndexCacheStrategy.cs +++ b/binding/csharp/IP2Region.Net/Internal/VectorIndexCacheStrategy.cs @@ -8,16 +8,14 @@ using IP2Region.Net.Internal.Abstractions; namespace IP2Region.Net.Internal; -internal class VectorIndexCacheStrategy(string xdbPath) : AbstractCacheStrategy(xdbPath) +internal class VectorIndexCacheStrategy : AbstractCacheStrategy { ReadOnlyMemory _vectorCahe = default; - public override ReadOnlyMemory GetVectorIndexStartPos(int offset) + public VectorIndexCacheStrategy(string xdbPath) : base(xdbPath) { - if (_vectorCahe.IsEmpty) - { - _vectorCahe = base.GetData(256, 256 * 256 * 8); - } - return _vectorCahe.Slice(offset, 8); + _vectorCahe = base.GetData(256, 256 * 256 * 8); } + + public override ReadOnlyMemory GetVectorIndexStartPos(int offset) => _vectorCahe.Slice(offset, 8); }