Skip to content

Commit

Permalink
Split ID maps in two parts: the constant one and the updated one
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Nov 23, 2024
1 parent 61a6981 commit c4c6cb2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 67 deletions.
118 changes: 57 additions & 61 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use std::iter::Peekable;
use std::ops::{ControlFlow, Range};
use std::path::PathBuf;
use std::str::{self, CharIndices};
use std::sync::OnceLock;

use pulldown_cmark::{
BrokenLink, CodeBlockKind, CowStr, Event, LinkType, Options, Parser, Tag, TagEnd, html,
Expand Down Expand Up @@ -1886,83 +1885,81 @@ pub(crate) fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec<Rust
#[derive(Clone, Default, Debug)]
pub struct IdMap {
map: FxHashMap<Cow<'static, str>, usize>,
defined_ids: FxHashSet<&'static str>,
existing_footnotes: usize,
}

// The map is pre-initialized and cloned each time to avoid reinitializing it repeatedly.
static DEFAULT_ID_MAP: OnceLock<FxHashSet<Cow<'static, str>>> = OnceLock::new();

fn init_id_map() -> FxHashSet<Cow<'static, str>> {
fn init_id_map() -> FxHashSet<&'static str> {
let mut map = FxHashSet::default();
// This is the list of IDs used in JavaScript.
map.insert("help".into());
map.insert("settings".into());
map.insert("not-displayed".into());
map.insert("alternative-display".into());
map.insert("search".into());
map.insert("crate-search".into());
map.insert("crate-search-div".into());
map.insert("help");
map.insert("settings");
map.insert("not-displayed");
map.insert("alternative-display");
map.insert("search");
map.insert("crate-search");
map.insert("crate-search-div");
// This is the list of IDs used in HTML generated in Rust (including the ones
// used in tera template files).
map.insert("themeStyle".into());
map.insert("settings-menu".into());
map.insert("help-button".into());
map.insert("sidebar-button".into());
map.insert("main-content".into());
map.insert("toggle-all-docs".into());
map.insert("all-types".into());
map.insert("default-settings".into());
map.insert("sidebar-vars".into());
map.insert("copy-path".into());
map.insert("rustdoc-toc".into());
map.insert("rustdoc-modnav".into());
map.insert("themeStyle");
map.insert("settings-menu");
map.insert("help-button");
map.insert("sidebar-button");
map.insert("main-content");
map.insert("toggle-all-docs");
map.insert("all-types");
map.insert("default-settings");
map.insert("sidebar-vars");
map.insert("copy-path");
map.insert("rustdoc-toc");
map.insert("rustdoc-modnav");
// This is the list of IDs used by rustdoc sections (but still generated by
// rustdoc).
map.insert("fields".into());
map.insert("variants".into());
map.insert("implementors-list".into());
map.insert("synthetic-implementors-list".into());
map.insert("foreign-impls".into());
map.insert("implementations".into());
map.insert("trait-implementations".into());
map.insert("synthetic-implementations".into());
map.insert("blanket-implementations".into());
map.insert("required-associated-types".into());
map.insert("provided-associated-types".into());
map.insert("provided-associated-consts".into());
map.insert("required-associated-consts".into());
map.insert("required-methods".into());
map.insert("provided-methods".into());
map.insert("dyn-compatibility".into());
map.insert("implementors".into());
map.insert("synthetic-implementors".into());
map.insert("implementations-list".into());
map.insert("trait-implementations-list".into());
map.insert("synthetic-implementations-list".into());
map.insert("blanket-implementations-list".into());
map.insert("deref-methods".into());
map.insert("layout".into());
map.insert("aliased-type".into());
map.insert("fields");
map.insert("variants");
map.insert("implementors-list");
map.insert("synthetic-implementors-list");
map.insert("foreign-impls");
map.insert("implementations");
map.insert("trait-implementations");
map.insert("synthetic-implementations");
map.insert("blanket-implementations");
map.insert("required-associated-types");
map.insert("provided-associated-types");
map.insert("provided-associated-consts");
map.insert("required-associated-consts");
map.insert("required-methods");
map.insert("provided-methods");
map.insert("dyn-compatibility");
map.insert("implementors");
map.insert("synthetic-implementors");
map.insert("implementations-list");
map.insert("trait-implementations-list");
map.insert("synthetic-implementations-list");
map.insert("blanket-implementations-list");
map.insert("deref-methods");
map.insert("layout");
map.insert("aliased-type");
map
}

impl IdMap {
pub fn new() -> Self {
let mut id_map = IdMap { map: FxHashMap::default(), existing_footnotes: 0 };
id_map.init_map();
id_map
}

#[allow(rustc::potential_query_instability)]
fn init_map(&mut self) {
for key in DEFAULT_ID_MAP.get_or_init(init_id_map).iter() {
self.map.insert(key.clone(), 1);
}
IdMap { map: FxHashMap::default(), defined_ids: init_id_map(), existing_footnotes: 0 }
}

pub(crate) fn derive<S: AsRef<str> + ToString>(&mut self, candidate: S) -> String {
let id = match self.map.get_mut(candidate.as_ref()) {
None => candidate.to_string(),
None => {
let candidate = candidate.to_string();
if self.defined_ids.contains(candidate.as_str()) {
let id = format!("{}-{}", candidate, 1);
self.map.insert(candidate.into(), 2);
id
} else {
candidate.to_string()
}
}
Some(a) => {
let id = format!("{}-{}", candidate.as_ref(), *a);
*a += 1;
Expand All @@ -1985,7 +1982,6 @@ impl IdMap {

pub(crate) fn clear(&mut self) {
self.map.clear();
self.init_map();
self.existing_footnotes = 0;
}
}
6 changes: 0 additions & 6 deletions src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,6 @@ impl ContextInfo {
}
}

// `Context` is cloned a lot, so we don't want the size to grow unexpectedly.
#[cfg(all(not(windows), target_pointer_width = "64"))]
rustc_data_structures::static_assert_size!(Context<'_>, 192);
#[cfg(all(windows, target_pointer_width = "64"))]
rustc_data_structures::static_assert_size!(Context<'_>, 200);

/// Shared mutable state used in [`Context`] and elsewhere.
pub(crate) struct SharedContext<'tcx> {
pub(crate) tcx: TyCtxt<'tcx>,
Expand Down

0 comments on commit c4c6cb2

Please sign in to comment.