Skip to content

Commit

Permalink
Misc: Optimized storage of window settings data (reducing allocation …
Browse files Browse the repository at this point in the history
…count).
  • Loading branch information
ocornut committed Nov 5, 2019
1 parent 3929255 commit 4c13807
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Other Changes:
incorrectly locating the arrow hit position to the left of the frame. (#2451, #2438, #1897)
- DragScalar, SliderScalar, InputScalar: Added p_ prefix to parameter that are pointers to the data
to clarify how they are used, and more comments redirecting to the demo code. (#2844)
- Misc: Optimized storage of window settings data (reducing allocation count).
- Misc: Windows: Do not use _wfopen() if IMGUI_DISABLE_WIN32_FUNCTIONS is defined. (#2815)
- Docs: Improved and moved FAQ to docs/FAQ.md so it can be readable on the web. [@ButternCream, @ocornut]
- Docs: Added permanent redirect from https://www.dearimgui.org/faq to FAQ page.
Expand Down
12 changes: 7 additions & 5 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3809,8 +3809,7 @@ void ImGui::Shutdown(ImGuiContext* context)
g.PrivateClipboard.clear();
g.InputTextState.ClearFreeMemory();

for (int i = 0; i < g.SettingsWindows.Size; i++)
IM_DELETE(g.SettingsWindows[i].Name);
g.SettingsWindowsNames.clear();
g.SettingsWindows.clear();
g.SettingsHandlers.clear();

Expand Down Expand Up @@ -9205,8 +9204,10 @@ ImGuiWindowSettings* ImGui::CreateNewWindowSettings(const char* name)
if (const char* p = strstr(name, "###"))
name = p;
#endif
settings->Name = ImStrdup(name);
settings->ID = ImHashStr(name);
size_t name_len = strlen(name);
settings->NameOffset = g.SettingsWindowsNames.size();
g.SettingsWindowsNames.append(name, name + name_len + 1); // Append with zero terminator
settings->ID = ImHashStr(name, name_len);
return settings;
}

Expand Down Expand Up @@ -9387,7 +9388,8 @@ static void SettingsHandlerWindow_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandl
for (int i = 0; i != g.SettingsWindows.Size; i++)
{
const ImGuiWindowSettings* settings = &g.SettingsWindows[i];
buf->appendf("[%s][%s]\n", handler->TypeName, settings->Name);
const char* settings_name = g.SettingsWindowsNames.c_str() + settings->NameOffset;
buf->appendf("[%s][%s]\n", handler->TypeName, settings_name);
buf->appendf("Pos=%d,%d\n", settings->Pos.x, settings->Pos.y);
buf->appendf("Size=%d,%d\n", settings->Size.x, settings->Size.y);
buf->appendf("Collapsed=%d\n", settings->Collapsed);
Expand Down
6 changes: 4 additions & 2 deletions imgui_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -662,15 +662,16 @@ struct IMGUI_API ImGuiInputTextState
};

// Windows data saved in imgui.ini file
// Because we never destroy or rename ImGuiWindowSettings, we can store the names in a separate buffer easily.
struct ImGuiWindowSettings
{
char* Name;
int NameOffset; // Offset into SettingsWindowNames[]
ImGuiID ID;
ImVec2ih Pos;
ImVec2ih Size;
bool Collapsed;

ImGuiWindowSettings() { Name = NULL; ID = 0; Pos = Size = ImVec2ih(0, 0); Collapsed = false; }
ImGuiWindowSettings() { NameOffset = -1; ID = 0; Pos = Size = ImVec2ih(0, 0); Collapsed = false; }
};

struct ImGuiSettingsHandler
Expand Down Expand Up @@ -1039,6 +1040,7 @@ struct ImGuiContext
ImGuiTextBuffer SettingsIniData; // In memory .ini settings
ImVector<ImGuiSettingsHandler> SettingsHandlers; // List of .ini settings handlers
ImVector<ImGuiWindowSettings> SettingsWindows; // ImGuiWindow .ini settings entries (parsed from the last loaded .ini file and maintained on saving)
ImGuiTextBuffer SettingsWindowsNames; // Names for SettingsWindows

// Logging
bool LogEnabled;
Expand Down

0 comments on commit 4c13807

Please sign in to comment.