Repository: microsoft/terminal
File: src/cascadia/TerminalSettingsModel/ActionMap.cpp (C++)

Code before:

        {
            // Check if all the directories are already in the cache
            const auto& cache{ _cwdLocalSnippetsCache.lock_shared() };

            bool allSnippetsCached = true;
            for (const auto& dir : directories)
            {
                if (!cache->contains(dir))
                {
                    allSnippetsCached = false;
                    break;
                }
            }
            if (allSnippetsCached)
            {
                // Load snippets from directories in reverse order.
                // This ensures that we prioritize snippets closer to the cwd.
                // The map makes it easy to avoid duplicates.
                std::unordered_map<hstring, Model::Command> localSnippetsMap;
                for (decltype(directories)::reverse_iterator rit = directories.rbegin(); rit != directories.rend(); ++rit)
                {
                    // register snippets from cache
                    std::ranges::for_each(cache->at(*rit), [&localSnippetsMap](const auto& kvPair) {
                        localSnippetsMap.insert_or_assign(kvPair.first, kvPair.second);
                    });
                }

                std::vector<Model::Command> localSnippets;
                localSnippets.reserve(localSnippetsMap.size());
                std::ranges::transform(localSnippetsMap,
                                       std::back_inserter(localSnippets),
                                       [](const auto& kvPair) { return kvPair.second; });
                co_return winrt::single_threaded_vector<Model::Command>(_filterToSnippets(NameMap(),
                                                                                          currentCommandline,
                                                                                          localSnippets));
            }
        } // release the lock on the cache

        // Don't do I/O on the main thread
        co_await winrt::resume_background();

        // Load snippets from directories in reverse order.
        // This ensures that we prioritize snippets closer to the cwd.
        // The map makes it easy to avoid duplicates.
        const auto& cache{ _cwdLocalSnippetsCache.lock() };
        std::unordered_map<hstring, Model::Command> localSnippetsMap;
        for (decltype(directories)::reverse_iterator rit = directories.rbegin(); rit != directories.rend(); ++rit)
        {
            const auto& dir = *rit;
            if (const auto cacheIterator = cache->find(dir); cacheIterator != cache->end())

Review comment (performance):
A plain `for(auto&& kvPair : cache->at(*rit))` seems more readable to me.
This and the previous comment are repeated once more below.

Code after:

        {
            // Check if all the directories are already in the cache
            const auto& cache{ _cwdLocalSnippetsCache.lock_shared() };
            if (std::ranges::all_of(directories, [&](auto&& dir) { return cache->contains(dir); }))
            {
                // Load snippets from directories in reverse order.
                // This ensures that we prioritize snippets closer to the cwd.
                // The map makes it easy to avoid duplicates.
                std::unordered_map<hstring, Model::Command> localSnippetsMap;
                for (auto rit = directories.rbegin(); rit != directories.rend(); ++rit)
                {
                    // register snippets from cache
                    for (const auto& [name, snippet] : cache->at(*rit))
                    {
                        localSnippetsMap.insert_or_assign(name, snippet);
                    }
                }

                std::vector<Model::Command> localSnippets;
                localSnippets.reserve(localSnippetsMap.size());
                std::ranges::transform(localSnippetsMap,
                                       std::back_inserter(localSnippets),
                                       [](const auto& kvPair) { return kvPair.second; });
                co_return winrt::single_threaded_vector<Model::Command>(_filterToSnippets(NameMap(),
                                                                                          currentCommandline,
                                                                                          localSnippets));
            }
        } // release the lock on the cache

        // Don't do I/O on the main thread
        co_await winrt::resume_background();

        // Load snippets from directories in reverse order.
        // This ensures that we prioritize snippets closer to the cwd.
        // The map makes it easy to avoid duplicates.
        const auto& cache{ _cwdLocalSnippetsCache.lock() };
        std::unordered_map<hstring, Model::Command> localSnippetsMap;
        for (auto rit = directories.rbegin(); rit != directories.rend(); ++rit)
        {
            const auto& dir = *rit;
            if (const auto cacheIterator = cache->find(dir); cacheIterator != cache->end())
            {
                // register snippets from cache
                for (const auto& [name, snippet] : cache->at(*rit))
                {
                    localSnippetsMap.insert_or_assign(name, snippet);
                }
            }
            else
            {