Skip to content

Commit

Permalink
CVARS_STATS
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-t committed Jun 2, 2023
1 parent 16ea796 commit da621be
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ The repo is organized as a cargo workspace for the main functionality, with cons

- **Benchmarking:** Run `./bench.sh` in `cvars-bench-compile-time` to benchmark incremental compile time when using the proc macros.

- **Useful commands**: [cargo-llvm-lines](https://github.com/dtolnay/cargo-llvm-lines) and [cargo-bloat](https://github.com/RazrFalcon/cargo-bloat). Use either of them in `cvars-bench-compile-time` (e.g. e.g. `cargo llvm-lines --features fnlike,cvars-1000`) to find out which functions generate a lot of LLVM IR and which compile to a lot of code. This is a good indicator of what is causing long compile times. Lines of LLVM IR is a bit more important because it better indicates how much work the backend has to do even if it compiles down to a small amount of machine code.
- **Useful commands**:
- [cargo-llvm-lines](https://github.com/dtolnay/cargo-llvm-lines) and [cargo-bloat](https://github.com/RazrFalcon/cargo-bloat). Use either of them in `cvars-bench-compile-time` (e.g. e.g. `cargo llvm-lines --features string,typed,fnlike,cvars-1000`) to find out which functions generate a lot of LLVM IR and which compile to a lot of code. This is a good indicator of what is causing long compile times. Lines of LLVM IR is a bit more important because it better indicates how much work the backend has to do even if it compiles down to a small amount of machine code.
- Set the environment variable `CVARS_STATS` to make the macros print how long they took - e.g. `CVARS_STATS= cargo bloat --features string,typed,fnlike,cvars-1000`. If it's small compared to the total compile time, most of the time is spent in codegen, dealing with the large amount of code generated.

## Contributing

Expand Down
29 changes: 21 additions & 8 deletions cvars-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![warn(missing_docs)]
#![allow(clippy::let_and_return)]

use std::collections::HashSet;
use std::{collections::HashSet, env};

use proc_macro::TokenStream;
use proc_macro2::{Span, TokenTree};
Expand Down Expand Up @@ -96,7 +96,7 @@ impl Parse for CvarDef {
/// ```
#[proc_macro]
pub fn cvars(input: TokenStream) -> TokenStream {
// let begin = std::time::Instant::now();
let begin = std::time::Instant::now();

let parser = Punctuated::<CvarDef, Token![,]>::parse_terminated;
let punctuated = parser.parse(input).expect("failed to parse");
Expand Down Expand Up @@ -142,8 +142,10 @@ pub fn cvars(input: TokenStream) -> TokenStream {
};
let expanded = expanded.into();

// let end = std::time::Instant::now();
// eprintln!("cvars! took {:?}", end - begin);
let end = std::time::Instant::now();
if env::var("CVARS_STATS").is_ok() {
eprintln!("cvars! took {:?}", end - begin);
}

expanded
}
Expand Down Expand Up @@ -182,7 +184,7 @@ pub fn cvars(input: TokenStream) -> TokenStream {
/// ```
#[proc_macro_derive(SetGet, attributes(cvars))]
pub fn derive(input: TokenStream) -> TokenStream {
// let begin = std::time::Instant::now();
let begin = std::time::Instant::now();

let input: DeriveInput = parse_macro_input!(input);
let struct_name = input.ident;
Expand Down Expand Up @@ -210,8 +212,10 @@ pub fn derive(input: TokenStream) -> TokenStream {
let expanded = generate(struct_name, &skips, &names, &tys);
let expanded = expanded.into();

// let end = std::time::Instant::now();
// eprintln!("derive(SetGet) took {:?}", end - begin);
let end = std::time::Instant::now();
if env::var("CVARS_STATS").is_ok() {
eprintln!("derive(SetGet) took {:?}", end - begin);
}

expanded
}
Expand Down Expand Up @@ -435,6 +439,8 @@ fn is_skip(attr: &Attribute) -> bool {
#[doc(hidden)]
#[proc_macro_derive(SetGetDummy, attributes(cvars))]
pub fn derive_dummy(input: TokenStream) -> TokenStream {
let begin = std::time::Instant::now();

let input: DeriveInput = parse_macro_input!(input);
let struct_name = input.ident;
let set_get_impl = impl_set_get(&struct_name);
Expand All @@ -458,7 +464,14 @@ pub fn derive_dummy(input: TokenStream) -> TokenStream {

#set_get_impl
};
TokenStream::from(expanded)
let expanded = TokenStream::from(expanded);

let end = std::time::Instant::now();
if env::var("CVARS_STATS").is_ok() {
eprintln!("derive(SetGetDummy) took {:?}", end - begin);
}

expanded
}

fn impl_set_get(struct_name: &Ident) -> proc_macro2::TokenStream {
Expand Down

0 comments on commit da621be

Please sign in to comment.