Skip to content

[Base] some cleaning in timers#1811

Open
tdavidcl wants to merge 7 commits into
Shamrock-code:mainfrom
tdavidcl:improve_timer
Open

[Base] some cleaning in timers#1811
tdavidcl wants to merge 7 commits into
Shamrock-code:mainfrom
tdavidcl:improve_timer

Conversation

@tdavidcl
Copy link
Copy Markdown
Member

No description provided.

@github-actions
Copy link
Copy Markdown
Contributor

Thanks @tdavidcl for opening this PR!

You can do multiple things directly here:
1 - Comment pre-commit.ci run to run pre-commit checks.
2 - Comment pre-commit.ci autofix to apply fixes.
3 - Add label autofix.ci to fix authorship & pre-commit for every commit made.
4 - Add label light-ci to only trigger a reduced & faster version of the CI (need the full one before merge).
5 - Add label trigger-ci to create an empty commit to trigger the CI.

Once the workflow completes a message will appear displaying informations related to the run.

Also the PR gets automatically reviewed by gemini, you can:
1 - Comment /gemini review to trigger a review
2 - Comment /gemini summary for a summary
3 - Tag it using @gemini-code-assist either in the PR or in review comments on files

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the timing utilities by extracting the Timer class and the nanosec_to_time_str function into dedicated header files, Timer.hpp and format_time.hpp, and introduces a new suite of unit tests. The code review identifies opportunities to improve encapsulation by making internal state members private, ensuring the std::chrono backend is properly initialized in the constructor to avoid incorrect duration calculations, and simplifying string formatting in the time conversion utility.

Comment on lines +37 to +43
public:
#if defined(USE_PLF_TIMER)
plf::nanotimer timer; ///< Internal timer
#else
std::chrono::steady_clock::time_point t_start; ///< Internal timer
#endif
f64 nanosec; ///< Time in nanoseconds
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The internal state of the Timer class (the backend timer/time_point and the nanosec accumulator) is currently exposed as public members. To improve encapsulation and prevent accidental modification of the timer's state from outside the class, these should be made private.

Suggested change
public:
#if defined(USE_PLF_TIMER)
plf::nanotimer timer; ///< Internal timer
#else
std::chrono::steady_clock::time_point t_start; ///< Internal timer
#endif
f64 nanosec; ///< Time in nanoseconds
private:
#if defined(USE_PLF_TIMER)
plf::nanotimer timer; ///< Internal timer
#else
std::chrono::steady_clock::time_point t_start; ///< Internal timer
#endif
f64 nanosec; ///< Time in nanoseconds
public:

f64 nanosec; ///< Time in nanoseconds

/// Constructor, init nanosec to 0
Timer() : nanosec(0.0) {}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When using the std::chrono backend, t_start is not initialized in the constructor. If stop() is called before start(), the calculated duration will be relative to the clock's epoch, resulting in a very large and incorrect value. It is safer to initialize t_start to the current time or ensure nanosec remains 0 if the timer hasn't been started.

Suggested change
Timer() : nanosec(0.0) {}
Timer() : nanosec(0.0) {
#if !defined(USE_PLF_TIMER)
t_start = std::chrono::steady_clock::now();
#endif
}

Comment thread src/shambase/include/shambase/format_time.hpp Outdated
tdavidcl and others added 3 commits May 15, 2026 17:47
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown
Contributor

Workflow report

workflow report corresponding to commit 4e252c0
Commiter email is timothee.davidcleris@proton.me
GitHub page artifact URL GitHub page artifact link (can expire)

Pre-commit check report

Pre-commit check: ✅

trim trailing whitespace.................................................Passed
fix end of files.........................................................Passed
check for merge conflicts................................................Passed
check that executables have shebangs.....................................Passed
check that scripts with shebangs are executable..........................Passed
check for added large files..............................................Passed
check for case conflicts.................................................Passed
check for broken symlinks................................................Passed
check yaml...............................................................Passed
detect private key.......................................................Passed
No-tabs checker..........................................................Passed
Tabs remover.............................................................Passed
cmake-format.............................................................Passed
Validate GitHub Workflows................................................Passed
clang-format.............................................................Passed
ruff check...............................................................Passed
ruff format..............................................................Passed
Check doxygen headers....................................................Passed
Check license headers....................................................Passed
Check #pragma once.......................................................Passed
Check SYCL #include......................................................Passed
No ssh in git submodules remote..........................................Passed
No UTF-8 in files (except for authors)...................................Passed

Test pipeline can run.

Clang-tidy diff report

No relevant changes found.
Well done!

You should now go back to your normal life and enjoy a hopefully sunny day while waiting for the review.

Doxygen diff with main

Removed warnings : 0
New warnings : 0
Warnings count : 8706 → 8706 (0.0%)

Detailed changes :

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant