Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to improve update_last_irreversible_block performance #2045

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libraries/chain/db_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,8 @@ void database::init_genesis(const genesis_state_type& genesis_state)
p.active_witnesses.insert(witness_id_type(i));
}
});
for( const auto witness : get_global_properties().active_witnesses )
modify( get<witness_object>( witness ), [] ( witness_object& w ) { w.is_active = true; } );

// Enable fees
modify(get_global_properties(), [&genesis_state](global_property_object& p) {
Expand Down
15 changes: 15 additions & 0 deletions libraries/chain/db_maint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,15 @@ void database::update_active_witnesses()
}
} );

const witness_object& last_witness = wits.back();
for( const auto witness_id : gpo.active_witnesses )
{
const auto& witness = get<witness_object>( witness_id );
if( _vote_tally_buffer[witness.vote_id] < _vote_tally_buffer[last_witness.vote_id]
|| (_vote_tally_buffer[witness.vote_id] == _vote_tally_buffer[last_witness.vote_id]
&& witness.id > last_witness.id) )
modify( witness, [] ( witness_object& w ) { w.is_active = false; } );
}
modify( gpo, [&wits]( global_property_object& gp )
{
gp.active_witnesses.clear();
Expand All @@ -296,6 +305,12 @@ void database::update_active_witnesses()
return w.id;
});
});
for( const auto witness_id : gpo.active_witnesses )
{
const auto& witness = get<witness_object>( witness_id );
if( !witness.is_active )
modify( witness, [] ( witness_object& w ) { w.is_active = true; } );
}

} FC_CAPTURE_AND_RETHROW() }

Expand Down
29 changes: 8 additions & 21 deletions libraries/chain/db_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,32 +107,19 @@ void database::update_last_irreversible_block()
const global_property_object& gpo = get_global_properties();
const dynamic_global_property_object& dpo = get_dynamic_global_properties();

// TODO for better performance, move this to db_maint, because only need to do it once per maintenance interval
vector< const witness_object* > wit_objs;
wit_objs.reserve( gpo.active_witnesses.size() );
for( const witness_id_type& wid : gpo.active_witnesses )
wit_objs.push_back( &(wid(*this)) );

static_assert( GRAPHENE_IRREVERSIBLE_THRESHOLD > 0, "irreversible threshold must be nonzero" );

// 1 1 1 2 2 2 2 2 2 2 -> 2 .3*10 = 3
// 1 1 1 1 1 1 1 2 2 2 -> 1
// 3 3 3 3 3 3 3 3 3 3 -> 3
// 3 3 3 4 4 4 4 4 4 4 -> 4

size_t offset = ((GRAPHENE_100_PERCENT - GRAPHENE_IRREVERSIBLE_THRESHOLD) * wit_objs.size() / GRAPHENE_100_PERCENT);

std::nth_element( wit_objs.begin(), wit_objs.begin() + offset, wit_objs.end(),
[]( const witness_object* a, const witness_object* b )
{
return a->last_confirmed_block_num < b->last_confirmed_block_num;
} );

uint32_t new_last_irreversible_block_num = wit_objs[offset]->last_confirmed_block_num;
const size_t offset = (GRAPHENE_100_PERCENT - GRAPHENE_IRREVERSIBLE_THRESHOLD)
* gpo.active_witnesses.size() / GRAPHENE_100_PERCENT;
const auto& witness_idx = get_index_type<witness_index>().indices().get<by_last_block>();
auto itr = witness_idx.end();
for( uint32_t i = gpo.active_witnesses.size() - offset; i > 0; --i )
--itr;
Copy link
Member

Choose a reason for hiding this comment

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

I think this is the main place where the performance got (slightly) lost, because we walk number_of_active_witnesses/2 steps on each new block. Each step costs log(number_of_all_witnesses).

My thoughts:

  • maintain a small set containing the witnesses that produced a block after LIB and are still in the active witness list;
  • maintain a pointer/iterator to the current witness that holds the LIB, and probably another pointer/iterator to its next witness in the set for better performance;
  • when a new block comes,
    • if its witness is already in the set, LIB does not change;
    • otherwise, add the new witness into the set,
      • if the new set size is smaller than or equal to number_of_active_witnesses/2, LIB does not change;
      • otherwise, move the pointer/iterator and LIB to the next witness in the set, and remove the old one from the set.

With this approach we walk one, two or three steps on each new block within a smaller set with cost log(number_of_active_witnesses) (one for the lookup (if a witness is already in the set), one for adding it, another for removing the old). This can probably be done with a secondary index to keep data consistency. Not sure how much performance gain it will bring though.

const auto new_last_irreversible_block_num = itr->last_confirmed_block_num;

if( new_last_irreversible_block_num > dpo.last_irreversible_block_num )
{
modify( dpo, [&]( dynamic_global_property_object& _dpo )
modify( dpo, [new_last_irreversible_block_num]( dynamic_global_property_object& _dpo )
{
_dpo.last_irreversible_block_num = new_last_irreversible_block_num;
} );
Expand Down
9 changes: 9 additions & 0 deletions libraries/chain/include/graphene/chain/witness_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include <graphene/protocol/asset.hpp>
#include <graphene/db/generic_index.hpp>

#include <boost/multi_index/composite_key.hpp>

namespace graphene { namespace chain {
using namespace graphene::db;

Expand All @@ -44,6 +46,7 @@ namespace graphene { namespace chain {
string url;
int64_t total_missed = 0;
uint32_t last_confirmed_block_num = 0;
bool is_active = false;

witness_object() : vote_id(vote_id_type::witness) {}
};
Expand All @@ -62,6 +65,12 @@ namespace graphene { namespace chain {
>,
ordered_unique< tag<by_vote_id>,
member<witness_object, vote_id_type, &witness_object::vote_id>
>,
ordered_non_unique< tag<by_last_block>, // non-unique because multiple can have last == 0
composite_key<witness_object,
member<witness_object, bool, &witness_object::is_active>,
member<witness_object, uint32_t, &witness_object::last_confirmed_block_num>
>
>
>
>;
Expand Down
1 change: 1 addition & 0 deletions libraries/chain/small_objects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ FC_REFLECT_DERIVED_NO_TYPENAME( graphene::chain::witness_object, (graphene::db::
(url)
(total_missed)
(last_confirmed_block_num)
(is_active)
)

FC_REFLECT_DERIVED_NO_TYPENAME(
Expand Down