Skip to content

Commit c55ef42

Browse files
author
saml1er
authored
Improve pool performance (#480)
* Implement fast index based lookup for ped pool. The code is tested and works nicely without breaking anything. This technique allows us to eliminate std::map and looping through arrays by using the index of the object pointer within the pool. Next step is to do the same for other pools. This will give us a decent performance boost. * Fix crashes, bugs, and formatting Some code was written by lucy.x and some was written by me. I combined them in one commit to fix the formatting. * Improve variable names * Replaced calls to Xref manager with new CPool getters * Remove CGameEntityXRefManager code * Remove debug code (printf) * Add sanity checks * Disable console * Remove commented code * Restore original code for unintended changes * Update bass files * Apply source code formatting * Fix source code formatting * Remove map and array members of SPoolData struct * Restore original code * Remove commented code from CClientPed::GetContactEntity * Add vehicle and object type check to CClientPed::GetContactEntity
1 parent 3b154bc commit c55ef42

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1095
-1403
lines changed

Client/game_sa/CCamSA.cpp

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,12 @@
1414
CEntity* CCamSA::GetTargetEntity(void) const
1515
{
1616
CEntitySAInterface* pInterface = m_pInterface->CamTargetEntity;
17-
CPoolsSA* pPools = (CPoolsSA*)pGame->GetPools();
18-
CEntity* pReturn = NULL;
19-
20-
if (pPools && pInterface)
17+
if (pInterface)
2118
{
22-
switch (pInterface->nType)
23-
{
24-
case ENTITY_TYPE_PED:
25-
pReturn = (CEntity*)(pPools->GetPed((DWORD*)pInterface));
26-
break;
27-
case ENTITY_TYPE_VEHICLE:
28-
pReturn = (CEntity*)(pPools->GetVehicle((DWORD*)pInterface));
29-
break;
30-
case ENTITY_TYPE_OBJECT:
31-
pReturn = (CEntity*)(pPools->GetObject((DWORD*)pInterface));
32-
break;
33-
default:
34-
break;
35-
}
19+
CPools* pPools = pGame->GetPools();
20+
return pPools->GetEntity((DWORD*)pInterface);
3621
}
37-
return pReturn;
22+
return nullptr;
3823
}
3924

4025
void CCamSA::SetTargetEntity(CEntity* pEntity)

Client/game_sa/CCameraSA.cpp

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -533,27 +533,12 @@ RwMatrix* CCameraSA::GetLTM(void)
533533
CEntity* CCameraSA::GetTargetEntity(void)
534534
{
535535
CEntitySAInterface* pInterface = this->GetInterface()->pTargetEntity;
536-
CPoolsSA* pPools = ((CPoolsSA*)pGame->GetPools());
537-
CEntity* pReturn = NULL;
538-
539-
if (pPools && pInterface)
536+
if (pInterface)
540537
{
541-
switch (pInterface->nType)
542-
{
543-
case ENTITY_TYPE_PED:
544-
pReturn = (CEntity*)(pPools->GetPed((DWORD*)pInterface));
545-
break;
546-
case ENTITY_TYPE_VEHICLE:
547-
pReturn = (CEntity*)(pPools->GetVehicle((DWORD*)pInterface));
548-
break;
549-
case ENTITY_TYPE_OBJECT:
550-
pReturn = (CEntity*)(pPools->GetObject((DWORD*)pInterface));
551-
break;
552-
default:
553-
break;
554-
}
538+
CPools* pPools = pGame->GetPools();
539+
return pPools->GetEntity((DWORD*)pInterface);
555540
}
556-
return pReturn;
541+
return nullptr;
557542
}
558543

559544
void CCameraSA::SetCameraClip(bool bObjects, bool bVehicles)

Client/game_sa/CEventDamageSA.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,8 @@ CEntity* CEventDamageSA::GetInflictingEntity(void)
6666
CEntitySAInterface* pInterface = m_pInterface->pInflictor;
6767
if (pInterface)
6868
{
69-
CPoolsSA* pPools = ((CPoolsSA*)pGame->GetPools());
70-
switch (pInterface->nType)
71-
{
72-
case ENTITY_TYPE_PED:
73-
pReturn = pPools->GetPed((DWORD*)pInterface);
74-
break;
75-
case ENTITY_TYPE_VEHICLE:
76-
pReturn = pPools->GetVehicle((DWORD*)pInterface);
77-
break;
78-
case ENTITY_TYPE_OBJECT:
79-
pReturn = pPools->GetObject((DWORD*)pInterface);
80-
break;
81-
default:
82-
break;
83-
}
69+
CPools* pPools = pGame->GetPools();
70+
return pPools->GetEntity((DWORD*)pInterface);
8471
}
8572
return pReturn;
8673
}

Client/game_sa/CExplosionSA.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,32 @@ CEntity* CExplosionSA::GetExplosionCreator(void)
4040
eEntityType entityType = entity->GetEntityType();
4141
delete entity;
4242

43-
CPoolsSA* pools = (CPoolsSA*)pGame->GetPools();
44-
43+
CPools* pools = pGame->GetPools();
4544
switch (entityType)
4645
{
4746
case ENTITY_TYPE_PED:
48-
return (CEntity*)(pools->GetPed((DWORD*)this->GetInterface()->m_pEntExplosionOwner));
47+
{
48+
SClientEntity<CPedSA>* pPedClientEntity = pools->GetPed((DWORD*)entity->m_pInterface);
49+
if (pPedClientEntity)
50+
{
51+
return pPedClientEntity->pEntity;
52+
;
53+
}
4954
break;
55+
}
5056
case ENTITY_TYPE_VEHICLE:
51-
return (CEntity*)(pools->GetVehicle((DWORD*)this->GetInterface()->m_pEntExplosionOwner));
52-
break;
57+
{
58+
SClientEntity<CVehicleSA>* pVehicleClientEntity = pools->GetVehicle((DWORD*)entity->m_pInterface);
59+
if (pVehicleClientEntity)
60+
{
61+
return pVehicleClientEntity->pEntity;
62+
break;
63+
}
64+
}
5365
case ENTITY_TYPE_OBJECT:
66+
{
5467
break;
68+
}
5569
}
5670
return NULL;
5771
}

Client/game_sa/CFireSA.cpp

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,27 @@ CEntity* CFireSA::GetCreator()
7676
switch (createEntitySA->nType)
7777
{
7878
case ENTITY_TYPE_PED:
79-
creatorEntity = pPools->GetPed((DWORD*)createEntitySA);
79+
{
80+
SClientEntity<CPedSA>* pPedClientEntity = pPools->GetPed((DWORD*)createEntitySA);
81+
if (pPedClientEntity)
82+
{
83+
creatorEntity = pPedClientEntity->pEntity;
84+
}
8085
break;
86+
}
8187
case ENTITY_TYPE_VEHICLE:
82-
creatorEntity = pPools->GetVehicle((DWORD*)createEntitySA);
88+
{
89+
SClientEntity<CVehicleSA>* pVehicleClientEntity = pPools->GetVehicle((DWORD*)createEntitySA);
90+
if (pVehicleClientEntity)
91+
{
92+
creatorEntity = pVehicleClientEntity->pEntity;
93+
}
8394
break;
95+
}
8496
default:
97+
{
8598
creatorEntity = NULL;
99+
}
86100
}
87101
}
88102
return creatorEntity;
@@ -99,13 +113,27 @@ CEntity* CFireSA::GetEntityOnFire()
99113
switch (TargetEntitySA->nType)
100114
{
101115
case ENTITY_TYPE_PED:
102-
TargetEntity = pPools->GetPed((DWORD*)TargetEntitySA);
116+
{
117+
SClientEntity<CPedSA>* pPedClientEntity = pPools->GetPed((DWORD*)TargetEntitySA);
118+
if (pPedClientEntity)
119+
{
120+
TargetEntity = pPedClientEntity->pEntity;
121+
}
103122
break;
123+
}
104124
case ENTITY_TYPE_VEHICLE:
105-
TargetEntity = pPools->GetVehicle((DWORD*)TargetEntitySA);
125+
{
126+
SClientEntity<CVehicleSA>* pVehicleClientEntity = pPools->GetVehicle((DWORD*)TargetEntitySA);
127+
if (pVehicleClientEntity)
128+
{
129+
TargetEntity = pVehicleClientEntity->pEntity;
130+
}
106131
break;
132+
}
107133
default:
134+
{
108135
TargetEntity = NULL;
136+
}
109137
}
110138
}
111139
return TargetEntity;

Client/game_sa/CGameSA.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -348,18 +348,10 @@ eSystemState CGameSA::GetSystemState()
348348
* This adds the local player to the ped pool, nothing else
349349
* @return BOOL TRUE if success, FALSE otherwise
350350
*/
351-
BOOL CGameSA::InitLocalPlayer()
351+
BOOL CGameSA::InitLocalPlayer(CClientPed* pClientPed)
352352
{
353353
DEBUG_TRACE("BOOL CGameSA::InitLocalPlayer( )");
354354

355-
// Added by ChrML - Looks like it isn't safe to call this more than once but mod code might do
356-
static bool bAlreadyInited = false;
357-
if (bAlreadyInited)
358-
{
359-
return TRUE;
360-
}
361-
bAlreadyInited = true;
362-
363355
CPoolsSA* pools = (CPoolsSA*)this->GetPools();
364356
if (pools)
365357
{
@@ -368,7 +360,8 @@ BOOL CGameSA::InitLocalPlayer()
368360

369361
if (pInterface)
370362
{
371-
pools->AddPed((DWORD*)pInterface);
363+
pools->ResetPedPoolCount();
364+
pools->AddPed(pClientPed, (DWORD*)pInterface);
372365
return TRUE;
373366
}
374367

Client/game_sa/CGameSA.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ class CGameSA : public CGame
364364
float GetTimeScale(void);
365365
void SetTimeScale(float fTimeScale);
366366

367-
BOOL InitLocalPlayer();
367+
BOOL InitLocalPlayer(class CClientPed* pClientPed);
368368

369369
float GetGravity(void);
370370
void SetGravity(float fGravity);

Client/game_sa/CPedSA.cpp

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,10 @@ CVehicle* CPedSA::GetVehicle()
242242
{
243243
CVehicleSAInterface* vehicle = (CVehicleSAInterface*)(((CPedSAInterface*)this->GetInterface())->CurrentObjective);
244244
if (vehicle)
245-
return ((CPoolsSA*)pGame->GetPools())->GetVehicle((DWORD*)vehicle);
245+
{
246+
SClientEntity<CVehicleSA>* pVehicleClientEntity = pGame->GetPools()->GetVehicle((DWORD*)vehicle);
247+
return pVehicleClientEntity ? pVehicleClientEntity->pEntity : nullptr;
248+
}
246249
}
247250
return NULL;
248251
}
@@ -764,24 +767,36 @@ void CPedSA::SetFightingStyle(eFightingStyle style, BYTE bStyleExtra)
764767
CEntity* CPedSA::GetContactEntity(void)
765768
{
766769
CEntitySAInterface* pInterface = ((CPedSAInterface*)this->GetInterface())->pContactEntity;
767-
CPoolsSA* pPools = ((CPoolsSA*)pGame->GetPools());
768-
CEntity* pReturn = NULL;
769-
770-
if (pPools && pInterface)
770+
if (pInterface)
771771
{
772+
CPools* pPools = pGame->GetPools();
772773
switch (pInterface->nType)
773774
{
774775
case ENTITY_TYPE_VEHICLE:
775-
pReturn = (CEntity*)(pPools->GetVehicle((DWORD*)pInterface));
776+
{
777+
SClientEntity<CVehicleSA>* pVehicleClientEntity = pPools->GetVehicle((DWORD*)pInterface);
778+
if (pVehicleClientEntity)
779+
{
780+
return pVehicleClientEntity->pEntity;
781+
}
776782
break;
783+
}
777784
case ENTITY_TYPE_OBJECT:
778-
pReturn = (CEntity*)(pPools->GetObject((DWORD*)pInterface));
785+
{
786+
SClientEntity<CObjectSA>* pObjectClientEntity = pPools->GetObject((DWORD*)pInterface);
787+
if (pObjectClientEntity)
788+
{
789+
return pObjectClientEntity->pEntity;
790+
}
779791
break;
792+
}
780793
default:
794+
{
781795
break;
796+
}
782797
}
783798
}
784-
return pReturn;
799+
return nullptr;
785800
}
786801

787802
unsigned char CPedSA::GetRunState(void)
@@ -792,27 +807,12 @@ unsigned char CPedSA::GetRunState(void)
792807
CEntity* CPedSA::GetTargetedEntity(void)
793808
{
794809
CEntitySAInterface* pInterface = ((CPedSAInterface*)this->GetInterface())->pTargetedEntity;
795-
CPoolsSA* pPools = ((CPoolsSA*)pGame->GetPools());
796-
CEntity* pReturn = NULL;
797-
798-
if (pPools && pInterface)
810+
if (pInterface)
799811
{
800-
switch (pInterface->nType)
801-
{
802-
case ENTITY_TYPE_PED:
803-
pReturn = (CEntity*)(pPools->GetPed((DWORD*)pInterface));
804-
break;
805-
case ENTITY_TYPE_VEHICLE:
806-
pReturn = (CEntity*)(pPools->GetVehicle((DWORD*)pInterface));
807-
break;
808-
case ENTITY_TYPE_OBJECT:
809-
pReturn = (CEntity*)(pPools->GetObject((DWORD*)pInterface));
810-
break;
811-
default:
812-
break;
813-
}
812+
CPools* pPools = pGame->GetPools();
813+
return pPools->GetEntity((DWORD*)pInterface);
814814
}
815-
return pReturn;
815+
return nullptr;
816816
}
817817

818818
void CPedSA::SetTargetedEntity(CEntity* pEntity)

Client/game_sa/CPhysicalSA.cpp

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -214,27 +214,12 @@ void CPhysicalSA::SetDamageImpulseMagnitude(float fMagnitude)
214214
CEntity* CPhysicalSA::GetDamageEntity(void)
215215
{
216216
CEntitySAInterface* pInterface = ((CPhysicalSAInterface*)this->GetInterface())->m_pCollidedEntity;
217-
CPoolsSA* pPools = ((CPoolsSA*)pGame->GetPools());
218-
CEntity* pReturn = NULL;
219-
220-
if (pPools && pInterface)
217+
if (pInterface)
221218
{
222-
switch (pInterface->nType)
223-
{
224-
case ENTITY_TYPE_PED:
225-
pReturn = (CEntity*)(pPools->GetPed((DWORD*)pInterface));
226-
break;
227-
case ENTITY_TYPE_VEHICLE:
228-
pReturn = (CEntity*)(pPools->GetVehicle((DWORD*)pInterface));
229-
break;
230-
case ENTITY_TYPE_OBJECT:
231-
pReturn = (CEntity*)(pPools->GetObject((DWORD*)pInterface));
232-
break;
233-
default:
234-
break;
235-
}
219+
CPools* pPools = pGame->GetPools();
220+
return pPools->GetEntity((DWORD*)pInterface);
236221
}
237-
return pReturn;
222+
return nullptr;
238223
}
239224

240225
void CPhysicalSA::SetDamageEntity(CEntity* pEntity)
@@ -253,27 +238,12 @@ void CPhysicalSA::ResetLastDamage(void)
253238
CEntity* CPhysicalSA::GetAttachedEntity(void)
254239
{
255240
CEntitySAInterface* pInterface = ((CPhysicalSAInterface*)this->GetInterface())->m_pAttachedEntity;
256-
CPoolsSA* pPools = ((CPoolsSA*)pGame->GetPools());
257-
CEntity* pReturn = NULL;
258-
259-
if (pPools && pInterface)
241+
if (pInterface)
260242
{
261-
switch (pInterface->nType)
262-
{
263-
case ENTITY_TYPE_PED:
264-
pReturn = (CEntity*)(pPools->GetPed((DWORD*)pInterface));
265-
break;
266-
case ENTITY_TYPE_VEHICLE:
267-
pReturn = (CEntity*)(pPools->GetVehicle((DWORD*)pInterface));
268-
break;
269-
case ENTITY_TYPE_OBJECT:
270-
pReturn = (CEntity*)(pPools->GetObject((DWORD*)pInterface));
271-
break;
272-
default:
273-
break;
274-
}
243+
CPools* pPools = pGame->GetPools();
244+
return pPools->GetEntity((DWORD*)pInterface);
275245
}
276-
return pReturn;
246+
return nullptr;
277247
}
278248

279249
void CPhysicalSA::AttachEntityToEntity(CPhysical& Entity, const CVector& vecPosition, const CVector& vecRotation)

Client/game_sa/CPlayerPedSA.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,8 @@ int GetAnimPose(int iAnim)
494494
////////////////////////////////////////////////////////////////
495495
__declspec(noinline) int _cdecl OnCPlayerPed_ProcessAnimGroups_Mid(CPlayerPedSAInterface* pPlayerPedSAInterface, int iReqMoveAnim)
496496
{
497-
CPed* pPed = pGame->GetPools()->GetPed((DWORD*)pPlayerPedSAInterface);
497+
SClientEntity<CPedSA>* pPedClientEntity = pGame->GetPools()->GetPed((DWORD*)pPlayerPedSAInterface);
498+
CPed* pPed = pPedClientEntity ? pPedClientEntity->pEntity : nullptr;
498499

499500
if (pPed)
500501
{

0 commit comments

Comments
 (0)