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
revert-21172-masked_select_api
Yihua Xu 6 years ago committed by Tao Luo
parent 952508527a
commit b085ecc258

@ -22,9 +22,8 @@ namespace paddle {
namespace operators { namespace operators {
namespace jit { namespace jit {
std::unordered_map<std::string, std::shared_ptr<void>>& GetFuncCacheMap() { std::map<size_t, std::shared_ptr<void>>& GetFuncCacheMap() {
static thread_local std::unordered_map<std::string, std::shared_ptr<void>> static thread_local std::map<size_t, std::shared_ptr<void>> g_func_cache_map;
g_func_cache_map;
return g_func_cache_map; return g_func_cache_map;
} }

@ -15,6 +15,7 @@
#pragma once #pragma once
#include <iostream> #include <iostream>
#include <map>
#include <memory> #include <memory>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
@ -176,8 +177,7 @@ typename KernelTuple::func_type GetDefaultBestFunc(
return funcs[0]; return funcs[0];
} }
extern std::unordered_map<std::string, std::shared_ptr<void>>& extern std::map<size_t, std::shared_ptr<void>>& GetFuncCacheMap();
GetFuncCacheMap();
template <typename KernelTuple, typename PlaceType> template <typename KernelTuple, typename PlaceType>
class KernelFuncs { class KernelFuncs {
@ -185,7 +185,7 @@ class KernelFuncs {
KernelFuncs() = default; KernelFuncs() = default;
static KernelFuncs& Cache() { static KernelFuncs& Cache() {
auto& func_cache_map = GetFuncCacheMap(); auto& func_cache_map = GetFuncCacheMap();
std::string key = typeid(KernelFuncs<KernelTuple, PlaceType>).name(); auto key = typeid(KernelFuncs<KernelTuple, PlaceType>).hash_code();
auto iter = func_cache_map.find(key); auto iter = func_cache_map.find(key);
if (iter != func_cache_map.end()) { if (iter != func_cache_map.end()) {
return *(KernelFuncs<KernelTuple, PlaceType>*)(iter->second.get()); return *(KernelFuncs<KernelTuple, PlaceType>*)(iter->second.get());

@ -21,9 +21,8 @@ namespace paddle {
namespace operators { namespace operators {
namespace jit { namespace jit {
std::unordered_map<std::string, std::shared_ptr<void>>& GetJITCodesMap() { std::map<size_t, std::shared_ptr<void>>& GetJITCodesMap() {
static thread_local std::unordered_map<std::string, std::shared_ptr<void>> static thread_local std::map<size_t, std::shared_ptr<void>> g_jit_codes_map;
g_jit_codes_map;
return g_jit_codes_map; return g_jit_codes_map;
} }

@ -14,6 +14,7 @@
#pragma once #pragma once
#include <map>
#include <memory> // for unique_ptr #include <memory> // for unique_ptr
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
@ -28,7 +29,7 @@ namespace paddle {
namespace operators { namespace operators {
namespace jit { namespace jit {
extern std::unordered_map<std::string, std::shared_ptr<void>>& GetJITCodesMap(); extern std::map<size_t, std::shared_ptr<void>>& GetJITCodesMap();
template <KernelType KT> template <KernelType KT>
class JitCodePool { class JitCodePool {
@ -39,7 +40,7 @@ class JitCodePool {
JitCodePool() = default; JitCodePool() = default;
static JitCodePool& Instance() { static JitCodePool& Instance() {
auto& jit_codes_map = GetJITCodesMap(); auto& jit_codes_map = GetJITCodesMap();
std::string key = typeid(JitCodePool<KT>).name(); auto key = typeid(JitCodePool<KT>).hash_code();
auto iter = jit_codes_map.find(key); auto iter = jit_codes_map.find(key);
if (iter != jit_codes_map.end()) { if (iter != jit_codes_map.end()) {
return *(JitCodePool<KT>*)(iter->second.get()); return *(JitCodePool<KT>*)(iter->second.get());

Loading…
Cancel
Save