chore: Remove unnecessary heap allocations (#1478)

This commit is contained in:
jadebenn 2024-02-25 19:35:07 -06:00 committed by GitHub
parent 192c8cf974
commit 95d687846a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 42 deletions

View File

@ -243,13 +243,13 @@ bool BehaviorContext::CalculateUpdate(const float deltaTime) {
echo.uiBehaviorHandle = entry.handle; echo.uiBehaviorHandle = entry.handle;
echo.uiSkillHandle = this->skillUId; echo.uiSkillHandle = this->skillUId;
auto* bitStream = new RakNet::BitStream(); RakNet::BitStream bitStream{};
// Calculate sync // Calculate sync
entry.behavior->SyncCalculation(this, bitStream, entry.branchContext); entry.behavior->SyncCalculation(this, &bitStream, entry.branchContext);
if (!clientInitalized) { if (!clientInitalized) {
echo.sBitStream.assign(reinterpret_cast<char*>(bitStream->GetData()), bitStream->GetNumberOfBytesUsed()); echo.sBitStream.assign(reinterpret_cast<char*>(bitStream.GetData()), bitStream.GetNumberOfBytesUsed());
// Write message // Write message
RakNet::BitStream message; RakNet::BitStream message;
@ -262,8 +262,6 @@ bool BehaviorContext::CalculateUpdate(const float deltaTime) {
} }
ExecuteUpdates(); ExecuteUpdates();
delete bitStream;
} }
std::vector<BehaviorSyncEntry> valid; std::vector<BehaviorSyncEntry> valid;

View File

@ -252,7 +252,7 @@ bool SkillComponent::CastSkill(const uint32_t skillId, LWOOBJID target, const LW
SkillExecutionResult SkillComponent::CalculateBehavior(const uint32_t skillId, const uint32_t behaviorId, const LWOOBJID target, const bool ignoreTarget, const bool clientInitalized, const LWOOBJID originatorOverride) { SkillExecutionResult SkillComponent::CalculateBehavior(const uint32_t skillId, const uint32_t behaviorId, const LWOOBJID target, const bool ignoreTarget, const bool clientInitalized, const LWOOBJID originatorOverride) {
auto* bitStream = new RakNet::BitStream(); RakNet::BitStream bitStream{};
auto* behavior = Behavior::CreateBehavior(behaviorId); auto* behavior = Behavior::CreateBehavior(behaviorId);
@ -266,14 +266,13 @@ SkillExecutionResult SkillComponent::CalculateBehavior(const uint32_t skillId, c
context->foundTarget = target != LWOOBJID_EMPTY || ignoreTarget || clientInitalized; context->foundTarget = target != LWOOBJID_EMPTY || ignoreTarget || clientInitalized;
behavior->Calculate(context, bitStream, { target, 0 }); behavior->Calculate(context, &bitStream, { target, 0 });
for (auto* script : CppScripts::GetEntityScripts(m_Parent)) { for (auto* script : CppScripts::GetEntityScripts(m_Parent)) {
script->OnSkillCast(m_Parent, skillId); script->OnSkillCast(m_Parent, skillId);
} }
if (!context->foundTarget) { if (!context->foundTarget) {
delete bitStream;
delete context; delete context;
// Invalid attack // Invalid attack
@ -299,7 +298,7 @@ SkillExecutionResult SkillComponent::CalculateBehavior(const uint32_t skillId, c
} }
//start.optionalTargetID = target; //start.optionalTargetID = target;
start.sBitStream.assign(reinterpret_cast<char*>(bitStream->GetData()), bitStream->GetNumberOfBytesUsed()); start.sBitStream.assign(reinterpret_cast<char*>(bitStream.GetData()), bitStream.GetNumberOfBytesUsed());
// Write message // Write message
RakNet::BitStream message; RakNet::BitStream message;
@ -313,8 +312,6 @@ SkillExecutionResult SkillComponent::CalculateBehavior(const uint32_t skillId, c
context->ExecuteUpdates(); context->ExecuteUpdates();
delete bitStream;
// Valid attack // Valid attack
return { true, context->skillTime }; return { true, context->skillTime };
} }
@ -424,13 +421,13 @@ void SkillComponent::SyncProjectileCalculation(const ProjectileSyncEntry& entry)
auto* behavior = Behavior::CreateBehavior(behaviorId); auto* behavior = Behavior::CreateBehavior(behaviorId);
auto* bitStream = new RakNet::BitStream(); RakNet::BitStream bitStream{};
behavior->Calculate(entry.context, bitStream, entry.branchContext); behavior->Calculate(entry.context, &bitStream, entry.branchContext);
DoClientProjectileImpact projectileImpact; DoClientProjectileImpact projectileImpact;
projectileImpact.sBitStream.assign(reinterpret_cast<char*>(bitStream->GetData()), bitStream->GetNumberOfBytesUsed()); projectileImpact.sBitStream.assign(reinterpret_cast<char*>(bitStream.GetData()), bitStream.GetNumberOfBytesUsed());
projectileImpact.i64OwnerID = this->m_Parent->GetObjectID(); projectileImpact.i64OwnerID = this->m_Parent->GetObjectID();
projectileImpact.i64OrgID = entry.id; projectileImpact.i64OrgID = entry.id;
projectileImpact.i64TargetID = entry.branchContext.target; projectileImpact.i64TargetID = entry.branchContext.target;
@ -444,37 +441,29 @@ void SkillComponent::SyncProjectileCalculation(const ProjectileSyncEntry& entry)
Game::server->Send(&message, UNASSIGNED_SYSTEM_ADDRESS, true); Game::server->Send(&message, UNASSIGNED_SYSTEM_ADDRESS, true);
entry.context->ExecuteUpdates(); entry.context->ExecuteUpdates();
delete bitStream;
} }
void SkillComponent::HandleUnmanaged(const uint32_t behaviorId, const LWOOBJID target, LWOOBJID source) { void SkillComponent::HandleUnmanaged(const uint32_t behaviorId, const LWOOBJID target, LWOOBJID source) {
auto* context = new BehaviorContext(source); BehaviorContext context{ source };
context->unmanaged = true; context.unmanaged = true;
context->caster = target; context.caster = target;
auto* behavior = Behavior::CreateBehavior(behaviorId); auto* behavior = Behavior::CreateBehavior(behaviorId);
auto* bitStream = new RakNet::BitStream(); RakNet::BitStream bitStream{};
behavior->Handle(context, bitStream, { target }); behavior->Handle(&context, &bitStream, { target });
delete bitStream;
delete context;
} }
void SkillComponent::HandleUnCast(const uint32_t behaviorId, const LWOOBJID target) { void SkillComponent::HandleUnCast(const uint32_t behaviorId, const LWOOBJID target) {
auto* context = new BehaviorContext(target); BehaviorContext context{ target };
context->caster = target; context.caster = target;
auto* behavior = Behavior::CreateBehavior(behaviorId); auto* behavior = Behavior::CreateBehavior(behaviorId);
behavior->UnCast(context, { target }); behavior->UnCast(&context, { target });
delete context;
} }
SkillComponent::SkillComponent(Entity* parent): Component(parent) { SkillComponent::SkillComponent(Entity* parent): Component(parent) {

View File

@ -269,11 +269,9 @@ void GameMessageHandler::HandleMessage(RakNet::BitStream* inStream, const System
auto* skill_component = entity->GetComponent<SkillComponent>(); auto* skill_component = entity->GetComponent<SkillComponent>();
if (skill_component != nullptr) { if (skill_component != nullptr) {
auto* bs = new RakNet::BitStream(reinterpret_cast<unsigned char*>(const_cast<char*>(message.sBitStream.c_str())), message.sBitStream.size(), false); auto bs = RakNet::BitStream(reinterpret_cast<unsigned char*>(&message.sBitStream[0]), message.sBitStream.size(), false);
skill_component->SyncPlayerProjectile(message.i64LocalID, bs, message.i64TargetID); skill_component->SyncPlayerProjectile(message.i64LocalID, &bs, message.i64TargetID);
delete bs;
} }
break; break;
@ -296,18 +294,16 @@ void GameMessageHandler::HandleMessage(RakNet::BitStream* inStream, const System
bool success = false; bool success = false;
if (behaviorId > 0) { if (behaviorId > 0) {
RakNet::BitStream* bs = new RakNet::BitStream(reinterpret_cast<unsigned char*>(const_cast<char*>(startSkill.sBitStream.c_str())), startSkill.sBitStream.size(), false); auto bs = RakNet::BitStream(reinterpret_cast<unsigned char*>(&startSkill.sBitStream[0]), startSkill.sBitStream.size(), false);
auto* skillComponent = entity->GetComponent<SkillComponent>(); auto* skillComponent = entity->GetComponent<SkillComponent>();
success = skillComponent->CastPlayerSkill(behaviorId, startSkill.uiSkillHandle, bs, startSkill.optionalTargetID, startSkill.skillID); success = skillComponent->CastPlayerSkill(behaviorId, startSkill.uiSkillHandle, &bs, startSkill.optionalTargetID, startSkill.skillID);
if (success && entity->GetCharacter()) { if (success && entity->GetCharacter()) {
DestroyableComponent* destComp = entity->GetComponent<DestroyableComponent>(); DestroyableComponent* destComp = entity->GetComponent<DestroyableComponent>();
destComp->SetImagination(destComp->GetImagination() - skillTable->GetSkillByID(startSkill.skillID).imaginationcost); destComp->SetImagination(destComp->GetImagination() - skillTable->GetSkillByID(startSkill.skillID).imaginationcost);
} }
delete bs;
} }
if (Game::server->GetZoneID() == 1302) { if (Game::server->GetZoneID() == 1302) {
@ -353,13 +349,11 @@ void GameMessageHandler::HandleMessage(RakNet::BitStream* inStream, const System
} }
if (usr != nullptr) { if (usr != nullptr) {
RakNet::BitStream* bs = new RakNet::BitStream(reinterpret_cast<unsigned char*>(const_cast<char*>(sync.sBitStream.c_str())), sync.sBitStream.size(), false); auto bs = RakNet::BitStream(reinterpret_cast<unsigned char*>(&sync.sBitStream[0]), sync.sBitStream.size(), false);
auto* skillComponent = entity->GetComponent<SkillComponent>(); auto* skillComponent = entity->GetComponent<SkillComponent>();
skillComponent->SyncPlayerSkill(sync.uiSkillHandle, sync.uiBehaviorHandle, bs); skillComponent->SyncPlayerSkill(sync.uiSkillHandle, sync.uiBehaviorHandle, &bs);
delete bs;
} }
EchoSyncSkill echo = EchoSyncSkill(); EchoSyncSkill echo = EchoSyncSkill();