Skip to content

Commit b08bfe3

Browse files
committed
Recreate py util for generating random markdown file.
*It's more than 10x faster*
1 parent 6d2805a commit b08bfe3

File tree

3 files changed

+308
-0
lines changed

3 files changed

+308
-0
lines changed

utils/generate_markdown/Cargo.lock

Lines changed: 241 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

utils/generate_markdown/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "generate_markdown"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
# `char::from`: be743d53d3404c9b13486c266b3b982fdd6c38ed | ~0.81s
8+
# unsafe `String` as mut vec extend: 4148dc7b01bb7d5b62817c660d64620652242c91 | ~0.61s
9+
# Branchless jump (using `char::from`): 4148dc7b01bb7d5b62817c660d64620652242c91 | ~0.81s
10+
rand = { git = "https://github.com/1Git2Clone/rand", rev = "4148dc7b01bb7d5b62817c660d64620652242c91" }
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*!
2+
Mainly made for benchmarking `taboc`.
3+
4+
The performance (Ryzen 5 3600X):
5+
6+
```sh
7+
time taboc output.md --allow-dirty
8+
# taboc output.md --allow-dirty 0.89s user 0.40s system 97% cpu 1.321 total
9+
```
10+
*/
11+
12+
use rand::{
13+
distr::{Alphabetic, SampleString},
14+
prelude::*,
15+
};
16+
use std::{
17+
fs::OpenOptions,
18+
io::{BufWriter, Write},
19+
};
20+
21+
const RAND_CHAR_COUNT: usize = 128;
22+
const LINE_COUNT: usize = 1_000_000;
23+
24+
fn generate_heading_level(rng: &mut ThreadRng) -> u32 {
25+
rng.next_u32() % 6
26+
}
27+
28+
fn generate_heading(rng: &mut ThreadRng) -> String {
29+
Alphabetic.sample_string(rng, RAND_CHAR_COUNT)
30+
}
31+
32+
fn main() -> std::io::Result<()> {
33+
let file = OpenOptions::new()
34+
.create(true)
35+
.truncate(true)
36+
.write(true)
37+
.read(true)
38+
.open("output.md")?;
39+
let mut writer = BufWriter::new(file);
40+
let mut rng = rand::rng();
41+
42+
// Taboc requires the first two headings to be proper markdown headings.
43+
// (Otherwise it'd just insert itself in a wrong section).
44+
writer.write_all(format!("# {}\n\n", generate_heading(&mut rng)).as_bytes())?;
45+
writer.write_all(format!("## {}\n\n", generate_heading(&mut rng)).as_bytes())?;
46+
47+
for _ in 3..=LINE_COUNT {
48+
let line = format!(
49+
"{} {}\n\n",
50+
"#".repeat(generate_heading_level(&mut rng) as usize + 1),
51+
generate_heading(&mut rng)
52+
);
53+
writer.write_all(line.as_bytes())?;
54+
}
55+
56+
Ok(())
57+
}

0 commit comments

Comments
 (0)