I noticed that a small optimization is possible in the flow of wait stat reporting for the LWLocks, when the pgstat_track_activities is disabled.
If the check for pgstat_track_activities is done before invoking LWLockReportWaitStart() instead of inside the pgstat_report_wait_start(), it can save some of the statements execution where the end result of LWLockReportWaitStart() is a NO-OP because pgstat_track_activities = false.
This is only called in slow path which means when we have to wait or sleep, so saving few instructions will not make much difference. Note that both the functions you have mentioned are inline functions. However, If you want, you can try that way and see if this leads to any gain.
I also see, that there are other callers of pgstat_report_wait_start() as well, which would also have to change to add a check for the pg_stat_activities, if the check is removed from the pgstat_report_wait_start(). Is the pg_stat_activities check inside pgstat_report_wait_start() because of some protocol being followed?
Not as such, but that variable is mainly used in pgstat.c/.h only.