Skip to content

Commit

Permalink
[10276] Use more appropriate vector container instead of list as cont…
Browse files Browse the repository at this point in the history
…ainer for CreatureEventAI holders

vector container provides less time to access to contained elements
  • Loading branch information
SilverIce committed Jul 27, 2010
1 parent 3e5d2a3 commit d899744
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
30 changes: 15 additions & 15 deletions src/game/CreatureEventAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ CreatureEventAI::CreatureEventAI(Creature *c ) : CreatureAI(c)
//Handle Spawned Events
if (!bEmptyList)
{
for (std::list<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
for (std::vector<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
if (SpawnedEventConditionsCheck((*i).Event))
ProcessEvent(*i);
}
Expand Down Expand Up @@ -794,7 +794,7 @@ void CreatureEventAI::JustRespawned()
return;

//Handle Spawned Events
for (std::list<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
for (std::vector<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
if (SpawnedEventConditionsCheck((*i).Event))
ProcessEvent(*i);
}
Expand All @@ -808,7 +808,7 @@ void CreatureEventAI::Reset()
return;

//Reset all events to enabled
for (std::list<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
for (std::vector<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
{
CreatureEventAI_Event const& event = (*i).Event;
switch (event.event_type)
Expand All @@ -835,7 +835,7 @@ void CreatureEventAI::JustReachedHome()

if (!bEmptyList)
{
for (std::list<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
for (std::vector<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
{
if ((*i).Event.event_type == EVENT_T_REACHED_HOME)
ProcessEvent(*i);
Expand All @@ -860,7 +860,7 @@ void CreatureEventAI::EnterEvadeMode()
return;

//Handle Evade events
for (std::list<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
for (std::vector<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
{
if ((*i).Event.event_type == EVENT_T_EVADE)
ProcessEvent(*i);
Expand All @@ -882,7 +882,7 @@ void CreatureEventAI::JustDied(Unit* killer)
return;

//Handle Evade events
for (std::list<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
for (std::vector<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
{
if ((*i).Event.event_type == EVENT_T_DEATH)
ProcessEvent(*i, killer);
Expand All @@ -897,7 +897,7 @@ void CreatureEventAI::KilledUnit(Unit* victim)
if (bEmptyList || victim->GetTypeId() != TYPEID_PLAYER)
return;

for (std::list<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
for (std::vector<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
{
if ((*i).Event.event_type == EVENT_T_KILL)
ProcessEvent(*i, victim);
Expand All @@ -909,7 +909,7 @@ void CreatureEventAI::JustSummoned(Creature* pUnit)
if (bEmptyList || !pUnit)
return;

for (std::list<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
for (std::vector<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
{
if ((*i).Event.event_type == EVENT_T_SUMMONED_UNIT)
ProcessEvent(*i, pUnit);
Expand All @@ -921,7 +921,7 @@ void CreatureEventAI::SummonedCreatureJustDied(Creature* pUnit)
if (bEmptyList || !pUnit)
return;

for (std::list<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
for (std::vector<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
{
if ((*i).Event.event_type == EVENT_T_SUMMONED_JUST_DIED)
ProcessEvent(*i, pUnit);
Expand All @@ -933,7 +933,7 @@ void CreatureEventAI::SummonedCreatureDespawn(Creature* pUnit)
if (bEmptyList || !pUnit)
return;

for (std::list<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
for (std::vector<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
{
if ((*i).Event.event_type == EVENT_T_SUMMONED_JUST_DESPAWN)
ProcessEvent(*i, pUnit);
Expand All @@ -945,7 +945,7 @@ void CreatureEventAI::EnterCombat(Unit *enemy)
//Check for on combat start events
if (!bEmptyList)
{
for (std::list<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
for (std::vector<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
{
CreatureEventAI_Event const& event = (*i).Event;
switch (event.event_type)
Expand Down Expand Up @@ -1003,7 +1003,7 @@ void CreatureEventAI::MoveInLineOfSight(Unit *who)
//Check for OOC LOS Event
if (!bEmptyList && !m_creature->getVictim())
{
for (std::list<CreatureEventAIHolder>::iterator itr = CreatureEventAIList.begin(); itr != CreatureEventAIList.end(); ++itr)
for (std::vector<CreatureEventAIHolder>::iterator itr = CreatureEventAIList.begin(); itr != CreatureEventAIList.end(); ++itr)
{
if ((*itr).Event.event_type == EVENT_T_OOC_LOS)
{
Expand Down Expand Up @@ -1054,7 +1054,7 @@ void CreatureEventAI::SpellHit(Unit* pUnit, const SpellEntry* pSpell)
if (bEmptyList)
return;

for (std::list<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
for (std::vector<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
if ((*i).Event.event_type == EVENT_T_SPELLHIT)
//If spell id matches (or no spell id) & if spell school matches (or no spell school)
if (!(*i).Event.spell_hit.spellId || pSpell->Id == (*i).Event.spell_hit.spellId)
Expand All @@ -1079,7 +1079,7 @@ void CreatureEventAI::UpdateAI(const uint32 diff)
EventDiff += diff;

//Check for time based events
for (std::list<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
for (std::vector<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
{
//Decrement Timers
if ((*i).Time)
Expand Down Expand Up @@ -1343,7 +1343,7 @@ void CreatureEventAI::ReceiveEmote(Player* pPlayer, uint32 text_emote)
if (bEmptyList)
return;

for (std::list<CreatureEventAIHolder>::iterator itr = CreatureEventAIList.begin(); itr != CreatureEventAIList.end(); ++itr)
for (std::vector<CreatureEventAIHolder>::iterator itr = CreatureEventAIList.begin(); itr != CreatureEventAIList.end(); ++itr)
{
if ((*itr).Event.event_type == EVENT_T_RECEIVE_EMOTE)
{
Expand Down
2 changes: 1 addition & 1 deletion src/game/CreatureEventAI.h
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ class MANGOS_DLL_SPEC CreatureEventAI : public CreatureAI
void DoFindFriendlyCC(std::list<Creature*>& _list, float range);

//Holder for events (stores enabled, time, and eventid)
std::list<CreatureEventAIHolder> CreatureEventAIList;
std::vector<CreatureEventAIHolder> CreatureEventAIList;
uint32 EventUpdateTime; //Time between event updates
uint32 EventDiff; //Time between the last event call
bool bEmptyList;
Expand Down
2 changes: 1 addition & 1 deletion src/shared/revision_nr.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifndef __REVISION_NR_H__
#define __REVISION_NR_H__
#define REVISION_NR "10275"
#define REVISION_NR "10276"
#endif // __REVISION_NR_H__

9 comments on commit d899744

@LordJZ
Copy link
Contributor

@LordJZ LordJZ commented on d899744 Jul 27, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SilverIce is back

@Wowka321
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graz, SilverIce ^^

@PatSmuk
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just wondering what the difference is and why vector is better than list for this. I don't think you're wrong, I just want to be able to understand why for the future.

@silviu199208
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think SilverIce already provided the answer: "vector container provides less time to access to contained elements"

@Azazel
Copy link

@Azazel Azazel commented on d899744 Jul 27, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, why not using typedef for collection? Thus you'll be able to change container type in one place only.

@VladimirMangos
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I not sure that type change so good. This container used for (1) full loop by elements, where std::list and std::vector not expected big differences. Ok maybe small vector prio. And (2) push_back at each AI creating, where vector i think lot slower that list. Ofc, AI creating (2) more seldom event that very often loops (1) but i not sure that small speedup in (1) will in sum more that slowdown in (2)

Btw, why not using typedef for collection?

I already do related patch.

@SilverIce
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(2) push_back at each AI creating, where vector i think lot slower that list

in vector case: not need to allocate memory for each element at each push_back call
I have never heard that vector is slower than list at puhing back ( at least for containers with small amount of elements, like here)

@VladimirMangos
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in vector case vector reserve some size, so use more memory that need and if this reserve too small then reallocate all memory block with all data move from old to new... in bad case.

@VladimirMangos
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vector will good for push_back if it will before this reserve exactly required data amount. So some -pre-counting need before pushs

Please sign in to comment.