Apply performance tracing to various functions

This commit is contained in:
Savanni D'Gerinel 2023-06-14 23:47:12 -04:00
parent ef045117be
commit 6456389eef
4 changed files with 117 additions and 101 deletions

View File

@ -19,3 +19,14 @@ impl CoreApi {
});
}
}
pub fn perftrace<F, A>(trace_name: &str, f: F) -> A
where
F: FnOnce() -> A,
{
let start = std::time::Instant::now();
let result = f();
let end = std::time::Instant::now();
println!("[Trace: {}] {:?}", trace_name, end - start);
return result;
}

View File

@ -1,43 +1,35 @@
use gtk::prelude::*;
use kifu_core::{CoreApp, CoreRequest, CoreResponse};
use kifu_gtk::{
perftrace,
ui::{NewGame, PlayingField},
CoreApi,
};
use std::sync::{Arc, RwLock};
fn perftrace<F, A>(trace_name: &str, f: F) -> A
where
F: FnOnce() -> A,
{
let start = std::time::Instant::now();
let result = f();
let end = std::time::Instant::now();
println!("[Trace: {}] {:?}", trace_name, end - start);
return result;
}
fn handle_response(api: CoreApi, window: gtk::ApplicationWindow, message: CoreResponse) {
let playing_field = Arc::new(RwLock::new(None));
match message {
CoreResponse::NewGameView(view) => {
CoreResponse::NewGameView(view) => perftrace("NewGameView", || {
let api = api.clone();
let new_game = NewGame::new(api, view);
window.set_child(Some(&new_game));
}
CoreResponse::PlayingFieldView(view) => {
}),
CoreResponse::PlayingFieldView(view) => perftrace("PlayingFieldView", || {
let api = api.clone();
let mut playing_field = playing_field.write().unwrap();
if playing_field.is_none() {
perftrace("creating a new playing field", || {
let field = PlayingField::new(api, view);
window.set_child(Some(&field));
*playing_field = Some(field);
})
} else {
playing_field.as_ref().map(|field| field.update_view(view));
}
}
}),
}
}

View File

@ -1,4 +1,4 @@
use crate::CoreApi;
use crate::{perftrace, CoreApi};
use gio::resources_lookup_data;
use glib::Object;
use gtk::{
@ -83,6 +83,7 @@ impl ObjectImpl for BoardPrivate {
self.drawing_area
.set_draw_func(move |_, context, width, height| {
perftrace("render drawing area", || {
let render_start = std::time::Instant::now();
let board = board.borrow();
@ -163,6 +164,7 @@ impl ObjectImpl for BoardPrivate {
}
let render_end = std::time::Instant::now();
println!("board rendering time: {:?}", render_end - render_start);
})
});
let motion_controller = gtk::EventControllerMotion::new();

View File

@ -1,8 +1,17 @@
use crate::ui::{Board, Chat, PlayerCard};
use crate::CoreApi;
use crate::{
perftrace,
ui::{Board, Chat, PlayerCard},
CoreApi,
};
use glib::Object;
use gtk::{prelude::*, subclass::prelude::*};
use kifu_core::ui::PlayingFieldView;
use kifu_core::{
ui::{
BoardElement, ChatElement, IntersectionElement, PlayerCardElement, PlayingFieldView,
StoneElement, TextFieldElement,
},
Color, CoreApp, Size,
};
use std::{cell::RefCell, rc::Rc};
pub struct PlayingFieldPrivate {
@ -69,10 +78,12 @@ impl PlayingField {
}
pub fn update_view(&self, view: PlayingFieldView) {
perftrace("update_view", || {
self.imp().board.borrow().as_ref().map(|board| {
board.set_board(view.board);
board.set_current_player(view.current_player);
});
})
}
}
@ -82,8 +93,8 @@ pub fn playing_field_view() -> PlayingFieldView {
width: 19,
height: 19,
});
*board.stone_mut(4, 4) = IntersectionElement::Filled(StoneElement::new(Color::White));
*board.stone_mut(15, 15) = IntersectionElement::Filled(StoneElement::new(Color::Black));
*board.stone_mut(4, 4) = IntersectionElement::Filled(StoneElement::new(Color::White, None));
*board.stone_mut(15, 15) = IntersectionElement::Filled(StoneElement::new(Color::Black, None));
*board.stone_mut(18, 18) = IntersectionElement::Unplayable;
let player_card_black = PlayerCardElement {
color: Color::Black,