Skip to content

Commit

Permalink
Misc micro-optimizations related to hot-path of dealing with a large …
Browse files Browse the repository at this point in the history
…clipped tree.

Halved times by two in MSVC Debug x64.
  • Loading branch information
ocornut committed Jul 26, 2024
1 parent 692bee5 commit d42fa46
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2583,12 +2583,11 @@ void ImGuiStorage::BuildSortByKey()
{
ImQsort(Data.Data, (size_t)Data.Size, sizeof(ImGuiStoragePair), PairComparerByID);
}
IM_MSVC_RUNTIME_CHECKS_RESTORE

int ImGuiStorage::GetInt(ImGuiID key, int default_val) const
{
ImGuiStoragePair* it = ImLowerBound(const_cast<ImGuiStoragePair*>(Data.Data), const_cast<ImGuiStoragePair*>(Data.Data + Data.Size), key);
if (it == Data.end() || it->key != key)
if (it == Data.Data + Data.Size || it->key != key)
return default_val;
return it->val_i;
}
Expand All @@ -2601,15 +2600,15 @@ bool ImGuiStorage::GetBool(ImGuiID key, bool default_val) const
float ImGuiStorage::GetFloat(ImGuiID key, float default_val) const
{
ImGuiStoragePair* it = ImLowerBound(const_cast<ImGuiStoragePair*>(Data.Data), const_cast<ImGuiStoragePair*>(Data.Data + Data.Size), key);
if (it == Data.end() || it->key != key)
if (it == Data.Data + Data.Size || it->key != key)
return default_val;
return it->val_f;
}

void* ImGuiStorage::GetVoidPtr(ImGuiID key) const
{
ImGuiStoragePair* it = ImLowerBound(const_cast<ImGuiStoragePair*>(Data.Data), const_cast<ImGuiStoragePair*>(Data.Data + Data.Size), key);
if (it == Data.end() || it->key != key)
if (it == Data.Data + Data.Size || it->key != key)
return NULL;
return it->val_p;
}
Expand All @@ -2618,7 +2617,7 @@ void* ImGuiStorage::GetVoidPtr(ImGuiID key) const
int* ImGuiStorage::GetIntRef(ImGuiID key, int default_val)
{
ImGuiStoragePair* it = ImLowerBound(Data.Data, Data.Data + Data.Size, key);
if (it == Data.end() || it->key != key)
if (it == Data.Data + Data.Size || it->key != key)
it = Data.insert(it, ImGuiStoragePair(key, default_val));
return &it->val_i;
}
Expand All @@ -2631,15 +2630,15 @@ bool* ImGuiStorage::GetBoolRef(ImGuiID key, bool default_val)
float* ImGuiStorage::GetFloatRef(ImGuiID key, float default_val)
{
ImGuiStoragePair* it = ImLowerBound(Data.Data, Data.Data + Data.Size, key);
if (it == Data.end() || it->key != key)
if (it == Data.Data + Data.Size || it->key != key)
it = Data.insert(it, ImGuiStoragePair(key, default_val));
return &it->val_f;
}

void** ImGuiStorage::GetVoidPtrRef(ImGuiID key, void* default_val)
{
ImGuiStoragePair* it = ImLowerBound(Data.Data, Data.Data + Data.Size, key);
if (it == Data.end() || it->key != key)
if (it == Data.Data + Data.Size || it->key != key)
it = Data.insert(it, ImGuiStoragePair(key, default_val));
return &it->val_p;
}
Expand All @@ -2648,7 +2647,7 @@ void** ImGuiStorage::GetVoidPtrRef(ImGuiID key, void* default_val)
void ImGuiStorage::SetInt(ImGuiID key, int val)
{
ImGuiStoragePair* it = ImLowerBound(Data.Data, Data.Data + Data.Size, key);
if (it == Data.end() || it->key != key)
if (it == Data.Data + Data.Size || it->key != key)
Data.insert(it, ImGuiStoragePair(key, val));
else
it->val_i = val;
Expand All @@ -2662,7 +2661,7 @@ void ImGuiStorage::SetBool(ImGuiID key, bool val)
void ImGuiStorage::SetFloat(ImGuiID key, float val)
{
ImGuiStoragePair* it = ImLowerBound(Data.Data, Data.Data + Data.Size, key);
if (it == Data.end() || it->key != key)
if (it == Data.Data + Data.Size || it->key != key)
Data.insert(it, ImGuiStoragePair(key, val));
else
it->val_f = val;
Expand All @@ -2671,7 +2670,7 @@ void ImGuiStorage::SetFloat(ImGuiID key, float val)
void ImGuiStorage::SetVoidPtr(ImGuiID key, void* val)
{
ImGuiStoragePair* it = ImLowerBound(Data.Data, Data.Data + Data.Size, key);
if (it == Data.end() || it->key != key)
if (it == Data.Data + Data.Size || it->key != key)
Data.insert(it, ImGuiStoragePair(key, val));
else
it->val_p = val;
Expand All @@ -2682,6 +2681,7 @@ void ImGuiStorage::SetAllInt(int v)
for (int i = 0; i < Data.Size; i++)
Data[i].val_i = v;
}
IM_MSVC_RUNTIME_CHECKS_RESTORE

//-----------------------------------------------------------------------------
// [SECTION] ImGuiTextFilter
Expand Down Expand Up @@ -2749,7 +2749,7 @@ void ImGuiTextFilter::Build()

bool ImGuiTextFilter::PassFilter(const char* text, const char* text_end) const
{
if (Filters.empty())
if (Filters.Size == 0)
return true;

if (text == NULL)
Expand Down Expand Up @@ -8241,6 +8241,7 @@ bool ImGui::IsRectVisible(const ImVec2& rect_min, const ImVec2& rect_max)

// This is one of the very rare legacy case where we use ImGuiWindow methods,
// it should ideally be flattened at some point but it's been used a lots by widgets.
IM_MSVC_RUNTIME_CHECKS_OFF
ImGuiID ImGuiWindow::GetID(const char* str, const char* str_end)
{
ImGuiID seed = IDStack.back();
Expand Down Expand Up @@ -8385,6 +8386,8 @@ ImGuiID ImGui::GetID(int int_id)
ImGuiWindow* window = GImGui->CurrentWindow;
return window->GetID(int_id);
}
IM_MSVC_RUNTIME_CHECKS_RESTORE

//-----------------------------------------------------------------------------
// [SECTION] INPUTS
//-----------------------------------------------------------------------------
Expand Down

0 comments on commit d42fa46

Please sign in to comment.