From b085ecc25896c0a4aea70bcfff316683a76ec5e4 Mon Sep 17 00:00:00 2001 From: Yihua Xu Date: Fri, 22 Nov 2019 10:29:43 +0800 Subject: [PATCH] Avoid the string as the key of map to improve the jit performance (#21292) * Avoid the string as the key of map to improve the jit performance. test=develop * Use map to replace unordered_map. test=develop --- paddle/fluid/operators/jit/helper.cc | 5 ++--- paddle/fluid/operators/jit/helper.h | 6 +++--- paddle/fluid/operators/jit/kernel_pool.cc | 5 ++--- paddle/fluid/operators/jit/kernel_pool.h | 5 +++-- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/paddle/fluid/operators/jit/helper.cc b/paddle/fluid/operators/jit/helper.cc index 992228adb50..2952cdb8714 100644 --- a/paddle/fluid/operators/jit/helper.cc +++ b/paddle/fluid/operators/jit/helper.cc @@ -22,9 +22,8 @@ namespace paddle { namespace operators { namespace jit { -std::unordered_map>& GetFuncCacheMap() { - static thread_local std::unordered_map> - g_func_cache_map; +std::map>& GetFuncCacheMap() { + static thread_local std::map> g_func_cache_map; return g_func_cache_map; } diff --git a/paddle/fluid/operators/jit/helper.h b/paddle/fluid/operators/jit/helper.h index 9a2447a98fb..39e5ee2be15 100644 --- a/paddle/fluid/operators/jit/helper.h +++ b/paddle/fluid/operators/jit/helper.h @@ -15,6 +15,7 @@ #pragma once #include +#include #include #include #include @@ -176,8 +177,7 @@ typename KernelTuple::func_type GetDefaultBestFunc( return funcs[0]; } -extern std::unordered_map>& -GetFuncCacheMap(); +extern std::map>& GetFuncCacheMap(); template class KernelFuncs { @@ -185,7 +185,7 @@ class KernelFuncs { KernelFuncs() = default; static KernelFuncs& Cache() { auto& func_cache_map = GetFuncCacheMap(); - std::string key = typeid(KernelFuncs).name(); + auto key = typeid(KernelFuncs).hash_code(); auto iter = func_cache_map.find(key); if (iter != func_cache_map.end()) { return *(KernelFuncs*)(iter->second.get()); diff --git a/paddle/fluid/operators/jit/kernel_pool.cc b/paddle/fluid/operators/jit/kernel_pool.cc index 8eef8e47474..f1719be9873 100644 --- a/paddle/fluid/operators/jit/kernel_pool.cc +++ b/paddle/fluid/operators/jit/kernel_pool.cc @@ -21,9 +21,8 @@ namespace paddle { namespace operators { namespace jit { -std::unordered_map>& GetJITCodesMap() { - static thread_local std::unordered_map> - g_jit_codes_map; +std::map>& GetJITCodesMap() { + static thread_local std::map> g_jit_codes_map; return g_jit_codes_map; } diff --git a/paddle/fluid/operators/jit/kernel_pool.h b/paddle/fluid/operators/jit/kernel_pool.h index 548c8704126..48435cf6ef2 100644 --- a/paddle/fluid/operators/jit/kernel_pool.h +++ b/paddle/fluid/operators/jit/kernel_pool.h @@ -14,6 +14,7 @@ #pragma once +#include #include // for unique_ptr #include #include @@ -28,7 +29,7 @@ namespace paddle { namespace operators { namespace jit { -extern std::unordered_map>& GetJITCodesMap(); +extern std::map>& GetJITCodesMap(); template class JitCodePool { @@ -39,7 +40,7 @@ class JitCodePool { JitCodePool() = default; static JitCodePool& Instance() { auto& jit_codes_map = GetJITCodesMap(); - std::string key = typeid(JitCodePool).name(); + auto key = typeid(JitCodePool).hash_code(); auto iter = jit_codes_map.find(key); if (iter != jit_codes_map.end()) { return *(JitCodePool*)(iter->second.get()); -- GitLab