fix: don't allocate for static strings in ereport!#2282
Open
gruuya wants to merge 1 commit intopgcentralfoundation:developfrom
Open
fix: don't allocate for static strings in ereport!#2282gruuya wants to merge 1 commit intopgcentralfoundation:developfrom
ereport!#2282gruuya wants to merge 1 commit intopgcentralfoundation:developfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a spin-off from #2269; whereas in that PR we resolve the issue of eager allocation in high level logging macros (debug1, log, warning, etc.), there was one omission concerning only
ereport!.Namely,
ereport!now usesIntoMessage::into_messageto get the message from the input arg. When invoked indirectly via high level logging macros this is alwaysstd::fmt::Arguments, which in case of static strings results in astd::borrow::Cow::Borrowedmessage.However, when
ereport!is called directly with a static string this applies insteadpgrx/pgrx-pg-sys/src/submodules/elog.rs
Lines 131 to 136 in b2be3e1
In other words, in this case we always allocate, even for static strings. The fix is simple: replace
impl IntoMessage for &strwithimpl IntoMessage for &'static str(since Rust doesn't allow both to co-exist), which can then returnstd::borrow::Cow::Borrowedforereport!(..., "my string literal").The only "downside" is that stuff like
will error out during compilation. Arguably though this is for the better, since it forces the user to pass the string down, or call
clone()/to_string()/to_owned(), making the allocation explicit.