Repository: microsoft/terminal
File: src/cascadia/TerminalApp/CommandPalette.cpp (C++)

Code before:
        {
            _listViewItemsCache[itemContainer.ContentTemplate()].insert(itemContainer);
            itemContainer.DataContext(nullptr);
        }
        else
        {
            itemContainer.DataContext(args.Item());
        }
    }

    // Method Description:
    // - Reads the list of recent commands from the persistent application state
    // Return Value:
    // - The list of FilteredCommand representing the ones stored in the state
    Windows::Foundation::Collections::IVector<winrt::TerminalApp::FilteredCommand> CommandPalette::_loadRecentCommands() const
    {
        const auto parsedCommands = winrt::single_threaded_vector<winrt::TerminalApp::FilteredCommand>();
        const auto recentCommands = ApplicationState::SharedInstance().RecentCommands();
        std::for_each(begin(recentCommands), end(recentCommands), [&](const auto& c) {
            auto parsedCommand = _buildCommandLineCommand(c.c_str());
            if (parsedCommand.has_value())
            {
                parsedCommands.Append(parsedCommand.value());
            }
        });
        return parsedCommands;
    }

    // Method Description:
    // - Update recent commands by putting the provided command as most recent.
    // Upon race condition might override an update made by another window.
    // Return Value:
    // - <none>
    void CommandPalette::_updateRecentCommands(const winrt::hstring& command) const
    {
        std::vector<hstring> newRecentCommands;
        newRecentCommands.push_back(command);

        const auto recentCommands = ApplicationState::SharedInstance().RecentCommands();
        std::copy_n(begin(recentCommands), std::min(recentCommands.Size(), CommandLineHistoryLength - 1), back_inserter(newRecentCommands));
        ApplicationState::SharedInstance().RecentCommands(winrt::single_threaded_vector<hstring>(std::move(newRecentCommands)));
    }
}


Review comment (suggestion):
```suggestion
        const auto recentCommands = ApplicationState::SharedInstance().RecentCommands();

        std::vector<winrt::TerminalApp::FilteredCommand> parsedCommands;
        parsedCommands.reserve(recentCommands.Size());

        for (const auto& c : recentCommands) {
            if (auto parsedCommand = _buildCommandLineCommand(c))
            {
                parsedCommands.Append(*parsedCommand);
            }
        }

        return winrt::single_threaded_vector(std::move(parsedCommands));
```

This does a few things:
* reserve the required memory upfront for `parsedCommands`
* STL classes are significantly faster than COM classes (~3x)
* for-range loop, which compilers _love_ and is easier to read than `std::for_each` (IMO)
* if with init-statement, means we don't have to ask for `has_value()` again, because the if condition will check for truthy-ness by default

Code after:
            _listViewItemsCache[itemContainer.ContentTemplate()].insert(itemContainer);
            itemContainer.DataContext(nullptr);
        }
        else
        {
            itemContainer.DataContext(args.Item());
        }
    }

    // Method Description:
    // - Reads the list of recent commands from the persistent application state
    // Return Value:
    // - The list of FilteredCommand representing the ones stored in the state
    IVector<TerminalApp::FilteredCommand> CommandPalette::_loadRecentCommands()
    {
        const auto recentCommands = ApplicationState::SharedInstance().RecentCommands();
        std::vector<TerminalApp::FilteredCommand> parsedCommands;
        parsedCommands.reserve(std::min(recentCommands.Size(), CommandLineHistoryLength));

        for (const auto& c : recentCommands)
        {
            if (parsedCommands.size() >= CommandLineHistoryLength)
            {
                // Don't load more than CommandLineHistoryLength commands
                break;
            }

            if (const auto parsedCommand = _buildCommandLineCommand(c))
            {
                parsedCommands.push_back(*parsedCommand);
            }
        }
        return single_threaded_vector(std::move(parsedCommands));
    }

    // Method Description:
    // - Update recent commands by putting the provided command as most recent.
    // Upon race condition might override an update made by another window.
    // Return Value:
    // - <none>
    void CommandPalette::_updateRecentCommands(const hstring& command)
    {
        const auto recentCommands = ApplicationState::SharedInstance().RecentCommands();
        const auto countToCopy = std::min(recentCommands.Size(), CommandLineHistoryLength - 1);
        std::vector<hstring> newRecentCommands{ countToCopy + 1 };
        til::at(newRecentCommands, 0) = command;
        if (countToCopy)
        {
            recentCommands.GetMany(0, { newRecentCommands.data() + 1, countToCopy });
        }
        ApplicationState::SharedInstance().RecentCommands(single_threaded_vector(std::move(newRecentCommands)));