1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 19:57:45 +00:00

Spider: Avoid reallocations for Vectors when possible

This commit is contained in:
Gunnar Beutner 2021-07-24 00:57:36 +02:00 committed by Andreas Kling
parent d76b42599e
commit 807b79d89e

View file

@ -42,23 +42,25 @@ void Game::setup(Mode mode)
for (auto& stack : m_stacks) for (auto& stack : m_stacks)
stack.clear(); stack.clear();
m_new_deck.clear();
m_new_game_animation_pile = 0; m_new_game_animation_pile = 0;
m_score = 500; m_score = 500;
update_score(0); update_score(0);
NonnullRefPtrVector<Card> deck;
deck.ensure_capacity(Card::card_count * 2);
for (int i = 0; i < Card::card_count; ++i) { for (int i = 0; i < Card::card_count; ++i) {
switch (m_mode) { switch (m_mode) {
case Mode::SingleSuit: case Mode::SingleSuit:
for (int j = 0; j < 8; j++) { for (int j = 0; j < 8; j++) {
m_new_deck.append(Card::construct(Card::Type::Spades, i)); deck.append(Card::construct(Card::Type::Spades, i));
} }
break; break;
case Mode::TwoSuit: case Mode::TwoSuit:
for (int j = 0; j < 4; j++) { for (int j = 0; j < 4; j++) {
m_new_deck.append(Card::construct(Card::Type::Spades, i)); deck.append(Card::construct(Card::Type::Spades, i));
m_new_deck.append(Card::construct(Card::Type::Hearts, i)); deck.append(Card::construct(Card::Type::Hearts, i));
} }
break; break;
default: default:
@ -67,9 +69,10 @@ void Game::setup(Mode mode)
} }
} }
for (int x = 0; x < 3; x++) m_new_deck.clear_with_capacity();
for (uint8_t i = 0; i < Card::card_count * 8; ++i) m_new_deck.ensure_capacity(deck.size());
m_new_deck.append(m_new_deck.take(rand() % m_new_deck.size())); while (!deck.is_empty())
m_new_deck.append(deck.take(rand() % deck.size()));
m_new_game_animation = true; m_new_game_animation = true;
start_timer(s_timer_interval_ms); start_timer(s_timer_interval_ms);