Allow (static) custom source locations for spans#151
Allow (static) custom source locations for spans#151moulins wants to merge 2 commits intonagisa:mainfrom
Conversation
Tracy requires that `SpanLocation`s live effectively forever, so `make_span_location` can just leak the string buffer here.
This crate predates it, but rust now has C-string literals which can be used for this. |
Oh, duh! Still, unlike a bare |
| $([name $name: expr])? | ||
| [function $function: expr] | ||
| $([file $file: expr])? | ||
| $([line $line: expr])? | ||
| $([color $color: expr])? |
There was a problem hiding this comment.
This approach forces the order of fields, which is sorta unfortunate. I feel like this limitation could be lifted if we went through a struct to construct the span location. That is have the span location look something like this:
macro_rules! span_location {
...
($($field: ident: $value: expr),* $(,)?) => {
$crate::internal::SpanLocationArgs {
$($field: $value),*
..SpanLocationArgs::const_default(),
}
}
There was a problem hiding this comment.
Interesting, I'll have to think on how to do it while preserving the special-casing when defaulting the function field.
There was a problem hiding this comment.
Gave it a try, and unfortunately I couldn't make it to work without using Lazy even when function is specified:
- the
const_default()approach needs to use#[track_caller]andLocation::caller(), which isconstonly from Rust 1.79.0 onwards (forcing a MSRV bump); - Even then, I couldn't find a nice way of detecting the absence of
function: "..."in the macro input (the special-case is still needed asLocation::caller()doesn't give use the caller's function name); it's probably possible with tt-munching tricks but that complexifies things massively.
It's true that |
I don't think this is a meaningful advantage. This isn't a significant amount of space use, and the linker should do just fine at dropping unreferenced symbols anyway. |
ca9bec9 to
0d90d91
Compare
|
Just a heads up that I won't be able to look at this for at least a few weeks. I'll keep a notification in my mailbox for this though, so this should be roughly the first thing I return to when I'm less occupied. |
nagisa
left a comment
There was a problem hiding this comment.
Having thought about it, I feel like trying to avoid the Lazy right now is making things more difficult than strictly necessary. If there is proof that it is causing a significant overhead, we can think about alternative approaches other than span_location potentially, but in absence of such proof (or in absence of significant impact) I'd rather wait until we get const-stable ways to obtain the function name instead.
Until then I think keeping Lazy will make it much more straightforward to provide a neat API. I reworked some code in #155 to that effect. Please take a look...
| line, | ||
| color, | ||
| }, | ||
| _function_name: function_name, |
There was a problem hiding this comment.
Although you're right that tracy requires function names to be effectively 'static, I think this change makes things strictly worse from the resilience and maintainability perspective.
While there is no practical use for having SpanLocation be dropped at all, I can imagine that in the future there may be use-cases that would drop SpanLocation (e.g. because it never got used actually, or maybe in some on-demand tracing scenarios...)
I think the first commit should be removed or adjusted to make the CString field an Option.
| ) => {{ | ||
| static LOC: $crate::internal::SpanLocation = | ||
| $crate::internal::static_span_location( | ||
| concat!($function, "\0").as_ptr(), |
There was a problem hiding this comment.
Lets have users supply &'static CStr (via c"literals".) You can force this by doing a
CStr::as_ptr($argument)SAme for when user provides a custom name, filename.
There was a problem hiding this comment.
Requiring C-string literals would make the macro different from all other literal-taking macros in the crate (span!, plot_name!, etc) so I'm not sure this is a good idea?
There was a problem hiding this comment.
+1, this seems like an unnecessary step backwards in ergonomics. The only upside I can see is if someone might need to pass a non-literal &'static CStr, but it still has to be a constant, so I'm not sure how that could arise.
| $([file $file: expr])? | ||
| $([line $line: expr])? |
There was a problem hiding this comment.
I feel like it makes little sense to allow overriding just the file, or just the line individually, but at the same time I have a hard time justifying why the library should prevent users from doing so 🤷
Closes #108 by expanding
span_location!to allow overriding source locations:As an optimization, when the
functionname is provided statically, theLazywrapper is skipped.EDIT: Also closes #106 by adding color support to static spans
I considered adding a
const SpanLocation::newconstructor, but this would mean adding a way of making static nul-terminated strings outside ofFrameName/PlotName::new_leak, so I skipped this for now. What do you think about adding an unifiedStaticName/static_name!type/macro that could be used everywhere Tracy expects such strings?