Fix the issue that stopped the packet pool from working. Add a mechanism

to recycley data blocs within a packet. Recycle the ObjectUpdate* data
blocks. Speeds up loading even more.
This may mean that the packet pool is now viable.
0.6.5-rc1
Melanie Thielker 2009-05-02 00:14:04 +00:00
parent 0f721da5f1
commit ac944def3f
4 changed files with 77 additions and 10 deletions

View File

@ -34,6 +34,7 @@ using log4net;
namespace OpenSim.Framework namespace OpenSim.Framework
{ {
public sealed class PacketPool public sealed class PacketPool
{ {
private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
@ -44,6 +45,9 @@ namespace OpenSim.Framework
private readonly Dictionary<PacketType, Stack<Packet>> pool = new Dictionary<PacketType, Stack<Packet>>(); private readonly Dictionary<PacketType, Stack<Packet>> pool = new Dictionary<PacketType, Stack<Packet>>();
private static Dictionary<Type, List<Object>> DataBlocks =
new Dictionary<Type, List<Object>>();
static PacketPool() static PacketPool()
{ {
} }
@ -136,6 +140,28 @@ namespace OpenSim.Framework
/// <param name="packet"></param> /// <param name="packet"></param>
public void ReturnPacket(Packet packet) public void ReturnPacket(Packet packet)
{ {
switch (packet.Type)
{
case PacketType.ObjectUpdate:
ObjectUpdatePacket oup = (ObjectUpdatePacket)packet;
foreach (ObjectUpdatePacket.ObjectDataBlock oupod in
oup.ObjectData)
ReturnDataBlock<ObjectUpdatePacket.ObjectDataBlock>(oupod);
oup.ObjectData = null;
break;
case PacketType.ImprovedTerseObjectUpdate:
ImprovedTerseObjectUpdatePacket itoup =
(ImprovedTerseObjectUpdatePacket)packet;
foreach (ImprovedTerseObjectUpdatePacket.ObjectDataBlock
itoupod in itoup.ObjectData)
ReturnDataBlock<ImprovedTerseObjectUpdatePacket.ObjectDataBlock>(itoupod);
itoup.ObjectData = null;
break;
}
if (!packetPoolEnabled) if (!packetPoolEnabled)
return; return;
@ -163,5 +189,45 @@ namespace OpenSim.Framework
return; return;
} }
} }
public static T GetDataBlock<T>() where T: new()
{
lock(DataBlocks)
{
if (DataBlocks.ContainsKey(typeof(T)) && DataBlocks[typeof(T)].Count > 0)
{
T block = (T)DataBlocks[typeof(T)][0];
DataBlocks[typeof(T)].RemoveAt(0);
if (block == null)
return new T();
return block;
}
else
{
return new T();
}
}
}
public static void ReturnDataBlock<T>(T block) where T: new()
{
if (block == null)
return;
lock (DataBlocks)
{
if (!DataBlocks.ContainsKey(typeof(T)))
{
List<Object> l = new List<Object>();
l.Add(block);
DataBlocks.Add(typeof(T), l);
}
else
{
if (DataBlocks[typeof(T)].Count < 500)
DataBlocks[typeof(T)].Add(block);
}
}
}
} }
} }

View File

@ -2893,10 +2893,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP
if (rotation.X == rotation.Y && rotation.Y == rotation.Z && rotation.Z == rotation.W && rotation.W == 0) if (rotation.X == rotation.Y && rotation.Y == rotation.Z && rotation.Z == rotation.W && rotation.W == 0)
rotation = Quaternion.Identity; rotation = Quaternion.Identity;
ObjectUpdatePacket.ObjectDataBlock objectData = ObjectUpdatePacket.ObjectDataBlock objectData = CreatePrimUpdateBlock(primShape, flags);
new ObjectUpdatePacket.ObjectDataBlock();
objectData = CreatePrimUpdateBlock(primShape, flags);
objectData.ID = localID; objectData.ID = localID;
objectData.FullID = objectID; objectData.FullID = objectID;
@ -3655,7 +3652,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP
{ {
byte[] bytes = new byte[60]; byte[] bytes = new byte[60];
int i = 0; int i = 0;
ImprovedTerseObjectUpdatePacket.ObjectDataBlock dat = new ImprovedTerseObjectUpdatePacket.ObjectDataBlock(); ImprovedTerseObjectUpdatePacket.ObjectDataBlock dat = PacketPool.GetDataBlock<ImprovedTerseObjectUpdatePacket.ObjectDataBlock>();
dat.TextureEntry = new byte[0]; // AvatarTemplate.TextureEntry; dat.TextureEntry = new byte[0]; // AvatarTemplate.TextureEntry;
@ -3750,7 +3747,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP
byte[] bytes = new byte[60]; byte[] bytes = new byte[60];
int i = 0; int i = 0;
ImprovedTerseObjectUpdatePacket.ObjectDataBlock dat = new ImprovedTerseObjectUpdatePacket.ObjectDataBlock(); ImprovedTerseObjectUpdatePacket.ObjectDataBlock dat = PacketPool.GetDataBlock<ImprovedTerseObjectUpdatePacket.ObjectDataBlock>();
dat.TextureEntry = new byte[0]; dat.TextureEntry = new byte[0];
bytes[i++] = (byte)(ID % 256); bytes[i++] = (byte)(ID % 256);
bytes[i++] = (byte)((ID >> 8) % 256); bytes[i++] = (byte)((ID >> 8) % 256);
@ -3837,7 +3834,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP
/// <returns></returns> /// <returns></returns>
protected ObjectUpdatePacket.ObjectDataBlock CreatePrimUpdateBlock(PrimitiveBaseShape primShape, uint flags) protected ObjectUpdatePacket.ObjectDataBlock CreatePrimUpdateBlock(PrimitiveBaseShape primShape, uint flags)
{ {
ObjectUpdatePacket.ObjectDataBlock objupdate = new ObjectUpdatePacket.ObjectDataBlock(); ObjectUpdatePacket.ObjectDataBlock objupdate = PacketPool.GetDataBlock<ObjectUpdatePacket.ObjectDataBlock>();
SetDefaultPrimPacketValues(objupdate); SetDefaultPrimPacketValues(objupdate);
objupdate.UpdateFlags = flags; objupdate.UpdateFlags = flags;
SetPrimPacketShapeData(objupdate, primShape); SetPrimPacketShapeData(objupdate, primShape);
@ -3908,7 +3905,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP
/// <returns></returns> /// <returns></returns>
public ObjectUpdatePacket.ObjectDataBlock CreateDefaultAvatarPacket(byte[] textureEntry) public ObjectUpdatePacket.ObjectDataBlock CreateDefaultAvatarPacket(byte[] textureEntry)
{ {
ObjectUpdatePacket.ObjectDataBlock objdata = new ObjectUpdatePacket.ObjectDataBlock(); ObjectUpdatePacket.ObjectDataBlock objdata = PacketPool.GetDataBlock<ObjectUpdatePacket.ObjectDataBlock>();
// new OpenMetaverse.Packets.ObjectUpdatePacket.ObjectDataBlock(data1, ref i); // new OpenMetaverse.Packets.ObjectUpdatePacket.ObjectDataBlock(data1, ref i);
SetDefaultAvatarPacketValues(ref objdata); SetDefaultAvatarPacketValues(ref objdata);

View File

@ -586,7 +586,11 @@ namespace OpenSim.Region.ClientStack.LindenUDP
return; return;
m_NeedAck.Remove(id); m_NeedAck.Remove(id);
PacketPool.Instance.ReturnPacket(data.Packet); // We can't return this packet, it will just have to be GC'd
// Reason for that is that the packet may still be in the
// send queue, and if it gets reused things get messy!
//
// PacketPool.Instance.ReturnPacket(data.Packet);
m_UnackedBytes -= data.Length; m_UnackedBytes -= data.Length;
} }
} }

View File

@ -129,7 +129,7 @@ namespace OpenSim.Region.CoreModules.Avatar.MuteList
private void OnMuteListRequest(IClientAPI client, uint crc) private void OnMuteListRequest(IClientAPI client, uint crc)
{ {
m_log.DebugFormat("[MUTE LIST] Got mute list requestg for crc {0}", crc); m_log.DebugFormat("[MUTE LIST] Got mute list request for crc {0}", crc);
string filename = "mutes"+client.AgentId.ToString(); string filename = "mutes"+client.AgentId.ToString();
IXfer xfer = client.Scene.RequestModuleInterface<IXfer>(); IXfer xfer = client.Scene.RequestModuleInterface<IXfer>();