From 95d687846a35c4fe01a647ccc4c00f24264561ba Mon Sep 17 00:00:00 2001 From: jadebenn Date: Sun, 25 Feb 2024 19:35:07 -0600 Subject: [PATCH] chore: Remove unnecessary heap allocations (#1478) --- dGame/dBehaviors/BehaviorContext.cpp | 8 ++--- dGame/dComponents/SkillComponent.cpp | 39 ++++++++-------------- dGame/dGameMessages/GameMessageHandler.cpp | 18 ++++------ 3 files changed, 23 insertions(+), 42 deletions(-) diff --git a/dGame/dBehaviors/BehaviorContext.cpp b/dGame/dBehaviors/BehaviorContext.cpp index e4f3011c..3e00ee9d 100644 --- a/dGame/dBehaviors/BehaviorContext.cpp +++ b/dGame/dBehaviors/BehaviorContext.cpp @@ -243,13 +243,13 @@ bool BehaviorContext::CalculateUpdate(const float deltaTime) { echo.uiBehaviorHandle = entry.handle; echo.uiSkillHandle = this->skillUId; - auto* bitStream = new RakNet::BitStream(); + RakNet::BitStream bitStream{}; // Calculate sync - entry.behavior->SyncCalculation(this, bitStream, entry.branchContext); + entry.behavior->SyncCalculation(this, &bitStream, entry.branchContext); if (!clientInitalized) { - echo.sBitStream.assign(reinterpret_cast(bitStream->GetData()), bitStream->GetNumberOfBytesUsed()); + echo.sBitStream.assign(reinterpret_cast(bitStream.GetData()), bitStream.GetNumberOfBytesUsed()); // Write message RakNet::BitStream message; @@ -262,8 +262,6 @@ bool BehaviorContext::CalculateUpdate(const float deltaTime) { } ExecuteUpdates(); - - delete bitStream; } std::vector valid; diff --git a/dGame/dComponents/SkillComponent.cpp b/dGame/dComponents/SkillComponent.cpp index c43813c1..e84c3eb1 100644 --- a/dGame/dComponents/SkillComponent.cpp +++ b/dGame/dComponents/SkillComponent.cpp @@ -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) { - auto* bitStream = new RakNet::BitStream(); + RakNet::BitStream bitStream{}; auto* behavior = Behavior::CreateBehavior(behaviorId); @@ -266,14 +266,13 @@ SkillExecutionResult SkillComponent::CalculateBehavior(const uint32_t skillId, c 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)) { script->OnSkillCast(m_Parent, skillId); } if (!context->foundTarget) { - delete bitStream; delete context; // Invalid attack @@ -299,7 +298,7 @@ SkillExecutionResult SkillComponent::CalculateBehavior(const uint32_t skillId, c } //start.optionalTargetID = target; - start.sBitStream.assign(reinterpret_cast(bitStream->GetData()), bitStream->GetNumberOfBytesUsed()); + start.sBitStream.assign(reinterpret_cast(bitStream.GetData()), bitStream.GetNumberOfBytesUsed()); // Write message RakNet::BitStream message; @@ -313,8 +312,6 @@ SkillExecutionResult SkillComponent::CalculateBehavior(const uint32_t skillId, c context->ExecuteUpdates(); - delete bitStream; - // Valid attack return { true, context->skillTime }; } @@ -424,13 +421,13 @@ void SkillComponent::SyncProjectileCalculation(const ProjectileSyncEntry& entry) 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; - projectileImpact.sBitStream.assign(reinterpret_cast(bitStream->GetData()), bitStream->GetNumberOfBytesUsed()); + projectileImpact.sBitStream.assign(reinterpret_cast(bitStream.GetData()), bitStream.GetNumberOfBytesUsed()); projectileImpact.i64OwnerID = this->m_Parent->GetObjectID(); projectileImpact.i64OrgID = entry.id; projectileImpact.i64TargetID = entry.branchContext.target; @@ -444,37 +441,29 @@ void SkillComponent::SyncProjectileCalculation(const ProjectileSyncEntry& entry) Game::server->Send(&message, UNASSIGNED_SYSTEM_ADDRESS, true); entry.context->ExecuteUpdates(); - - delete bitStream; } void SkillComponent::HandleUnmanaged(const uint32_t behaviorId, const LWOOBJID target, LWOOBJID source) { - auto* context = new BehaviorContext(source); + BehaviorContext context{ source }; - context->unmanaged = true; - context->caster = target; + context.unmanaged = true; + context.caster = target; auto* behavior = Behavior::CreateBehavior(behaviorId); - auto* bitStream = new RakNet::BitStream(); + RakNet::BitStream bitStream{}; - behavior->Handle(context, bitStream, { target }); - - delete bitStream; - - delete context; + behavior->Handle(&context, &bitStream, { 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); - behavior->UnCast(context, { target }); - - delete context; + behavior->UnCast(&context, { target }); } SkillComponent::SkillComponent(Entity* parent): Component(parent) { diff --git a/dGame/dGameMessages/GameMessageHandler.cpp b/dGame/dGameMessages/GameMessageHandler.cpp index 64790e31..1ee9f694 100644 --- a/dGame/dGameMessages/GameMessageHandler.cpp +++ b/dGame/dGameMessages/GameMessageHandler.cpp @@ -269,11 +269,9 @@ void GameMessageHandler::HandleMessage(RakNet::BitStream* inStream, const System auto* skill_component = entity->GetComponent(); if (skill_component != nullptr) { - auto* bs = new RakNet::BitStream(reinterpret_cast(const_cast(message.sBitStream.c_str())), message.sBitStream.size(), false); + auto bs = RakNet::BitStream(reinterpret_cast(&message.sBitStream[0]), message.sBitStream.size(), false); - skill_component->SyncPlayerProjectile(message.i64LocalID, bs, message.i64TargetID); - - delete bs; + skill_component->SyncPlayerProjectile(message.i64LocalID, &bs, message.i64TargetID); } break; @@ -296,18 +294,16 @@ void GameMessageHandler::HandleMessage(RakNet::BitStream* inStream, const System bool success = false; if (behaviorId > 0) { - RakNet::BitStream* bs = new RakNet::BitStream(reinterpret_cast(const_cast(startSkill.sBitStream.c_str())), startSkill.sBitStream.size(), false); + auto bs = RakNet::BitStream(reinterpret_cast(&startSkill.sBitStream[0]), startSkill.sBitStream.size(), false); auto* skillComponent = entity->GetComponent(); - 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()) { DestroyableComponent* destComp = entity->GetComponent(); destComp->SetImagination(destComp->GetImagination() - skillTable->GetSkillByID(startSkill.skillID).imaginationcost); } - - delete bs; } if (Game::server->GetZoneID() == 1302) { @@ -353,13 +349,11 @@ void GameMessageHandler::HandleMessage(RakNet::BitStream* inStream, const System } if (usr != nullptr) { - RakNet::BitStream* bs = new RakNet::BitStream(reinterpret_cast(const_cast(sync.sBitStream.c_str())), sync.sBitStream.size(), false); + auto bs = RakNet::BitStream(reinterpret_cast(&sync.sBitStream[0]), sync.sBitStream.size(), false); auto* skillComponent = entity->GetComponent(); - skillComponent->SyncPlayerSkill(sync.uiSkillHandle, sync.uiBehaviorHandle, bs); - - delete bs; + skillComponent->SyncPlayerSkill(sync.uiSkillHandle, sync.uiBehaviorHandle, &bs); } EchoSyncSkill echo = EchoSyncSkill();