Skip to content

Commit

Permalink
butano: bg blocks manager allocation optimized
Browse files Browse the repository at this point in the history
  • Loading branch information
GValiente committed Nov 24, 2024
1 parent c543861 commit c2590fc
Showing 1 changed file with 25 additions and 58 deletions.
83 changes: 25 additions & 58 deletions butano/src/bn_bg_blocks_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,6 @@ namespace
{
item_type* item = &data.items.item(id);
int blocks_count = create_data.blocks_count;
bool fast_blocks_count_check = true;

if(padding_blocks_count)
{
Expand All @@ -1016,7 +1015,8 @@ namespace
break;

case status_type::TO_REMOVE:
fast_blocks_count_check = false;
data.free_blocks_count += new_item_blocks_count;
data.to_remove_blocks_count -= new_item_blocks_count;
break;

default:
Expand Down Expand Up @@ -1047,7 +1047,7 @@ namespace

if(create_item_at_back)
{
item->blocks_count -= uint8_t(blocks_count);
item->blocks_count = uint8_t(new_item_blocks_count);

switch(item->status())
{
Expand All @@ -1060,7 +1060,8 @@ namespace
break;

case status_type::TO_REMOVE:
fast_blocks_count_check = false;
data.free_blocks_count += new_item_blocks_count;
data.to_remove_blocks_count -= new_item_blocks_count;
break;

default:
Expand All @@ -1084,8 +1085,27 @@ namespace
}
}

switch(item->status())
{

case status_type::FREE:
data.free_blocks_count -= blocks_count;
break;

case status_type::USED:
BN_ERROR("Invalid item state");
break;

case status_type::TO_REMOVE:
data.to_remove_blocks_count -= blocks_count;
break;

default:
BN_ERROR("Invalid item status: ", int(item->status()));
break;
}

const uint16_t* data_ptr = create_data.data_ptr;
status_type old_status = item->status();
item->data = data_ptr;
item->blocks_count = uint8_t(blocks_count);
item->set_compression(create_data.compression);
Expand Down Expand Up @@ -1118,59 +1138,6 @@ namespace

item->commit = commit_item;

if(fast_blocks_count_check)
{
switch(old_status)
{

case status_type::FREE:
data.free_blocks_count -= blocks_count;
break;

case status_type::USED:
BN_ERROR("Invalid item state");
break;

case status_type::TO_REMOVE:
data.to_remove_blocks_count -= blocks_count;
break;

default:
BN_ERROR("Invalid item status: ", int(item->status()));
break;
}
}
else
{
int free_blocks_count = 0;
int to_remove_blocks_count = 0;

for(const item_type& data_item : data.items)
{
switch(data_item.status())
{

case status_type::FREE:
free_blocks_count += data_item.blocks_count;
break;

case status_type::USED:
break;

case status_type::TO_REMOVE:
to_remove_blocks_count += data_item.blocks_count;
break;

default:
BN_ERROR("Invalid item status: ", int(item->status()));
break;
}
}

data.free_blocks_count = free_blocks_count;
data.to_remove_blocks_count = to_remove_blocks_count;
}

return id;
}

Expand Down

0 comments on commit c2590fc

Please sign in to comment.