Skip to content

Commit d29d5c5

Browse files
committed
bench: add tailwind_parse benchmark to isolate parser from merge pipeline
Exposes parse_tailwind, AstStyle, and AstParseOptions via a #[doc(hidden)] __bench module so the nom parser can be benchmarked independently of conflict detection and class merging.
1 parent 6d42c54 commit d29d5c5

4 files changed

Lines changed: 31 additions & 3 deletions

File tree

crates/tw_merge/tw_merge/benches/merge.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use divan::Bencher;
2+
use tw_merge::__bench::{AstParseOptions, parse_tailwind};
23
use tw_merge::merge::{merge_classes, tw_merge_slice};
34

45
fn main() {
@@ -27,6 +28,15 @@ fn tailwind_merge_slice(bencher: Bencher, len: usize) {
2728
bencher.with_inputs(|| generate_random_classes(len)).bench_values(|class| tw_merge_slice(&class));
2829
}
2930

31+
#[divan::bench(
32+
args = LENS,
33+
sample_count = SAMPLE_COUNT,
34+
sample_size = SAMPLE_SIZE
35+
)]
36+
fn tailwind_parse(bencher: Bencher, len: usize) {
37+
bencher.with_inputs(|| generate_random_classes(len)).bench_values(|classes| parse_tailwind(&classes, AstParseOptions::default()));
38+
}
39+
3040
// create a vec with the a length of len and fill it with random data
3141
fn generate_random_classes(n: usize) -> Vec<&'static str> {
3242
let mut classes_vec = Vec::with_capacity(n);

crates/tw_merge/tw_merge/src/ast/mod.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ mod parser;
22
#[cfg(test)]
33
mod parser_test;
44

5-
pub(crate) use parser::{parse_tailwind, take_until_unbalanced};
5+
pub(crate) use parser::take_until_unbalanced;
6+
#[doc(hidden)]
7+
pub use parser::parse_tailwind;
68

9+
#[doc(hidden)]
710
#[derive(Clone, Debug, PartialEq, Default)]
8-
pub(crate) struct AstStyle<'a> {
11+
pub struct AstStyle<'a> {
912
pub source: &'a str,
1013
/// Is a `!important` style
1114
pub important: bool,
@@ -36,8 +39,9 @@ enum ASTVariant<'a> {
3639
ArbitraryAttribute(&'a str),
3740
}
3841

42+
#[doc(hidden)]
3943
#[derive(Clone, Debug, PartialEq)]
40-
pub(crate) struct AstParseOptions<'a> {
44+
pub struct AstParseOptions<'a> {
4145
/// Custom prefix for modifiers in Tailwind classes
4246
/// <https://tailwindcss.com/docs/configuration#prefix>
4347
pub prefix: &'a str,
@@ -46,6 +50,7 @@ pub(crate) struct AstParseOptions<'a> {
4650
pub separator: &'a str,
4751
}
4852

53+
#[doc(hidden)]
4954
impl Default for AstParseOptions<'static> {
5055
fn default() -> Self {
5156
Self { prefix: "", separator: ":" }

crates/tw_merge/tw_merge/src/ast/parser.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use nom::sequence::{delimited, tuple};
88

99
use super::{ASTVariant, AstElements, AstParseOptions, AstStyle};
1010

11+
/// Parses a slice of Tailwind class strings into structured AST nodes.
12+
#[doc(hidden)]
1113
pub fn parse_tailwind<'a>(class: &[&'a str], options: AstParseOptions<'a>) -> Vec<Result<AstStyle<'a>, &'a str>> {
1214
class
1315
.iter()

crates/tw_merge/tw_merge/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,17 @@ pub use crate::core::{merge, *};
262262
mod ast;
263263
mod core;
264264

265+
/// Internal utilities exposed for benchmarking only. Not part of the public API.
266+
#[doc(hidden)]
267+
pub mod __bench {
268+
#[doc(hidden)]
269+
pub use crate::ast::AstParseOptions;
270+
#[doc(hidden)]
271+
pub use crate::ast::AstStyle;
272+
#[doc(hidden)]
273+
pub use crate::ast::parse_tailwind;
274+
}
275+
265276
#[cfg(feature = "variant")]
266277
pub use variant::*;
267278

0 commit comments

Comments
 (0)