提交 a2d8b564 编写于 作者: T tanghai

使用RecyclableMemoryStream消除解析protobuf消息new MemoryStream产生的gc

上级 d6e35fa5
......@@ -54,6 +54,9 @@
<Compile Include="..\..\Unity\Assets\Scripts\Base\Object\ObjectSystemAttribute.cs" Link="Base\Object\ObjectSystemAttribute.cs" />
<Compile Include="..\..\Unity\Assets\Scripts\Base\OneThreadSynchronizationContext.cs" Link="Base\OneThreadSynchronizationContext.cs" />
<Compile Include="..\..\Unity\Assets\Scripts\Base\QueueDictionary.cs" Link="Base\QueueDictionary.cs" />
<Compile Include="..\..\Unity\Assets\Scripts\Base\RecyclableMemoryStream\Events.cs" Link="Base\RecyclableMemoryStream\Events.cs" />
<Compile Include="..\..\Unity\Assets\Scripts\Base\RecyclableMemoryStream\RecyclableMemoryStream.cs" Link="Base\RecyclableMemoryStream\RecyclableMemoryStream.cs" />
<Compile Include="..\..\Unity\Assets\Scripts\Base\RecyclableMemoryStream\RecyclableMemoryStreamManager.cs" Link="Base\RecyclableMemoryStream\RecyclableMemoryStreamManager.cs" />
<Compile Include="..\..\Unity\Assets\Scripts\Base\TryLocker.cs" Link="Base\TryLocker.cs" />
<Compile Include="..\..\Unity\Assets\Scripts\Base\UnOrderMultiMap.cs" Link="Base\UnOrderMultiMap.cs" />
<Compile Include="..\..\Unity\Assets\Scripts\Component\Config\ClientConfig.cs" Link="Component\Config\ClientConfig.cs" />
......@@ -114,6 +117,7 @@
<ProjectReference Include="..\ThirdParty\MongodbDriver\DotNetCoreDriver\MongoDB.Driver\MongoDB.Driver.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Base\RecyclableMemoryStream\" />
<Folder Include="Component\Config\" />
<Folder Include="Module\Message\Network\KCP\" />
<Folder Include="Module\Message\Network\TCP\" />
......
using System;
using System.ComponentModel;
using System.IO;
using Microsoft.IO;
using ProtoBuf;
namespace ETModel
{
public static class ProtobufHelper
{
private static readonly RecyclableMemoryStreamManager recyclableMemoryStreamManager = new RecyclableMemoryStreamManager();
public static byte[] ToBytes(object message)
{
using (MemoryStream ms = new MemoryStream())
......@@ -19,7 +22,7 @@ namespace ETModel
public static T FromBytes<T>(byte[] bytes)
{
T t;
using (MemoryStream ms = new MemoryStream(bytes, 0, bytes.Length))
using (MemoryStream ms = recyclableMemoryStreamManager.GetStream("protobuf", bytes, 0, bytes.Length))
{
t = Serializer.Deserialize<T>(ms);
}
......@@ -35,7 +38,7 @@ namespace ETModel
public static T FromBytes<T>(byte[] bytes, int index, int length)
{
T t;
using (MemoryStream ms = new MemoryStream(bytes, index, length))
using (MemoryStream ms = recyclableMemoryStreamManager.GetStream("protobuf", bytes, index, length))
{
t = Serializer.Deserialize<T>(ms);
}
......@@ -51,7 +54,7 @@ namespace ETModel
public static object FromBytes(Type type, byte[] bytes)
{
object t;
using (MemoryStream ms = new MemoryStream(bytes, 0, bytes.Length))
using (MemoryStream ms = recyclableMemoryStreamManager.GetStream("protobuf", bytes, 0, bytes.Length))
{
t = Serializer.NonGeneric.Deserialize(type, ms);
}
......@@ -67,7 +70,7 @@ namespace ETModel
public static object FromBytes(Type type, byte[] bytes, int index, int length)
{
object t;
using (MemoryStream ms = new MemoryStream(bytes, index, length))
using (MemoryStream ms = recyclableMemoryStreamManager.GetStream("protobuf", bytes, index, length))
{
t = Serializer.NonGeneric.Deserialize(type, ms);
}
......
fileFormatVersion: 2
guid: 70be99fa9645c134cb2894b12b106b3f
folderAsset: yes
timeCreated: 1527234045
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
// ---------------------------------------------------------------------
// Copyright (c) 2015 Microsoft
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ---------------------------------------------------------------------
namespace Microsoft.IO
{
using System;
using System.Diagnostics.Tracing;
public sealed partial class RecyclableMemoryStreamManager
{
[EventSource(Name = "Microsoft-IO-RecyclableMemoryStream", Guid = "{B80CD4E4-890E-468D-9CBA-90EB7C82DFC7}")]
public sealed class Events : EventSource
{
public static Events Writer = new Events();
public enum MemoryStreamBufferType
{
Small,
Large
}
public enum MemoryStreamDiscardReason
{
TooLarge,
EnoughFree
}
[Event(1, Level = EventLevel.Verbose)]
public void MemoryStreamCreated(Guid guid, string tag, int requestedSize)
{
if (this.IsEnabled(EventLevel.Verbose, EventKeywords.None))
{
WriteEvent(1, guid, tag ?? string.Empty, requestedSize);
}
}
[Event(2, Level = EventLevel.Verbose)]
public void MemoryStreamDisposed(Guid guid, string tag)
{
if (this.IsEnabled(EventLevel.Verbose, EventKeywords.None))
{
WriteEvent(2, guid, tag ?? string.Empty);
}
}
[Event(3, Level = EventLevel.Critical)]
public void MemoryStreamDoubleDispose(Guid guid, string tag, string allocationStack, string disposeStack1,
string disposeStack2)
{
if (this.IsEnabled())
{
this.WriteEvent(3, guid, tag ?? string.Empty, allocationStack ?? string.Empty,
disposeStack1 ?? string.Empty, disposeStack2 ?? string.Empty);
}
}
[Event(4, Level = EventLevel.Error)]
public void MemoryStreamFinalized(Guid guid, string tag, string allocationStack)
{
if (this.IsEnabled())
{
WriteEvent(4, guid, tag ?? string.Empty, allocationStack ?? string.Empty);
}
}
[Event(5, Level = EventLevel.Verbose)]
public void MemoryStreamToArray(Guid guid, string tag, string stack, int size)
{
if (this.IsEnabled(EventLevel.Verbose, EventKeywords.None))
{
WriteEvent(5, guid, tag ?? string.Empty, stack ?? string.Empty, size);
}
}
[Event(6, Level = EventLevel.Informational)]
public void MemoryStreamManagerInitialized(int blockSize, int largeBufferMultiple, int maximumBufferSize)
{
if (this.IsEnabled())
{
WriteEvent(6, blockSize, largeBufferMultiple, maximumBufferSize);
}
}
[Event(7, Level = EventLevel.Verbose)]
public void MemoryStreamNewBlockCreated(long smallPoolInUseBytes)
{
if (this.IsEnabled(EventLevel.Verbose, EventKeywords.None))
{
WriteEvent(7, smallPoolInUseBytes);
}
}
[Event(8, Level = EventLevel.Verbose)]
public void MemoryStreamNewLargeBufferCreated(int requiredSize, long largePoolInUseBytes)
{
if (this.IsEnabled(EventLevel.Verbose, EventKeywords.None))
{
WriteEvent(8, requiredSize, largePoolInUseBytes);
}
}
[Event(9, Level = EventLevel.Verbose)]
public void MemoryStreamNonPooledLargeBufferCreated(int requiredSize, string tag, string allocationStack)
{
if (this.IsEnabled(EventLevel.Verbose, EventKeywords.None))
{
WriteEvent(9, requiredSize, tag ?? string.Empty, allocationStack ?? string.Empty);
}
}
[Event(10, Level = EventLevel.Warning)]
public void MemoryStreamDiscardBuffer(MemoryStreamBufferType bufferType, string tag,
MemoryStreamDiscardReason reason)
{
if (this.IsEnabled())
{
WriteEvent(10, bufferType, tag ?? string.Empty, reason);
}
}
[Event(11, Level = EventLevel.Error)]
public void MemoryStreamOverCapacity(int requestedCapacity, long maxCapacity, string tag,
string allocationStack)
{
if (this.IsEnabled())
{
WriteEvent(11, requestedCapacity, maxCapacity, tag ?? string.Empty, allocationStack ?? string.Empty);
}
}
}
}
}
// This is here for .NET frameworks which don't support EventSource. We basically shim bare functionality used above to
#if NET40
namespace System.Diagnostics.Tracing
{
public enum EventLevel
{
LogAlways = 0,
Critical,
Error,
Warning,
Informational,
Verbose,
}
public enum EventKeywords : long
{
None = 0x0,
}
[AttributeUsage(AttributeTargets.Class)]
public sealed class EventSourceAttribute : Attribute
{
public string Name { get; set; }
public string Guid { get; set; }
}
[AttributeUsage(AttributeTargets.Method)]
public sealed class EventAttribute : Attribute
{
public EventAttribute(int id) { }
public EventLevel Level { get; set; }
}
public class EventSource
{
public void WriteEvent(params object[] unused)
{
return;
}
public bool IsEnabled()
{
return false;
}
public bool IsEnabled(EventLevel level, EventKeywords keywords)
{
return false;
}
}
}
#endif
fileFormatVersion: 2
guid: 52cd395d4cf275c48b1f8690689d3869
timeCreated: 1527234045
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 04ecc2633820de7459b8ba26a114f62f
timeCreated: 1527234045
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: e7f5123bf8a488045a8e48c49b605cc9
timeCreated: 1527234045
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -12,16 +12,13 @@
<ProjectTypeGuids>{E097FAD1-6243-4DAD-9C02-E9B9EFC3FFC1};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<TargetFrameworkIdentifier>.NETFramework</TargetFrameworkIdentifier>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<TargetFrameworkProfile>
</TargetFrameworkProfile>
<CompilerResponseFile>
</CompilerResponseFile>
<TargetFrameworkProfile></TargetFrameworkProfile>
<CompilerResponseFile></CompilerResponseFile>
<UnityProjectGenerator>VSTU</UnityProjectGenerator>
<UnityProjectType>Game:1</UnityProjectType>
<UnityBuildTarget>StandaloneWindows:5</UnityBuildTarget>
<UnityVersion>2017.1.3p2</UnityVersion>
<RootNamespace>
</RootNamespace>
<RootNamespace></RootNamespace>
<LangVersion>6</LangVersion>
</PropertyGroup>
<PropertyGroup>
......@@ -170,6 +167,9 @@
<Compile Include="Assets\Scripts\Base\Object\ObjectSystemAttribute.cs" />
<Compile Include="Assets\Scripts\Base\OneThreadSynchronizationContext.cs" />
<Compile Include="Assets\Scripts\Base\QueueDictionary.cs" />
<Compile Include="Assets\Scripts\Base\RecyclableMemoryStream\Events.cs" />
<Compile Include="Assets\Scripts\Base\RecyclableMemoryStream\RecyclableMemoryStream.cs" />
<Compile Include="Assets\Scripts\Base\RecyclableMemoryStream\RecyclableMemoryStreamManager.cs" />
<Compile Include="Assets\Scripts\Base\TryLocker.cs" />
<Compile Include="Assets\Scripts\Base\UnOrderMultiMap.cs" />
<Compile Include="Assets\Scripts\BehaviorTreeNode\CreateUIEffect.cs" />
......@@ -779,4 +779,4 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="GenerateTargetFrameworkMonikerAttribute" />
</Project>
\ No newline at end of file
</Project>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册