Skip to content

Commit 593328a

Browse files
committed
add thread-local cache for string handle lookups
1 parent 3fc05df commit 593328a

4 files changed

Lines changed: 47 additions & 12 deletions

File tree

cpp/include/ittapi_domain.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,26 +93,26 @@ class Domain
9393

9494
void task_begin(std::string_view name) const
9595
{
96-
__itt_string_handle* h = detail::create_string_handle(std::string(name).c_str());
96+
__itt_string_handle* h = detail::get_or_create_string_handle(name);
9797
__itt_task_begin(m_handle, detail::make_null_id(), detail::make_null_id(), h);
9898
}
9999

100100
void task_begin(std::string_view name, __itt_id taskid, __itt_id parentid) const
101101
{
102-
__itt_string_handle* h = detail::create_string_handle(std::string(name).c_str());
102+
__itt_string_handle* h = detail::get_or_create_string_handle(name);
103103
__itt_task_begin(m_handle, taskid, parentid, h);
104104
}
105105

106106
#if ITT_PLATFORM == ITT_PLATFORM_WIN
107107
void task_begin(std::wstring_view name) const
108108
{
109-
__itt_string_handle* h = detail::create_string_handle(std::wstring(name).c_str());
109+
__itt_string_handle* h = detail::get_or_create_string_handle(name);
110110
__itt_task_begin(m_handle, detail::make_null_id(), detail::make_null_id(), h);
111111
}
112112

113113
void task_begin(std::wstring_view name, __itt_id taskid, __itt_id parentid) const
114114
{
115-
__itt_string_handle* h = detail::create_string_handle(std::wstring(name).c_str());
115+
__itt_string_handle* h = detail::get_or_create_string_handle(name);
116116
__itt_task_begin(m_handle, taskid, parentid, h);
117117
}
118118
#endif

cpp/include/ittapi_region.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ScopedRegion
2525
, m_id(detail::make_null_id())
2626
, m_active(true)
2727
{
28-
__itt_string_handle* h = detail::create_string_handle(std::string(name).c_str());
28+
__itt_string_handle* h = detail::get_or_create_string_handle(name);
2929
__itt_region_begin(m_domain, m_id, detail::make_null_id(), h);
3030
}
3131

@@ -35,7 +35,7 @@ class ScopedRegion
3535
, m_id(id)
3636
, m_active(true)
3737
{
38-
__itt_string_handle* h = detail::create_string_handle(std::string(name).c_str());
38+
__itt_string_handle* h = detail::get_or_create_string_handle(name);
3939
__itt_region_begin(m_domain, m_id, parentid, h);
4040
}
4141

@@ -45,7 +45,7 @@ class ScopedRegion
4545
, m_id(detail::make_null_id())
4646
, m_active(true)
4747
{
48-
__itt_string_handle* h = detail::create_string_handle(std::wstring(name).c_str());
48+
__itt_string_handle* h = detail::get_or_create_string_handle(name);
4949
__itt_region_begin(m_domain, m_id, detail::make_null_id(), h);
5050
}
5151

@@ -55,7 +55,7 @@ class ScopedRegion
5555
, m_id(id)
5656
, m_active(true)
5757
{
58-
__itt_string_handle* h = detail::create_string_handle(std::wstring(name).c_str());
58+
__itt_string_handle* h = detail::get_or_create_string_handle(name);
5959
__itt_region_begin(m_domain, m_id, parentid, h);
6060
}
6161
#endif

cpp/include/ittapi_task.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ScopedTask
2424
: m_domain(domain)
2525
, m_active(true)
2626
{
27-
__itt_string_handle* h = detail::create_string_handle(std::string(name).c_str());
27+
__itt_string_handle* h = detail::get_or_create_string_handle(name);
2828
__itt_task_begin(m_domain, detail::make_null_id(), detail::make_null_id(), h);
2929
}
3030

@@ -33,7 +33,7 @@ class ScopedTask
3333
: m_domain(domain)
3434
, m_active(true)
3535
{
36-
__itt_string_handle* h = detail::create_string_handle(std::string(name).c_str());
36+
__itt_string_handle* h = detail::get_or_create_string_handle(name);
3737
__itt_task_begin(m_domain, taskid, parentid, h);
3838
}
3939

@@ -42,7 +42,7 @@ class ScopedTask
4242
: m_domain(domain)
4343
, m_active(true)
4444
{
45-
__itt_string_handle* h = detail::create_string_handle(std::wstring(name).c_str());
45+
__itt_string_handle* h = detail::get_or_create_string_handle(name);
4646
__itt_task_begin(m_domain, detail::make_null_id(), detail::make_null_id(), h);
4747
}
4848

@@ -51,7 +51,7 @@ class ScopedTask
5151
: m_domain(domain)
5252
, m_active(true)
5353
{
54-
__itt_string_handle* h = detail::create_string_handle(std::wstring(name).c_str());
54+
__itt_string_handle* h = detail::get_or_create_string_handle(name);
5555
__itt_task_begin(m_domain, taskid, parentid, h);
5656
}
5757
#endif

cpp/include/ittapi_utils.hpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
#include <ittnotify.h>
1010

11+
#include <string_view>
12+
#include <unordered_map>
13+
1114
namespace ittapi
1215
{
1316
namespace detail
@@ -31,6 +34,22 @@ inline __itt_string_handle* create_string_handle(const char* name) noexcept
3134
#endif
3235
}
3336

37+
inline __itt_string_handle* get_or_create_string_handle(std::string_view name)
38+
{
39+
thread_local std::unordered_map<std::string_view, __itt_string_handle*> cache;
40+
auto it = cache.find(name);
41+
if (it != cache.end())
42+
{
43+
return it->second;
44+
}
45+
__itt_string_handle* h = create_string_handle(std::string(name).c_str());
46+
if (h != nullptr)
47+
{
48+
cache.emplace(h->strA, h);
49+
}
50+
return h;
51+
}
52+
3453
inline void thread_set_name(const char* name) noexcept
3554
{
3655
#if ITT_PLATFORM == ITT_PLATFORM_WIN
@@ -52,6 +71,22 @@ inline __itt_string_handle* create_string_handle(const wchar_t* name) noexcept
5271
return __itt_string_handle_createW(name);
5372
}
5473

74+
inline __itt_string_handle* get_or_create_string_handle(std::wstring_view name)
75+
{
76+
thread_local std::unordered_map<std::wstring_view, __itt_string_handle*> cache;
77+
auto it = cache.find(name);
78+
if (it != cache.end())
79+
{
80+
return it->second;
81+
}
82+
__itt_string_handle* h = create_string_handle(std::wstring(name).c_str());
83+
if (h != nullptr)
84+
{
85+
cache.emplace(h->strW, h);
86+
}
87+
return h;
88+
}
89+
5590
inline void thread_set_name(const wchar_t* name) noexcept
5691
{
5792
__itt_thread_set_nameW(name);

0 commit comments

Comments
 (0)