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

Code before:
    {
        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):
Previously this was accomplished using:
```cpp
if (_commandLineHistory.Size() == CommandLineHistoryLength)
{
    _commandLineHistory.RemoveAtEnd();
}
_commandLineHistory.InsertAt(0, filteredCommand.value());
```

I suspect such an approach to be easier to understand and also to be significantly faster than this unfortunately.

Code after:
        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)));
    }
}