Skip to content

Commit 241f678

Browse files
committed
优化模型加载逻辑,移除LanguageIdentification:TryLoadModelWithLowMemory选项;
1 parent a5204f8 commit 241f678

File tree

4 files changed

+9
-39
lines changed

4 files changed

+9
-39
lines changed

readme.md

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,3 @@ var classifier = new LanguageIdentificationClassifier(model);
6767
- 具体各个参数是什么意义。。不清楚。。自行研究源项目。。。
6868

6969
----
70-
71-
3. 加载默认模型时,使用更少的内存
72-
73-
```C#
74-
AppContext.SetSwitch("LanguageIdentification:TryLoadModelWithLowMemory", true);
75-
```
76-
77-
- 在使用前设置开关;
78-
- 加载时会使用更少的内存,对应的会增加加载时间;

src/LanguageIdentification/LanguageIdentification.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<VersionPrefix>1.0.2</VersionPrefix>
3+
<VersionPrefix>1.0.3</VersionPrefix>
44
<IsPackable>true</IsPackable>
55
<Description>本地离线运行的语言识别库,移植自langid-java</Description>
66

src/LanguageIdentification/LanguageIdentificationModel.cs

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.IO;
66
using System.IO.Compression;
77
using System.Linq;
8-
using System.Runtime.InteropServices;
98
using System.Runtime.Serialization;
109
using System.Text;
1110

@@ -317,34 +316,9 @@ private static LanguageIdentificationModel InternalLoadDefaultModel()
317316
{
318317
using var stream = new MemoryStream(Resource.ModelData);
319318
using var gzipStream = new GZipStream(stream, CompressionMode.Decompress);
319+
using var bufferedStream = new BufferedStream(gzipStream);
320320

321-
if (AppContext.TryGetSwitch("LanguageIdentification:TryLoadModelWithLowMemory", out var tryLoadModelWithLowMemory)
322-
&& tryLoadModelWithLowMemory)
323-
{
324-
return Deserialize(gzipStream);
325-
}
326-
else
327-
{
328-
//解压后MemoryStream长度为 7649400 ,初始化一个 7700000 大小的数组,保留一点冗余
329-
const int BufferSize = 7700000;
330-
331-
//使用非托管内存,以便手动释放
332-
var unmanagePtr = Marshal.AllocHGlobal(BufferSize);
333-
try
334-
{
335-
unsafe
336-
{
337-
using var unzipedStream = new UnmanagedMemoryStream((byte*)unmanagePtr.ToPointer(), 0, BufferSize, FileAccess.ReadWrite);
338-
gzipStream.CopyTo(unzipedStream);
339-
unzipedStream.Seek(0, SeekOrigin.Begin);
340-
return Deserialize(unzipedStream);
341-
}
342-
}
343-
finally
344-
{
345-
Marshal.FreeHGlobal(unmanagePtr);
346-
}
347-
}
321+
return Deserialize(bufferedStream);
348322
}
349323

350324
#endregion Private 方法

test/LanguageIdentification.TestConsole/Program.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics;
23

34
namespace LanguageIdentification.TestConsole
45
{
@@ -22,9 +23,13 @@ C. Compartimiento/Costes .......................................................
2223

2324
static void Main(string[] args)
2425
{
25-
AppContext.SetSwitch("LanguageIdentification:TryLoadModelWithLowMemory", false);
26+
var stopwatch = Stopwatch.StartNew();
2627

2728
var langIdClassifier = new LanguageIdentificationClassifier();
29+
30+
stopwatch.Stop();
31+
Console.WriteLine($"Load Time: {stopwatch.ElapsedMilliseconds}");
32+
2833
langIdClassifier.Append(Text_ES);
2934
var result = langIdClassifier.Classify();
3035

0 commit comments

Comments
 (0)