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

Code before:
                // Don't load more than CommandLineHistoryLength commands
                break;
            }

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

            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

Review comment (refactor):
On saving I don't think you need to deduplicate everything and have a separate `uniqueCommands`. All you have to do is create the one output vector, add the new command, and then add in commands from `RecentCommands` where they are not equal to the current command.

Code after:
            {
                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.emplace(c).second)
            {
                newRecentCommands.push_back(c);
            }
        }

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