Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce amount of heap allocations by changing List usage #365

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions WowPacketParser/Parsing/Parsers/NpcHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,10 @@ public static void HandleVendorInventoryList434(Packet packet)

guidBytes[4] = packet.ReadBit();

var tempList = new List<NpcVendor>();
var tempArray = new NpcVendor[count];
for (int i = 0; i < count; ++i)
{
NpcVendor npcVendor = new NpcVendor
var npcVendor = new NpcVendor
{
Slot = packet.ReadInt32("Item Position", i)
};
Expand All @@ -300,7 +300,7 @@ public static void HandleVendorInventoryList434(Packet packet)
if (npcVendor.Type == 2)
npcVendor.MaxCount = buyCount;

tempList.Add(npcVendor);
tempArray[i] = npcVendor;
}

packet.ReadXORByte(guidBytes, 5);
Expand All @@ -316,11 +316,11 @@ public static void HandleVendorInventoryList434(Packet packet)
packet.ReadXORByte(guidBytes, 7);

uint entry = packet.WriteGuid("GUID", guidBytes).GetEntry();
tempList.ForEach(v =>
for(int i = 0; i < count; ++i)
{
v.Entry = entry;
Storage.NpcVendors.Add(v, packet.TimeSpan);
});
tempArray[i].Entry = entry;
Storage.NpcVendors.Add(tempArray[i], packet.TimeSpan);
}
}

[Parser(Opcode.CMSG_GOSSIP_HELLO)]
Expand Down
2 changes: 1 addition & 1 deletion WowPacketParserModule.BattleNet.V37165/Parsers/Presence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void HandleUpdateNotify(BattlenetPacket packet)
varSizes.Enqueue(packet.Read<ushort>(0, 16));

count = packet.Read<int>(0, 4);
var handles = new List<uint>();
var handles = new List<uint>(count);
for (var i = 0; i < count; ++i)
handles.Add(packet.Read<uint>(0, 32));

Expand Down
28 changes: 15 additions & 13 deletions WowPacketParserModule.V5_3_0_16981/Parsers/NpcHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public static void HandleServerTrainerList(Packet packet)
packet.StartBitStream(guidBytes, 3, 7, 1, 4, 5);
packet.ResetBitReader();

var tempList = new List<TrainerSpell>();
var tempArray = new TrainerSpell[count];
for (int i = 0; i < count; ++i)
{
TrainerSpell trainerSpell = new TrainerSpell
Expand All @@ -184,7 +184,7 @@ public static void HandleServerTrainerList(Packet packet)
trainerSpell.SpellId = packet.ReadUInt32<SpellId>("SpellID", i);
packet.ReadByteE<TrainerSpellState>("Usable", i);

tempList.Add(trainerSpell);
tempArray[i] = trainerSpell;
}

Trainer trainer = new Trainer();
Expand All @@ -197,11 +197,13 @@ public static void HandleServerTrainerList(Packet packet)

packet.WriteGuid("TrainerGUID", guidBytes);
Storage.Trainers.Add(trainer, packet.TimeSpan);
tempList.ForEach(trainerSpell =>

for(int i = 0; i < count; ++i)
{
trainerSpell.TrainerId = trainer.Id;
Storage.TrainerSpells.Add(trainerSpell, packet.TimeSpan);
});
tempArray[i].TrainerId = trainer.Id;
Storage.TrainerSpells.Add(tempArray[i], packet.TimeSpan);
}

var lastGossipOption = CoreParsers.NpcHandler.LastGossipOption;
if (lastGossipOption.HasSelection)
Storage.GossipMenuOptionTrainers.Add(new GossipMenuOptionTrainer { MenuId = lastGossipOption.MenuId, OptionIndex = lastGossipOption.OptionIndex, TrainerId = trainer.Id }, packet.TimeSpan);
Expand Down Expand Up @@ -230,10 +232,10 @@ public static void HandleVendorInventoryList(Packet packet)
packet.ResetBitReader();
packet.ReadXORBytes(guid, 3, 4);

var tempList = new List<NpcVendor>();
var tempArray = new NpcVendor[itemCount];
for (int i = 0; i < itemCount; ++i)
{
NpcVendor vendor = new NpcVendor
var vendor = new NpcVendor
{
Item = packet.ReadInt32<ItemId>("Item ID", i),
Slot = packet.ReadInt32("Item Position", i)
Expand All @@ -257,19 +259,19 @@ public static void HandleVendorInventoryList(Packet packet)
if (vendor.Type == 2)
vendor.MaxCount = buyCount;

tempList.Add(vendor);
tempArray[i] = vendor;
}

packet.ReadXORBytes(guid, 1, 2, 7);
packet.ReadByte("Unk Byte");
packet.ReadXORBytes(guid, 6, 0, 5);

uint entry = packet.WriteGuid("GUID", guid).GetEntry();
tempList.ForEach(v =>
for(int i = 0; i < itemCount; ++i)
{
v.Entry = entry;
Storage.NpcVendors.Add(v, packet.TimeSpan);
});
tempArray[i].Entry = entry;
Storage.NpcVendors.Add(tempArray[i], packet.TimeSpan);
}
}
}
}
16 changes: 7 additions & 9 deletions WowPacketParserModule.V5_4_0_17359/Parsers/NpcHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,6 @@ public static void HandleThreatlistUpdate(Packet packet)
[Parser(Opcode.SMSG_VENDOR_INVENTORY)]
public static void HandleVendorInventoryList(Packet packet)
{


var guid = new byte[8];

guid[5] = packet.ReadBit();
Expand All @@ -305,10 +303,10 @@ public static void HandleVendorInventoryList(Packet packet)
packet.ReadXORByte(guid, 6);
packet.ReadXORByte(guid, 1);

var tempList = new List<NpcVendor>();
var tempArray = new NpcVendor[count];
for (int i = 0; i < count; ++i)
{
NpcVendor vendor = new NpcVendor();
var vendor = new NpcVendor();

int maxCount = packet.ReadInt32("Max Count", i);
vendor.Type = packet.ReadUInt32("Type", i); // 1 - item, 2 - currency
Expand All @@ -331,7 +329,7 @@ public static void HandleVendorInventoryList(Packet packet)
if (vendor.Type == 2)
vendor.MaxCount = buyCount;

tempList.Add(vendor);
tempArray[i] = vendor;
}

packet.ReadXORByte(guid, 2);
Expand All @@ -343,11 +341,11 @@ public static void HandleVendorInventoryList(Packet packet)
packet.ReadXORByte(guid, 3);

uint entry = packet.WriteGuid("GUID", guid).GetEntry();
tempList.ForEach(v =>
for(int i = 0; i < count; ++i)
{
v.Entry = entry;
Storage.NpcVendors.Add(v, packet.TimeSpan);
});
tempArray[i].Entry = entry;
Storage.NpcVendors.Add(tempArray[i], packet.TimeSpan);
}
}

[Parser(Opcode.SMSG_TRAINER_LIST)]
Expand Down
28 changes: 15 additions & 13 deletions WowPacketParserModule.V5_4_1_17538/Parsers/NpcHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,10 @@ public static void HandleVendorInventoryList(Packet packet)
guid[6] = packet.ReadBit();
guid[7] = packet.ReadBit();

var tempList = new List<NpcVendor>();
var tempArray = new NpcVendor[count];
for (int i = 0; i < count; ++i)
{
NpcVendor vendor = new NpcVendor();
var vendor = new NpcVendor();

vendor.Item = packet.ReadInt32<ItemId>("Item ID", i);

Expand All @@ -253,17 +253,17 @@ public static void HandleVendorInventoryList(Packet packet)
packet.ReadInt32("Item Upgrade ID", i);
packet.ReadInt32("Max Durability", i);

tempList.Add(vendor);
tempArray[i] = vendor;
}

packet.ParseBitStream(guid, 0, 2, 1, 3, 5, 7, 4, 6);

uint entry = packet.WriteGuid("GUID", guid).GetEntry();
tempList.ForEach(v =>
for(int i = 0; i < count; ++i)
{
v.Entry = entry;
Storage.NpcVendors.Add(v, packet.TimeSpan);
});
tempArray[i].Entry = entry;
Storage.NpcVendors.Add(tempArray[i], packet.TimeSpan);
}
}

[Parser(Opcode.SMSG_TRAINER_LIST)]
Expand All @@ -285,7 +285,7 @@ public static void HandleServerTrainerList(Packet packet)
guid[2] = packet.ReadBit();
guid[1] = packet.ReadBit();

var tempList = new List<TrainerSpell>();
var tempArray = new TrainerSpell[count];
for (int i = 0; i < count; ++i)
{
TrainerSpell trainerSpell = new TrainerSpell
Expand All @@ -303,7 +303,7 @@ public static void HandleServerTrainerList(Packet packet)
trainerSpell.MoneyCost = packet.ReadUInt32("MoneyCost", i);
trainerSpell.ReqSkillLine = packet.ReadUInt32("ReqSkillLine", i);

tempList.Add(trainerSpell);
tempArray[i] = trainerSpell;
}

packet.ReadXORByte(guid, 4);
Expand All @@ -325,11 +325,13 @@ public static void HandleServerTrainerList(Packet packet)

packet.WriteGuid("TrainerGUID", guid);
Storage.Trainers.Add(trainer, packet.TimeSpan);
tempList.ForEach(trainerSpell =>

for(int i = 0; i < count; ++i)
{
trainerSpell.TrainerId = trainer.Id;
Storage.TrainerSpells.Add(trainerSpell, packet.TimeSpan);
});
tempArray[i].TrainerId = trainer.Id;
Storage.TrainerSpells.Add(tempArray[i], packet.TimeSpan);
}

var lastGossipOption = CoreParsers.NpcHandler.LastGossipOption;
if (lastGossipOption.HasSelection)
Storage.GossipMenuOptionTrainers.Add(new GossipMenuOptionTrainer { MenuId = lastGossipOption.MenuId, OptionIndex = lastGossipOption.OptionIndex, TrainerId = trainer.Id }, packet.TimeSpan);
Expand Down
28 changes: 15 additions & 13 deletions WowPacketParserModule.V5_4_2_17658/Parsers/NpcHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,10 @@ public static void HandleVendorInventoryList(Packet packet)
packet.ReadXORByte(guid, 7);
packet.ReadXORByte(guid, 6);

var tempList = new List<NpcVendor>();
var tempArray = new NpcVendor[count];
for (int i = 0; i < count; ++i)
{
NpcVendor vendor = new NpcVendor
var vendor = new NpcVendor
{
Type = packet.ReadUInt32("Type", i)
};
Expand All @@ -303,7 +303,7 @@ public static void HandleVendorInventoryList(Packet packet)
if (hasCondition[i])
vendor.PlayerConditionID = packet.ReadUInt32("Condition ID", i);

tempList.Add(vendor);
tempArray[i] = vendor;
}

packet.ReadByte("Byte28");
Expand All @@ -318,11 +318,11 @@ public static void HandleVendorInventoryList(Packet packet)
packet.WriteGuid("Guid", guid);

uint entry = packet.WriteGuid("GUID", guid).GetEntry();
tempList.ForEach(v =>
for(int i = 0; i < count; ++i)
{
v.Entry = entry;
Storage.NpcVendors.Add(v, packet.TimeSpan);
});
tempArray[i].Entry = entry;
Storage.NpcVendors.Add(tempArray[i], packet.TimeSpan);
}
}

[Parser(Opcode.SMSG_TRAINER_LIST)]
Expand All @@ -349,7 +349,7 @@ public static void HandleServerTrainerList(Packet packet)
Type = packet.ReadUInt32E<TrainerType>("TrainerType")
};

var tempList = new List<TrainerSpell>();
var tempArray = new TrainerSpell[count];
for (int i = 0; i < count; ++i)
{
TrainerSpell trainerSpell = new TrainerSpell
Expand All @@ -366,7 +366,7 @@ public static void HandleServerTrainerList(Packet packet)
trainerSpell.ReqLevel = packet.ReadByte("ReqLevel", i);
trainerSpell.ReqSkillRank = packet.ReadUInt32("ReqSkillRank", i);

tempList.Add(trainerSpell);
tempArray[i] = trainerSpell;
}

packet.ReadXORByte(guidBytes, 7);
Expand All @@ -386,11 +386,13 @@ public static void HandleServerTrainerList(Packet packet)

packet.WriteGuid("TrainerGUID", guidBytes);
Storage.Trainers.Add(trainer, packet.TimeSpan);
tempList.ForEach(trainerSpell =>

for(int i = 0; i < count; ++i)
{
trainerSpell.TrainerId = trainer.Id;
Storage.TrainerSpells.Add(trainerSpell, packet.TimeSpan);
});
tempArray[i].TrainerId = trainer.Id;
Storage.TrainerSpells.Add(tempArray[i], packet.TimeSpan);
}

var lastGossipOption = CoreParsers.NpcHandler.LastGossipOption;
if (lastGossipOption.HasSelection)
Storage.GossipMenuOptionTrainers.Add(new GossipMenuOptionTrainer { MenuId = lastGossipOption.MenuId, OptionIndex = lastGossipOption.OptionIndex, TrainerId = trainer.Id }, packet.TimeSpan);
Expand Down
30 changes: 16 additions & 14 deletions WowPacketParserModule.V5_4_7_17898/Parsers/NpcHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,11 @@ public static void HandleServerTrainerList(Packet packet)

packet.ReadXORByte(guidBytes, 3);

var tempList = new List<TrainerSpell>();
var tempArray = new TrainerSpell[count];
for (int i = 0; i < count; ++i)
{
packet.ReadByteE<TrainerSpellState>("Usable", i);
TrainerSpell trainerSpell = new TrainerSpell
var trainerSpell = new TrainerSpell
{
SpellId = packet.ReadUInt32<SpellId>("SpellID", i),
ReqSkillLine = packet.ReadUInt32("ReqSkillLine", i),
Expand All @@ -276,7 +276,7 @@ public static void HandleServerTrainerList(Packet packet)
trainerSpell.ReqLevel = packet.ReadByte("ReqLevel", i);
trainerSpell.ReqSkillRank = packet.ReadUInt32("ReqSkillRank", i);

tempList.Add(trainerSpell);
tempArray[i] = trainerSpell;
}

Trainer trainer = new Trainer();
Expand All @@ -293,11 +293,13 @@ public static void HandleServerTrainerList(Packet packet)

packet.WriteGuid("TrainerGUID", guidBytes);
Storage.Trainers.Add(trainer, packet.TimeSpan);
tempList.ForEach(trainerSpell =>

for(int i = 0; i < count; ++i)
{
trainerSpell.TrainerId = trainer.Id;
Storage.TrainerSpells.Add(trainerSpell, packet.TimeSpan);
});
tempArray[i].TrainerId = trainer.Id;
Storage.TrainerSpells.Add(tempArray[i], packet.TimeSpan);
}

var lastGossipOption = CoreParsers.NpcHandler.LastGossipOption;
if (lastGossipOption.HasSelection)
Storage.GossipMenuOptionTrainers.Add(new GossipMenuOptionTrainer { MenuId = lastGossipOption.MenuId, OptionIndex = lastGossipOption.OptionIndex, TrainerId = trainer.Id }, packet.TimeSpan);
Expand Down Expand Up @@ -355,10 +357,10 @@ public static void HandleVendorInventoryList(Packet packet)

packet.ReadXORByte(guid, 3);

var tempList = new List<NpcVendor>();
var tempArray = new NpcVendor[count];
for (int i = 0; i < count; ++i)
{
NpcVendor vendor = new NpcVendor();
var vendor = new NpcVendor();
packet.ReadInt32("Max Durability", i);
vendor.Type = packet.ReadUInt32("Type", i); // 1 - item, 2 - currency
uint buyCount = packet.ReadUInt32("Buy Count", i);
Expand All @@ -381,7 +383,7 @@ public static void HandleVendorInventoryList(Packet packet)
packet.ReadInt32("Item Upgrade ID", i);
packet.ReadInt32("Display ID", i);

tempList.Add(vendor);
tempArray[i] = vendor;
}

packet.ReadXORByte(guid, 6);
Expand All @@ -394,11 +396,11 @@ public static void HandleVendorInventoryList(Packet packet)
packet.ReadXORByte(guid, 7);

uint entry = packet.WriteGuid("GUID", guid).GetEntry();
tempList.ForEach(v =>
for(int i = 0; i < count; ++i)
{
v.Entry = entry;
Storage.NpcVendors.Add(v, packet.TimeSpan);
});
tempArray[i].Entry = entry;
Storage.NpcVendors.Add(tempArray[i], packet.TimeSpan);
}
}

[Parser(Opcode.CMSG_BUY_BANK_SLOT)]
Expand Down
Loading