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

Move away from printf-style formatting #203

Open
davidjoffe opened this issue Dec 24, 2023 · 0 comments
Open

Move away from printf-style formatting #203

davidjoffe opened this issue Dec 24, 2023 · 0 comments

Comments

@davidjoffe
Copy link
Owner

davidjoffe commented Dec 24, 2023

I know some may disagree, but after much consideration I feel it's better to completely move away from these types of functions (so-called printf-style in C/C++, famous, perhaps infamous, i.e. functions using "%d" "%s" etc. to format strings), as even if it's done 'perfectly correctly' and even if it's done 'safely' with dynamically resizable buffers and thread_local variables, and even if nobody ever makes mistakes in the formatting arguments (which can sometimes be subtle and platform dependent e.g. when moving from 32-bit to 64-bit say), it STILL in some cases requires locks even with thread_local buffers. Also even though I love thread_local, we want to avoid UNNECESSARY OVER-use of thread_local because it incurs a tiny extra performance penalty every time a thread is created (and I'd like an architecture that allows scaling). Also for most simple string logs etc. there isn't even formatting and then it's unnecessary performance overhead to do "printf("foo") - better to use functions where (1) it does not need to parse for formatting in the first place (thereby reducing risk of accidental mistakes too) and (2) such functions can potentially gain slightly further from constexpr compile-time strlen calculation of string literal to avoid in some cases an extra unnecessary strlen call at runtime (e.g. for something like "LOG("foo") if the compiler can calculate strlen for us - as it can - likewise with std::string::append(char*,len)) we get tiny extra performance.

This may be negligible for most stuff, but potentially in performance-sensitive parts of code it may help - especially for gamedev.

So these alternatives can be both faster, and safer from a bug and security perspective. And functions like std::to_string(n) instead of "printf("%d",n) can be automatically thread-safe without need for locking or buffers (thread_local or not) - desirable if one generally wants to start doing threading stuff - even though we don't currently have threads.

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

No branches or pull requests

1 participant