增加内存流池MemoryStreamPool,减少MemoryStream的分配和回收大石头 authored at 2018-06-12 14:47:11
diff --git a/NewLife.Core/Collections/IPool.cs b/NewLife.Core/Collections/IPool.cs
index aedc251..637ded4 100644
--- a/NewLife.Core/Collections/IPool.cs
+++ b/NewLife.Core/Collections/IPool.cs
@@ -33,10 +33,6 @@ namespace NewLife.Collections
/// <summary>字符串构建器池</summary>
public static IPool<StringBuilder> StringBuilder { get; set; } = new StringBuilderPool();
- ///// <summary>借助一个字符串构建器</summary>
- ///// <returns></returns>
- //public static StringBuilder GetStringBuilder() => Pool.StringBuilder.Get();
-
/// <summary>归还一个字符串构建器到对象池</summary>
/// <param name="sb"></param>
/// <param name="requireResult">是否需要返回结果</param>
@@ -98,7 +94,7 @@ namespace NewLife.Collections
return buf;
}
- /// <summary>内存流构建器池</summary>
+ /// <summary>内存流池</summary>
public class MemoryStreamPool : Pool<MemoryStream>
{
/// <summary>初始容量</summary>
diff --git a/NewLife.Core/Data/Packet.cs b/NewLife.Core/Data/Packet.cs
index 61d075b..8ae7a6d 100644
--- a/NewLife.Core/Data/Packet.cs
+++ b/NewLife.Core/Data/Packet.cs
@@ -2,6 +2,7 @@
using System.IO;
using System.Text;
using System.Threading.Tasks;
+using NewLife.Collections;
namespace NewLife.Data
{
@@ -203,10 +204,10 @@ namespace NewLife.Data
if (Next == null) Data.ReadBytes(Offset, Count);
// 链式包输出
- var ms = new MemoryStream();
+ var ms = Pool.MemoryStream.Get();
WriteTo(ms);
- return ms.ToArray();
+ return ms.Put(true);
}
/// <summary>从封包中读取指定数据</summary>
@@ -224,7 +225,7 @@ namespace NewLife.Data
// 链式包输出
if (count < 0) count = Total - offset;
- var ms = new MemoryStream();
+ var ms = Pool.MemoryStream.Get();
// 遍历
var cur = this;
@@ -246,7 +247,7 @@ namespace NewLife.Data
cur = cur.Next;
}
- return ms.ToArray();
+ return ms.Put(true);
//// 以上算法太复杂,直接来
//return ToArray().ReadBytes(offset, count);
diff --git a/NewLife.Core/IO/IOHelper.cs b/NewLife.Core/IO/IOHelper.cs
index dfd5a93..032f4ba 100644
--- a/NewLife.Core/IO/IOHelper.cs
+++ b/NewLife.Core/IO/IOHelper.cs
@@ -409,7 +409,7 @@ namespace System
// 如果要读完数据,又不支持定位,则采用内存流搬运
if (!stream.CanSeek)
{
- var ms = new MemoryStream();
+ var ms = Pool.MemoryStream.Get();
while (true)
{
var buf = new Byte[1024];
@@ -420,7 +420,7 @@ namespace System
if (count < buf.Length) break;
}
- return ms.ToArray();
+ return ms.Put(true);
}
else
{
diff --git a/NewLife.Core/Net/UdpSession.cs b/NewLife.Core/Net/UdpSession.cs
index d80894b..ea4aa94 100644
--- a/NewLife.Core/Net/UdpSession.cs
+++ b/NewLife.Core/Net/UdpSession.cs
@@ -28,8 +28,8 @@ namespace NewLife.Net
/// <summary>底层Socket</summary>
Socket ISocket.Client => Server?.Client;
- /// <summary>数据流</summary>
- public Stream Stream { get; set; }
+ ///// <summary>数据流</summary>
+ //public Stream Stream { get; set; }
private NetUri _Local;
/// <summary>本地地址</summary>
@@ -94,7 +94,7 @@ namespace NewLife.Net
public UdpSession(UdpServer server, IPEndPoint remote)
{
Name = server.Name;
- Stream = new MemoryStream();
+ //Stream = new MemoryStream();
StartTime = DateTime.Now;
Server = server;
diff --git a/NewLife.Core/Serialization/Binary/BinaryPair.cs b/NewLife.Core/Serialization/Binary/BinaryPair.cs
index b471f8f..2ce754b 100644
--- a/NewLife.Core/Serialization/Binary/BinaryPair.cs
+++ b/NewLife.Core/Serialization/Binary/BinaryPair.cs
@@ -6,6 +6,7 @@ using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml.Serialization;
+using NewLife.Collections;
using NewLife.Reflection;
namespace NewLife.Serialization
@@ -86,14 +87,14 @@ namespace NewLife.Serialization
else
{
// 准备好名值对再一起写入。为了得到数据长度,需要提前计算好数据长度,所以需要临时切换数据流
- var ms = new MemoryStream();
+ var ms = Pool.MemoryStream.Get();
var old = host.Stream;
host.Stream = ms;
var rs = host.Write(value, type);
host.Stream = old;
if (!rs) return false;
- buf = ms.ToArray();
+ buf = ms.Put(true);
}
WriteLog(" WritePair {0}\t= {1}", name, value);
diff --git a/NewLife.Core/Serialization/Binary/BinaryUnknown.cs b/NewLife.Core/Serialization/Binary/BinaryUnknown.cs
index 1bda34e..caf52ef 100644
--- a/NewLife.Core/Serialization/Binary/BinaryUnknown.cs
+++ b/NewLife.Core/Serialization/Binary/BinaryUnknown.cs
@@ -2,6 +2,7 @@
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
+using NewLife.Collections;
namespace NewLife.Serialization
{
@@ -33,11 +34,12 @@ namespace NewLife.Serialization
// 调用.Net的二进制序列化来解决剩下的事情
var bf = new BinaryFormatter();
- var ms = new MemoryStream();
+ var ms = Pool.MemoryStream.Get();
bf.Serialize(ms, value);
Host.WriteSize((Int32)ms.Length);
ms.CopyTo(Host.Stream);
+ ms.Put();
return true;
}
diff --git a/NewLife.Core/Web/WebClientX.cs b/NewLife.Core/Web/WebClientX.cs
index eee14d5..d66c260 100644
--- a/NewLife.Core/Web/WebClientX.cs
+++ b/NewLife.Core/Web/WebClientX.cs
@@ -176,7 +176,7 @@ namespace NewLife.Web
break;
}
- var ms = new MemoryStream();
+ var ms = NewLife.Collections.Pool.MemoryStream.Get();
var ns = rs.GetResponseStream();
ns.CopyTo(ms);
@@ -186,7 +186,7 @@ namespace NewLife.Web
ns.CopyTo(ms);
}
- return ms.ToArray();
+ return ms.Put(true);
}
}