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

Code before:
            }

            uniqueCommands.insert(c);

            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();
        // If this is the first time we've opened the commandline mode and
        // there aren't any recent commands, then just store the new command.
        if (!recentCommands)
        {
            ApplicationState::SharedInstance().RecentCommands(single_threaded_vector(std::move(std::vector{ command })));
            return;
        }

        const auto numNewRecentCommands = std::min(recentCommands.Size() + 1, CommandLineHistoryLength);

        std::vector<hstring> newRecentCommands;
        newRecentCommands.reserve(numNewRecentCommands);

        std::unordered_set<hstring> uniqueCommands;
        uniqueCommands.reserve(numNewRecentCommands);

        newRecentCommands.push_back(command);
        uniqueCommands.insert(command);

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

            if (uniqueCommands.count(c) > 0)
            {
                continue;
            }

Review comment (suggestion):
I believe this is unnecessary, considering that users will only have an empty command history once (after enabling the feature).

Code after:
    // 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();
        // If this is the first time we've opened the commandline mode and
        // there aren't any recent commands, then just store the new command.
        if (!recentCommands)
        {
            ApplicationState::SharedInstance().RecentCommands(single_threaded_vector(std::move(std::vector{ command })));
            return;
        }

        const auto numNewRecentCommands = std::min(recentCommands.Size() + 1, CommandLineHistoryLength);

        std::vector<hstring> newRecentCommands;
        newRecentCommands.reserve(numNewRecentCommands);

        std::unordered_set<hstring> uniqueCommands;
        uniqueCommands.reserve(numNewRecentCommands);

        newRecentCommands.push_back(command);
        uniqueCommands.insert(command);

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

            if (uniqueCommands.emplace(c).second)
            {
                newRecentCommands.push_back(c);
            }
        }

        ApplicationState::SharedInstance().RecentCommands(single_threaded_vector(std::move(newRecentCommands)));
    }
}