From fc861c7904840b2b0b9de0621e9b5d976c8071b1 Mon Sep 17 00:00:00 2001
From: Justin Clark-Casey (justincc)
Date: Tue, 16 Oct 2012 23:35:05 +0100
Subject: Add optional pool for the UDPPacketBuffer objects that handle all
incoming UDP data.
Even when an avatar is standing still, it's sending in a constant stream of AgentUpdate packets that the client creates new UDPPacketBuffer objects to handle.
This option pools those objects. This reduces memory churn.
Currently off by default. Works but the scope can be expanded.
---
.../ClientStack/Linden/UDP/OpenSimUDPBase.cs | 31 +++++++++++++++++-----
1 file changed, 25 insertions(+), 6 deletions(-)
(limited to 'OpenSim/Region/ClientStack/Linden/UDP/OpenSimUDPBase.cs')
diff --git a/OpenSim/Region/ClientStack/Linden/UDP/OpenSimUDPBase.cs b/OpenSim/Region/ClientStack/Linden/UDP/OpenSimUDPBase.cs
index 828c23c..6e6b3ef 100644
--- a/OpenSim/Region/ClientStack/Linden/UDP/OpenSimUDPBase.cs
+++ b/OpenSim/Region/ClientStack/Linden/UDP/OpenSimUDPBase.cs
@@ -30,6 +30,7 @@ using System.Net;
using System.Net.Sockets;
using System.Threading;
using log4net;
+using OpenSim.Framework;
namespace OpenMetaverse
{
@@ -58,6 +59,16 @@ namespace OpenMetaverse
/// Flag to process packets asynchronously or synchronously
private bool m_asyncPacketHandling;
+ ///
+ /// Pool to use for handling data. May be null if UsePools = false;
+ ///
+ protected OpenSim.Framework.Pool m_pool;
+
+ ///
+ /// Are we to use object pool(s) to reduce memory churn when receiving data?
+ ///
+ public bool UsePools { get; protected set; }
+
/// Returns true if the server is currently listening for inbound packets, otherwise false
public bool IsRunningInbound { get; private set; }
@@ -70,6 +81,7 @@ namespace OpenMetaverse
///
/// Local IP address to bind the server to
/// Port to listening for incoming UDP packets on
+ /// /// Are we to use an object pool to get objects for handing inbound data?
public OpenSimUDPBase(IPAddress bindAddress, int port)
{
m_localBindAddress = bindAddress;
@@ -94,6 +106,11 @@ namespace OpenMetaverse
/// necessary
public void StartInbound(int recvBufferSize, bool asyncPacketHandling)
{
+ if (UsePools)
+ m_pool = new Pool(() => new UDPPacketBuffer(), 500);
+ else
+ m_pool = null;
+
m_asyncPacketHandling = asyncPacketHandling;
if (!IsRunningInbound)
@@ -165,9 +182,12 @@ namespace OpenMetaverse
private void AsyncBeginReceive()
{
- // allocate a packet buffer
- //WrappedObject wrappedBuffer = Pool.CheckOut();
- UDPPacketBuffer buf = new UDPPacketBuffer();
+ UDPPacketBuffer buf;
+
+ if (UsePools)
+ buf = m_pool.GetObject();
+ else
+ buf = new UDPPacketBuffer();
if (IsRunningInbound)
{
@@ -231,8 +251,6 @@ namespace OpenMetaverse
// get the buffer that was created in AsyncBeginReceive
// this is the received data
- //WrappedObject wrappedBuffer = (WrappedObject)iar.AsyncState;
- //UDPPacketBuffer buffer = wrappedBuffer.Instance;
UDPPacketBuffer buffer = (UDPPacketBuffer)iar.AsyncState;
try
@@ -249,7 +267,8 @@ namespace OpenMetaverse
catch (ObjectDisposedException) { }
finally
{
- //wrappedBuffer.Dispose();
+ if (UsePools)
+ m_pool.ReturnObject(buffer);
// Synchronous mode waits until the packet callback completes
// before starting the receive to fetch another packet
--
cgit v1.1