Skip to content

Commit aba2599

Browse files
authored
perf: monster process_items performance improvements (#8227)
* this shouldn't work * wow this works
1 parent 942e505 commit aba2599

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

src/monster.cpp

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,9 @@ void monster::shift( point sm_shift )
12461246

12471247
detached_ptr<item> monster::set_tack_item( detached_ptr<item> &&to )
12481248
{
1249+
if( to && to->typeId() != itype_id::NULL_ID() ) {
1250+
has_processable_items = true;
1251+
}
12491252
return tack_item.swap( std::move( to ) );
12501253
}
12511254

@@ -1264,6 +1267,9 @@ item *monster::get_tack_item() const
12641267

12651268
detached_ptr<item> monster::set_tied_item( detached_ptr<item> &&to )
12661269
{
1270+
if( to && to->typeId() != itype_id::NULL_ID() ) {
1271+
has_processable_items = true;
1272+
}
12671273
return tied_item.swap( std::move( to ) );
12681274
}
12691275

@@ -1282,6 +1288,9 @@ item *monster::get_tied_item() const
12821288

12831289
detached_ptr<item> monster::set_armor_item( detached_ptr<item> &&to )
12841290
{
1291+
if( to && to->typeId() != itype_id::NULL_ID() ) {
1292+
has_processable_items = true;
1293+
}
12851294
return armor_item.swap( std::move( to ) );
12861295
}
12871296

@@ -1300,6 +1309,9 @@ item *monster::get_armor_item() const
13001309

13011310
detached_ptr<item> monster::set_storage_item( detached_ptr<item> &&to )
13021311
{
1312+
if( to && to->typeId() != itype_id::NULL_ID() ) {
1313+
has_processable_items = true;
1314+
}
13031315
return storage_item.swap( std::move( to ) );
13041316
}
13051317

@@ -1318,6 +1330,9 @@ item *monster::get_storage_item() const
13181330

13191331
detached_ptr<item> monster::set_battery_item( detached_ptr<item> &&to )
13201332
{
1333+
if( to && to->typeId() != itype_id::NULL_ID() ) {
1334+
has_processable_items = true;
1335+
}
13211336
return battery_item.swap( std::move( to ) );
13221337
}
13231338

@@ -3018,19 +3033,22 @@ static void process_item_valptr( item *ptr, monster &mon )
30183033
void monster::process_items()
30193034
{
30203035
ZoneScoped;
3036+
if( !inv.empty() ) {
3037+
inv.remove_with( [this]( detached_ptr<item> &&it ) {
3038+
if( it->needs_processing() ) {
3039+
return item::process( std::move( it ), nullptr, pos(), false );
3040+
}
3041+
return std::move( it );
3042+
} );
3043+
}
30213044

3022-
inv.remove_with( [this]( detached_ptr<item> &&it ) {
3023-
if( it->needs_processing() ) {
3024-
return item::process( std::move( it ), nullptr, pos(), false );
3025-
}
3026-
return std::move( it );
3027-
} );
3028-
3029-
process_item_valptr( &*storage_item, *this );
3030-
process_item_valptr( &*armor_item, *this );
3031-
process_item_valptr( &*tack_item, *this );
3032-
process_item_valptr( &*tied_item, *this );
3033-
process_item_valptr( &*battery_item, *this );
3045+
if( has_processable_items ) {
3046+
process_item_valptr( &*storage_item, *this );
3047+
process_item_valptr( &*armor_item, *this );
3048+
process_item_valptr( &*tack_item, *this );
3049+
process_item_valptr( &*tied_item, *this );
3050+
process_item_valptr( &*battery_item, *this );
3051+
}
30343052
}
30353053

30363054
namespace

src/monster.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ class monster : public Creature, public location_visitable<monster>
656656
void nursebot_operate( player *dragged_foe );
657657

658658
protected:
659+
bool has_processable_items = false;
659660
location_ptr<item, false> tied_item; // item used to tie the monster
660661
location_ptr<item, false> tack_item; // item representing saddle and reins and such
661662
location_ptr<item, false> armor_item; // item of armor the monster may be wearing

0 commit comments

Comments
 (0)