[CDRIVER-4697] More efficient trace toggle Created: 22/Jul/23  Updated: 05/Feb/24

Status: Backlog
Project: C Driver
Component/s: libmongoc, Logging
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: Christian Schmitz Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: feature-request, neweng
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to CDRIVER-4800 Return early in mongoc_log_trace func... Backlog
Epic Link: Easier debugging with standardized logging
Quarter: FY25Q1

 Description   

I enjoy using trace option in libmongoc.

But I see that trace is toggled via compiler directive. Since we plan to build libmongoc with trace enabled, the trace data is always prepared, but eventually later not output if trace is disabled.

This could be done more optimized by checking a global variable instead of compiler switch.


 

Expected:

Optimize trace toggle in mongoc-trace-private.h by updating

if (MONGOC_TRACE_ENABLED)

to

if (MONGOC_TRACE_ENABLED && gLogTrace)

  • MONGOC_TRACE_ENABLED is a compile time flag.
  • If it is MONGOC_TRACE_ENABLED to false, the compiler can optimize away the whole code black inside if statement.
  • Note: We also have a similar function -  _mongoc_log_trace_is_enabled 


 Comments   
Comment by Rishabh Bisht [ 01/Aug/23 ]

Understood, thank you for the clarification! 

Comment by Christian Schmitz [ 27/Jul/23 ]

Well, technically you could call  _mongoc_log_trace_is_enabled() function.

But I still prefer to use

 

 if (MONGOC_TRACE_ENABLED && gLogTrace)

 

since the compiler can optimize the whole if away, if MONGOC_TRACE_ENABLED is not 1.

If MONGOC_TRACE_ENABLED is 1 (as for our builds), reading the boolean variable should be quick and have nearly no performance impact. The whole thing avoids building the debug message when trace is not enabled.

Comment by Rishabh Bisht [ 27/Jul/23 ]

Hi support@monkeybreadsoftware.de, thanks for your suggestion.
If I understand correctly, you're suggesting to add  _mongoc_log_trace_is_enabled check in mongoc-trace-private.h, additional to mongoc_log. In other words, making the check early whether tracing is enabled and trace messages are toggled on. Is this correct understanding?

Comment by Christian Schmitz [ 22/Jul/23 ]

In mongoc-trace-private.h, you see

 

if (MONGOC_TRACE_ENABLED)

 

so if the library is compiled with tracing, the block below will run.

But if tracing is disabled, we do all the log calls and potential also convert bson to text to include it.

 

My suggestion is to change this to:

 

 if (MONGOC_TRACE_ENABLED && gLogTrace)

 

So the trace code only runs if enabled. We keep MONGOC_TRACE_ENABLED, so the compiler can optimize away the whole block if MONGOC_TRACE_ENABLED is false. But if it is true, it check the gLogTrace variable.

 

This variable must be declared of course:

 

extern bool gLogTrace;

 

and Made non-static in mongoc-log.c. Maybe also rename it as it is now in the global namespace.

 

We have built library with this changes. Performance with trace enabled for building the library and then having trace disabled should be about the same as if you would build library with log disabled.

Comment by PM Bot [ 22/Jul/23 ]

Hi support@monkeybreadsoftware.de, thank you for reporting this issue! The team will look into it and get back to you soon.

Generated at Wed Feb 07 21:21:40 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.