puffin icon indicating copy to clipboard operation
puffin copied to clipboard

Make puffin_egui use puffin provided timer function

Open therocode opened this issue 3 years ago • 0 comments

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 avatar May 31 '22 07:05 therocode