Skip to main content

psync_graph/
profile.rs

1//! `#[psync::graph(profile)]`: compile the function as written, timing every statement that
2//! awaits, for `psync-predict` (see `psync::profile`).
3//!
4//! A statement is timed when it awaits outside any nested block; statements inside its blocks are
5//! timed on their own, so no cost is counted twice. Async blocks and closures are separate
6//! futures and are left alone. Each statement reports `line!()` at its own span, the line its
7//! first token is on, which is the line psync's report gives its node.
8use proc_macro2::TokenStream;
9use quote::quote_spanned;
10use syn::{spanned::Spanned, visit::Visit, visit_mut::VisitMut};
11
12pub(crate) fn instrument(block: &mut syn::Block, krate: &TokenStream) {
13    Profile { krate }.visit_block_mut(block);
14}
15
16struct Profile<'a> {
17    krate: &'a TokenStream,
18}
19
20/// Whether `stmt` awaits outside any nested block, closure or async block.
21fn awaits_directly(stmt: &syn::Stmt) -> bool {
22    struct A(bool);
23    impl<'ast> Visit<'ast> for A {
24        fn visit_expr_await(&mut self, _: &'ast syn::ExprAwait) {
25            self.0 = true;
26        }
27        fn visit_block(&mut self, _: &'ast syn::Block) {}
28        fn visit_expr_closure(&mut self, _: &'ast syn::ExprClosure) {}
29        fn visit_expr_async(&mut self, _: &'ast syn::ExprAsync) {}
30        fn visit_item(&mut self, _: &'ast syn::Item) {}
31    }
32    let mut a = A(false);
33    a.visit_stmt(stmt);
34    a.0
35}
36
37impl VisitMut for Profile<'_> {
38    fn visit_expr_closure_mut(&mut self, _: &mut syn::ExprClosure) {}
39    fn visit_expr_async_mut(&mut self, _: &mut syn::ExprAsync) {}
40    fn visit_item_mut(&mut self, _: &mut syn::Item) {}
41
42    fn visit_block_mut(&mut self, block: &mut syn::Block) {
43        let k = self.krate;
44        let mut out = Vec::with_capacity(block.stmts.len());
45        for mut stmt in std::mem::take(&mut block.stmts) {
46            syn::visit_mut::visit_stmt_mut(self, &mut stmt);
47            if !awaits_directly(&stmt) {
48                out.push(stmt);
49                continue;
50            }
51            let line = quote_spanned!(stmt.span()=> ::core::line!());
52            let start: syn::Stmt = syn::parse_quote! {
53                let __psync_profile = #k::__private::profile_start();
54            };
55            let end: syn::Stmt = syn::parse_quote! {
56                #k::__private::profile_end(__psync_profile, #line);
57            };
58            match stmt {
59                // The block's value: time it, then hand it on.
60                syn::Stmt::Expr(e, None) => {
61                    let wrapped: syn::Expr = syn::parse_quote! {{
62                        let __psync_profile = #k::__private::profile_start();
63                        let __psync_value = #e;
64                        #k::__private::profile_end(__psync_profile, #line);
65                        __psync_value
66                    }};
67                    out.push(syn::Stmt::Expr(wrapped, None));
68                }
69                stmt => {
70                    out.push(start);
71                    out.push(stmt);
72                    out.push(end);
73                }
74            }
75        }
76        block.stmts = out;
77    }
78}