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.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<char*>(bitStream->GetData()), bitStream->GetNumberOfBytesUsed());
echo.sBitStream.assign(reinterpret_cast<char*>(bitStream.GetData()), bitStream.GetNumberOfBytesUsed());
// Write message
RakNet::BitStream message;
@ -262,8 +262,6 @@ bool BehaviorContext::CalculateUpdate(const float deltaTime) {
}
ExecuteUpdates();
delete bitStream;
}
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) {
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<char*>(bitStream->GetData()), bitStream->GetNumberOfBytesUsed());
start.sBitStream.assign(reinterpret_cast<char*>(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<char*>(bitStream->GetData()), bitStream->GetNumberOfBytesUsed());
projectileImpact.sBitStream.assign(reinterpret_cast<char*>(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) {

View File

@ -269,11 +269,9 @@ void GameMessageHandler::HandleMessage(RakNet::BitStream* inStream, const System
auto* skill_component = entity->GetComponent<SkillComponent>();
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);
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<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>();
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<DestroyableComponent>();
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<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>();
skillComponent->SyncPlayerSkill(sync.uiSkillHandle, sync.uiBehaviorHandle, bs);
delete bs;
skillComponent->SyncPlayerSkill(sync.uiSkillHandle, sync.uiBehaviorHandle, &bs);
}
EchoSyncSkill echo = EchoSyncSkill();