Properly cache CStrings for plot names#36
Properly cache CStrings for plot names#36clibequilibrium merged 1 commit intoclibequilibrium:mainfrom
Conversation
|
@Mervill thank you for the PR, would it make sense in this case to make public static void Dispose()
{
// Free CStrings
foreach (var kvp in tracyAllocationsDictionary)
{
kvp.Value.Item1.Dispose();
kvp.Value.Item2.Dispose();
}
} |
|
For the reasons I outlined in the OP, I don't think allowing external code to dispose the string cache is appropriate. If you really want to implement it, I suggest you use the In the |
how about making Dispose method private and Profile class not static. The proper usage of Profiler then will be via using statement. Otherwise right now the Profiler leaks memory after it's no longer used? |
|
The The Tracy docs are saying that these stings (the plot names) need to be kept around for the entire program lifetime. Even if you use The only appropriate time to free these strings is at program shutdown, in which case we can simply let the memory be invalidated by the C# runtime as it's shutting down. |
Thanks a lot. Sorry for missing this part |
From the Tracy docs:
I had forgotten when I added the
Plotwrapper that those functions fall into category 1 above. We need to cache the Plot name CStrings to satisfy Tracy's requirements. That's what this PR does.Currently the strings are cached and kept privately by the Profiler for the duration of the program. I'm not yet convinced allowing external code to clear the cache is what we want. Let me explain.
The cache clear function would look like this:
It was brought up in the other PR that one reason we might want to have support for clearing the cache is because of
TRACY_ON_DEMANDwhich causes Tracy to only store data when the profiler is actually connected. When the profiler is disconnected we might conceivably want to free the cache.But remember that Tracy wants these strings to be unique for the application lifetime. If you:
Then I think Tracy will eat bad data because the pointers are not the same between the two times you've connected with the profiler.
With that in mind, the only time it would make sense to let external code clear the cache when
TRACY_ON_DEMANDis active is when we can guarantee that the profiler won't reconnect. Which seems like an extremely rare situation? Other then at program shutdown?On a similar line of thinking;
TRACY_ON_DEMANDprevents sending data inside the native code but you could clear the CString cache on the C# side, callPlot("Foo", 123)again after the profiler is disconnected, and have the Profiler class happily recache the string. Even though it's likely invalid (as discussed above) and even though it may not actually be sent anywhere because the profiler is disconnected. I feel like we would need to add guard statements to the functions that use cached CStrings to prevent this behavior. I could be overthinking it though.I use this library though the NuGet package so I don't have access to the version of the lib that has
TRACY_ON_DEMAND. If you wanted to test that version with the cache clearing code I posted above and test reconnecting, that would be helpful.Minor changes:
bool IsConnected().gitignoreentries for C# artifacts and .vs files.Fixes #34