diff --git a/src/env.cpp b/src/env.cpp index ecadab44d..288497a12 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -635,7 +635,7 @@ std::shared_ptr> env_scoped_impl_t::create_e assert(var && "Variable should be present in uvars"); // Note that std::map::insert does NOT overwrite a value already in the map, // which we depend on here. - vals.insert(std::make_pair(key, *var)); + vals.insert(std::move(std::make_pair(std::move(key), std::move(*var)))); } } diff --git a/src/env.h b/src/env.h index 5cf907e2f..263292bea 100644 --- a/src/env.h +++ b/src/env.h @@ -5,9 +5,9 @@ #include #include -#include #include #include +#include #include #include "common.h" @@ -186,7 +186,7 @@ class env_var_t { } bool operator!=(const env_var_t &rhs) const { return !(*this == rhs); } }; -typedef std::map var_table_t; +typedef std::unordered_map var_table_t; /// An environment is read-only access to variable values. class environment_t { diff --git a/src/env_universal_common.cpp b/src/env_universal_common.cpp index c01845f7f..295ad5884 100644 --- a/src/env_universal_common.cpp +++ b/src/env_universal_common.cpp @@ -441,7 +441,14 @@ std::string env_universal_t::serialize_with_vars(const var_table_t &vars) { contents.append(SAVE_MSG); contents.append("# VERSION: " UVARS_VERSION_3_0 "\n"); - for (const auto &kv : vars) { + // Preserve legacy behavior by sorting the values first + typedef std::pair, std::reference_wrapper> env_pair_t; + std::vector cloned(vars.begin(), vars.end()); + std::sort(cloned.begin(), cloned.end(), [](const env_pair_t &p1, const env_pair_t &p2) { + return p1.first.get() < p2.first.get(); + }); + + for (const auto &kv : cloned) { // Append the entry. Note that append_file_entry may fail, but that only affects one // variable; soldier on. const wcstring &key = kv.first;