Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small message buffer optimization for SDL_log and SDL_assert #5584

Merged
merged 5 commits into from
Apr 29, 2022

Conversation

eloj
Copy link
Contributor

@eloj eloj commented Apr 28, 2022

I saw the discussion on 2a42952 and subsequent commits, and thought this sort of solution could be another approach worth trying.

After forgetting I had to copy the va_list I spent way too much time on this... but that's on me, if you don't like the approach it goes in the bin.

Pros:

  • Stack size worry goes away.
  • No malloc for small messages.
  • (Could in theory support any-size message)
  • No mutex required (unless I'm missing something?)

Cons:

  • More complicated code.
  • malloc + extra vsnprintf for large messages.
  • ... but in a pinch you could set SDL_MAX_LOG_MESSAGE_STACK to SDL_MAX_LOG_MESSAGE and avoid the extra allocation.

@eloj eloj force-pushed the small-message-buffer-opt branch 2 times, most recently from d6cc791 to 99f5f1d Compare April 28, 2022 23:07
@slouken slouken requested a review from icculus April 29, 2022 00:00
@slouken slouken added this to the 2.0.24 milestone Apr 29, 2022
src/SDL_assert.c Outdated Show resolved Hide resolved
Copy link
Collaborator

@icculus icculus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good idea, with some tweaks, but the mutex needs to stay regardless.

src/SDL_log.c Outdated Show resolved Hide resolved
include/SDL_log.h Outdated Show resolved Hide resolved
src/SDL_log.c Outdated Show resolved Hide resolved
@slouken
Copy link
Collaborator

slouken commented Apr 29, 2022

I like the idea that we're no longer limited on log message size. Let's do it! :)

@eloj
Copy link
Contributor Author

eloj commented Apr 29, 2022

I appreciate the review, and I have pushed changes addressing Ryan's comments.

As for being limited on size, do you want me to actually remove SDL_MAX_LOG_MESSAGE and the truncation of messages, truly allowing 'unlimited' length log lines? It'd probably simplify the code to just malloc exactly what we need, but I sort of feel like the truncation is justified.

Starting to second-guess myself, maybe this isn't worth it and using a static buffer with locks around it is better overall.

Please be very specific in what you want, and I'll get on it.

For short messages, use a stack buffer that is
significantly smaller than SDL_MAX_LOG_MESSAGE.

For larger messages, fall back to allocation.
For short messages, use a stack buffer that is
significantly smaller than SDL_MAX_LOG_MESSAGE.

The rationale for this is that we don't want to risk
blowing the stack, while at the same time we would
like to not put pressure on the memory allocator unless
absolutely necessary.
@slouken
Copy link
Collaborator

slouken commented Apr 29, 2022

Let's use a static buffer for messages <= 256 bytes, and malloc for longer lines. I'm not too worried about the unbounded pointer case, the likelihood of not getting a zero for a long time is pretty low, and malloc() will fail for really really long lines. We free the pointer almost immediately, so I'm not worried about introducing any issues with consuming too much memory here.

@eloj
Copy link
Contributor Author

eloj commented Apr 29, 2022

Okay, I'll rework the code to no longer truncate log messages. Same for the assert code?

Log messages are no longer truncated to SDL_MAX_LOG_MESSAGE.
Messages are no longer truncated to SDL_MAX_LOG_MESSAGE.
@eloj
Copy link
Contributor Author

eloj commented Apr 29, 2022

I have pushed the changes to allow for 'unbounded' log and assert messages.

I have not touched this Apple code in SDL_LogOutput. I guess I'll do the same thing there, unless someone objects. I'll push this change later today.

       char *text;
        /* !!! FIXME: why not just "char text[SDL_MAX_LOG_MESSAGE];" ? */
        text = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE);
        if (text) {
            SDL_snprintf(text, SDL_MAX_LOG_MESSAGE, "%s: %s", SDL_priority_prefixes[priority], message);
            SDL_NSLog(text);
            SDL_stack_free(text);
            return;
        }

@slouken
Copy link
Collaborator

slouken commented Apr 29, 2022

I have not touched this Apple code in SDL_LogOutput. I guess I'll do the same thing there, unless someone objects. I'll push this change later today.

       char *text;
        /* !!! FIXME: why not just "char text[SDL_MAX_LOG_MESSAGE];" ? */
        text = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE);
        if (text) {
            SDL_snprintf(text, SDL_MAX_LOG_MESSAGE, "%s: %s", SDL_priority_prefixes[priority], message);
            SDL_NSLog(text);
            SDL_stack_free(text);
            return;
        }

I'll go ahead and fix this.

slouken added a commit that referenced this pull request Apr 29, 2022
@slouken
Copy link
Collaborator

slouken commented Apr 29, 2022

@icculus, do you have a preference whether the 256 byte buffer is static or on the stack?
@eloj, if the buffer becomes static, you'll need to move the lock around the access to it.

@icculus
Copy link
Collaborator

icculus commented Apr 29, 2022

Eloj convinced me; 256 bytes is good, and let's stack allocate it and see what happens.

@slouken
Copy link
Collaborator

slouken commented Apr 29, 2022

@eloj, I think this PR is good to go.

@eloj eloj marked this pull request as ready for review April 29, 2022 17:38
@eloj eloj changed the title DRAFT: Small message buffer optimization for SDL_log and SDL_assert Small message buffer optimization for SDL_log and SDL_assert Apr 29, 2022
@slouken slouken merged commit ca26df3 into libsdl-org:main Apr 29, 2022
@eloj eloj deleted the small-message-buffer-opt branch April 29, 2022 17:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants