Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make puffin_egui use puffin provided timer function #81

Open
therocode opened this issue May 31, 2022 · 0 comments
Open

Make puffin_egui use puffin provided timer function #81

therocode opened this issue May 31, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@therocode
Copy link

Is your feature request related to a problem? Please describe.
Puffin works in the browser, if you provide it with a custom now() function using puffin::ThreadProfiler::initialize(...). However, puffin_egui does not, since it internally uses std::time for timing, and this function gives 'error time is not implemented on this platform' when running it in the browser environment. This means that currently, puffin_egui is unusable in the browser, due to this small internal catch.

Describe the solution you'd like
puffin_egui could use the puffin::ThreadProfiler's timing function since that one can be configured to something that works and would make puffin_egui just work from a user's perspective.

Describe alternatives you've considered
I guess another option is that puffin_egui also provides a way to use a custom timing function that could be different to the one given to puffin.

Additional context
I hacked the proposed solution into puffin_egui to see if it would work and it does. It's a very rough solution and changes some types, so it's not necessarily PR-worthy.

fn run_pack_pass_if_needed(&mut self, frame_view: &FrameView) {
        // NOTE: This function contains changes compared to the original repository.
        // last_pack_pass is changed from Option<std::time::Instant> to Option<i64>.
        // 'now' is using the ThreadProfiler now function instead of the system time one. Note that I added a public `now_ns_fn()`
        // to the ThreadProfiler too to be able to access the timing function.
        let now = || ThreadProfiler::call(|tp| tp.now_ns_fn());

        let last_pack_pass = *self.last_pack_pass.get_or_insert_with(now);
        let time_in_ns_since_last_pack = (now() - last_pack_pass) as u128;
        if time_in_ns_since_last_pack > std::time::Duration::from_secs(1).as_nanos() {
            puffin::profile_scope!("pack_pass");
            for frame in self.all_known_frames(frame_view) {
                if !self.is_selected(frame_view, frame.frame_index()) {
                    frame.pack();
                }
            }
            self.last_pack_pass = Some(now());
        }
    }
@therocode therocode added the enhancement New feature or request label May 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant