Skip to content

Commit

Permalink
various perf fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats committed Jul 19, 2024
1 parent 80ca340 commit fb24812
Show file tree
Hide file tree
Showing 20 changed files with 351 additions and 326 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ tree-sitter-md = { version = "0.1.7", optional = true }
tree-sitter-rust = { version = "0.20.4", optional = true }
tree-sitter-html = { version = "0.20.0", optional = true }
tree-sitter-bash = { version = "0.20.5", optional = true }
itoa = "1.0.10"

[dev-dependencies]
anyhow = { version = "1.0.58" }
Expand Down
57 changes: 43 additions & 14 deletions src/html/comrak_adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,42 @@ pub struct ToCEntry {
pub anchor: String,
}

#[derive(Default)]
pub struct Anchorizer {
map: HashMap<String, i32>,
itoa_buffer: itoa::Buffer,
reuse_last_space_for_id: bool,
}

impl Anchorizer {
pub fn anchorize(&mut self, s: &str) -> String {
let mut s = s.to_lowercase();
let last_space = s.rfind(" ").unwrap_or(0);
s = REJECTED_CHARS.replace_all(&s, "").replace(" ", "-");

if let Some(count) = self.map.get_mut(&s) {
let a = self.itoa_buffer.format(*count);
if last_space > 0 && self.reuse_last_space_for_id {
s.replace_range(last_space..last_space + a.len(), &a);
} else {
s.push('-');
s.push_str(a);
}

*count += 1;
} else {
self.map.insert(s.clone(), 1);
}

s
}
}

#[derive(Clone)]
pub struct HeadingToCAdapter {
toc: Arc<Mutex<Vec<ToCEntry>>>,
anchorizer: Arc<Mutex<comrak::html::Anchorizer>>,
offset: Arc<Mutex<u8>>,
anchorizer: Arc<Mutex<Anchorizer>>,
}

impl Default for HeadingToCAdapter {
Expand All @@ -249,18 +280,18 @@ impl Default for HeadingToCAdapter {
}
}

lazy_static! {
static ref REJECTED_CHARS: regex::Regex =
regex::Regex::new(r"[^\p{L}\p{M}\p{N}\p{Pc} -]").unwrap();
}

impl HeadingToCAdapter {
pub fn anchorize(&self, content: String) -> String {
pub fn anchorize(&self, content: &str) -> String {
let mut anchorizer = self.anchorizer.lock().unwrap();
anchorizer.anchorize(content.clone())
anchorizer.anchorize(content)
}

pub fn add_entry(
&self,
level: u8,
content: String,
anchor: String,
) -> String {
pub fn add_entry(&self, level: u8, content: &str, anchor: &str) {
let mut toc = self.toc.lock().unwrap();
let mut offset = self.offset.lock().unwrap();

Expand All @@ -269,12 +300,10 @@ impl HeadingToCAdapter {
if toc.last().map_or(true, |toc| toc.content != content) {
toc.push(ToCEntry {
level,
content,
anchor: anchor.clone(),
content: content.to_owned(),
anchor: anchor.to_owned(),
});
}

anchor
}

pub fn render(self) -> Option<String> {
Expand Down Expand Up @@ -322,7 +351,7 @@ impl HeadingAdapter for HeadingToCAdapter {
let mut anchorizer = self.anchorizer.lock().unwrap();
let offset = self.offset.lock().unwrap();

let anchor = anchorizer.anchorize(heading.content.clone());
let anchor = anchorizer.anchorize(&heading.content);
writeln!(output, r#"<h{} id="{anchor}">"#, heading.level)?;

let mut toc = self.toc.lock().unwrap();
Expand Down
36 changes: 18 additions & 18 deletions src/html/jsdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,13 @@ pub fn markdown_to_html(
}

let mut plugins = comrak::Plugins::default();
plugins.render.codefence_syntax_highlighter =
Some(&render_ctx.ctx.highlight_adapter);
if !render_options.no_toc {
plugins.render.heading_adapter = Some(&render_ctx.toc);

if !render_options.summary {
plugins.render.codefence_syntax_highlighter =
Some(&render_ctx.ctx.highlight_adapter);
if !render_options.no_toc {
plugins.render.heading_adapter = Some(&render_ctx.toc);
}
}

let md = parse_links(md, render_ctx);
Expand Down Expand Up @@ -649,20 +652,17 @@ impl ModuleDocCtx {

sections.extend(super::namespace::render_namespace(
render_ctx,
partitions_by_kind
.into_iter()
.map(|(title, nodes)| {
(
SectionHeaderCtx {
title: title.clone(),
anchor: AnchorCtx { id: title },
href: None,
doc: None,
},
nodes,
)
})
.collect(),
partitions_by_kind.into_iter().map(|(title, nodes)| {
(
SectionHeaderCtx {
title: title.clone(),
anchor: AnchorCtx { id: title },
href: None,
doc: None,
},
nodes,
)
}),
));
}

Expand Down
Loading

0 comments on commit fb24812

Please sign in to comment.