From 4fa1a49765a4c725bb3bb3a053f488be633135dc Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Wed, 21 Jan 2026 23:00:59 +0100 Subject: [PATCH] :BeginProfile :EndProfile --- src/text_editor/commands.cpp | 10 +++++++++- src/text_editor/profiler.h | 9 ++++++++- src/text_editor/text_editor.cpp | 5 ++--- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index 905a882..bce37cc 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -462,4 +462,12 @@ void CMD_OpenLogs() { void CMD_Errors() { CMD_OpenLogs(); -} RegisterCommand(CMD_Errors, "", "Opens the text editor logs and clear error counter"); \ No newline at end of file +} RegisterCommand(CMD_Errors, "", "Opens the text editor logs and clear error counter"); + +void CMD_BeginProfile() { + BeginProfiler(); +} RegisterCommand(CMD_BeginProfile, "", "Start gathering profile data"); + +void CMD_EndProfile() { + EndProfiler(); +} RegisterCommand(CMD_EndProfile, "", "Stop gathering profile data and write to disk"); \ No newline at end of file diff --git a/src/text_editor/profiler.h b/src/text_editor/profiler.h index 0bb1970..b32f230 100644 --- a/src/text_editor/profiler.h +++ b/src/text_editor/profiler.h @@ -365,24 +365,30 @@ SPALL_FN bool spall_buffer_name_process(SpallProfile *ctx, SpallBuffer *wb, cons static SpallProfile spall_ctx; static SpallBuffer spall_buffer; uint64_t GetTimeNanos(void); +bool ActivelyProfiling = false; void BeginProfiler() { - spall_init_file("te.spall", 1, &spall_ctx); + Scratch scratch; + String filename = Format(scratch, "te%llu.spall", (unsigned long long)GetTimeNanos()); + spall_init_file(filename.data, 1, &spall_ctx); int buffer_size = 1 * 1024 * 1024; unsigned char *buffer = (unsigned char *)malloc(buffer_size); spall_buffer = {buffer, (size_t)buffer_size}; spall_buffer_init(&spall_ctx, &spall_buffer); + ActivelyProfiling = true; } void EndProfiler() { spall_buffer_quit(&spall_ctx, &spall_buffer); free(spall_buffer.data); spall_quit(&spall_ctx); + ActivelyProfiling = false; } void _BeginProfileScope(const char *name, int len) { + if (!ActivelyProfiling) return; spall_buffer_begin(&spall_ctx, &spall_buffer, name, // name of your name len, // name len minus the null terminator @@ -392,6 +398,7 @@ void _BeginProfileScope(const char *name, int len) { #define BeginProfileScope(name) _BeginProfileScope(#name, sizeof(#name) - 1) void EndProfileScope() { + if (!ActivelyProfiling) return; spall_buffer_end(&spall_ctx, &spall_buffer, GetTimeNanos() // timestamp in microseconds -- end of your timing block ); diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 8031f74..9b3b3d2 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -683,6 +683,7 @@ void Update(Event event) { // We update it here despite the name to make it sure that all the possible changes are // included albeit with delayed response. If we did this at the beginning of the frame // and the DebugWindowUpdated we wouldnt get to know that in the OnCommand. + // @todo: this is slow, we don't want to loop through every buffer on every frame :( For (Buffers) { it->begin_frame_change_id = it->change_id; } @@ -772,6 +773,7 @@ void Update(Event event) { // Reopen modified buffers // @todo: This is O(n) so could see slow downs with number of buffers, likely need OS help to make it more // efficient + // @todo: maybe instead go through windows and only reopen active buffers For(Buffers) { if (it->file_mod_time) { int64_t new_file_mod_time = GetFileModTime(it->name); @@ -868,7 +870,6 @@ int main(int argc, char **argv) char **argv = __argv; AttachConsole(ATTACH_PARENT_PROCESS); #endif - BeginProfiler(); if (1) { RunArenaTest(); @@ -1067,7 +1068,5 @@ int main(int argc, char **argv) SDL_DestroyWindow(SDLWindow); SDL_Quit(); - EndProfiler(); - return 0; }