Last active
October 6, 2026 00:28
-
-
Save Techcable/1d1a7403893df9f8efbcb9218eaa1ee1 to your computer and use it in GitHub Desktop.
Tests the parsing speed of syn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env cargo | |
| ---cargo | |
| [dependencies] | |
| syn = { version = "3", features = ["full"] } | |
| ignore = "0.4" | |
| anyhow = "1" | |
| --- | |
| use anyhow::Context; | |
| use ignore::Walk; | |
| use std::collections::HashMap; | |
| use std::path::{Path, PathBuf}; | |
| use std::time::{Duration, Instant}; | |
| pub fn main() -> anyhow::Result<()> { | |
| println!("dbg: {}", cfg!(debug_assertions)); | |
| let path = PathBuf::from( | |
| std::env::args_os() | |
| .skip(1) | |
| .next() | |
| .expect("Missing directory argument"), | |
| ); | |
| let start = Instant::now(); | |
| let mut all_parsed = HashMap::new(); | |
| let mut parse_duration = Duration::ZERO; | |
| let mut do_parse = |path: &Path| { | |
| let text = | |
| std::fs::read_to_string(&path).with_context(|| format!("Failed to read {path:?}"))?; | |
| let parse_start = Instant::now(); | |
| let parsed = syn::parse_str::<syn::File>(&text) | |
| .with_context(|| format!("Failed to parse {path:?}"))?; | |
| parse_duration += Instant::now() - parse_start; | |
| std::assert!( | |
| all_parsed.insert(path.to_owned(), parsed).is_none(), | |
| "conflicts for {path:?}" | |
| ); | |
| anyhow::Ok(()) | |
| }; | |
| if path.is_file() { | |
| do_parse(&path)?; | |
| } else { | |
| for entry in Walk::new(path) { | |
| let entry = entry?; | |
| if !entry.file_name().as_encoded_bytes().ends_with(b".rs") { | |
| continue; | |
| } | |
| let path = entry.path(); | |
| assert!(entry.file_type().unwrap().is_file(), "{entry:?}"); | |
| do_parse(path)?; | |
| } | |
| } | |
| let io_duration = Instant::now() - start; | |
| println!( | |
| "Parsed {} files, took {}ms excluding IO ({} ms including it)", | |
| all_parsed.len(), | |
| parse_duration.as_millis(), | |
| io_duration.as_millis(), | |
| ); | |
| Ok(()) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment