Skip to content

Commit

Permalink
chore(performance): use concat_strs
Browse files Browse the repository at this point in the history
remove concat-in-place
  • Loading branch information
fiji-flo committed Sep 24, 2024
1 parent e1721d1 commit 67aba1e
Show file tree
Hide file tree
Showing 22 changed files with 205 additions and 145 deletions.
8 changes: 0 additions & 8 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ serde_json = { version = "1", features = ["preserve_order"] }
chrono = { version = "0.4", features = ["serde"] }
regex = "1"
url = { version = "2", features = ["serde"] }
concat-in-place = "1"
itertools = "0.13"
constcat = "0.5"
anyhow = "1"
Expand Down
1 change: 0 additions & 1 deletion crates/rari-doc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ regex.workspace = true
tracing.workspace = true
url.workspace = true
itertools.workspace = true
concat-in-place.workspace = true
constcat.workspace = true
indexmap.workspace = true

Expand Down
3 changes: 2 additions & 1 deletion crates/rari-doc/src/helpers/webextapi.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use rari_types::fm_types::PageType;
use rari_types::locale::Locale;
use rari_utils::concat_strs;

use super::l10n::l10n_json_data;
use crate::error::DocError;
Expand All @@ -15,7 +16,7 @@ pub fn entry(slug: &str, locale: Locale) -> Result<Vec<SidebarMetaEntry>, DocErr
let events_label = l10n_json_data("Common", "Events", locale)?;

let sub_pages = get_sub_pages(
&format!("/en-US/docs/{}", slug),
&concat_strs!("/en-US/docs/", slug),
Some(1),
SubPagesSorter::TitleAPI,
)?;
Expand Down
3 changes: 2 additions & 1 deletion crates/rari-doc/src/html/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::borrow::Cow;
use rari_md::anchor::anchorize;
use rari_types::fm_types::FeatureStatus;
use rari_types::locale::Locale;
use rari_utils::concat_strs;
use tracing::warn;

use crate::error::DocError;
Expand Down Expand Up @@ -90,7 +91,7 @@ pub fn render_link_via_page(
if let Some(link) = link.strip_prefix('/') {
if let Some(locale) = locale {
if !link.starts_with(Locale::default().as_url_str()) {
url = Cow::Owned(format!("/{}/docs/{link}", locale.as_url_str()));
url = Cow::Owned(concat_strs!("/", locale.as_url_str(), "/docs/", link));
}
};
let (url, anchor) = url.split_once('#').unwrap_or((&url, ""));
Expand Down
45 changes: 25 additions & 20 deletions crates/rari-doc/src/html/rewriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use lol_html::{element, rewrite_str, HtmlRewriter, RewriteStrSettings, Settings}
use rari_md::node_card::NoteCard;
use rari_types::fm_types::PageType;
use rari_types::locale::Locale;
use rari_utils::concat_strs;
use tracing::warn;
use url::Url;

Expand Down Expand Up @@ -42,8 +43,8 @@ pub fn post_process_html<T: PageLike>(
let open_dt_a = std::rc::Rc::new(std::cell::RefCell::new(false));
let options = Url::options();
let url = page.url();
let base = Url::parse(&format!(
"http://rari.placeholder{}{}",
let base = Url::parse(&concat_strs!(
"http://rari.placeholder",
url,
if url.ends_with('/') { "" } else { "/" }
))?;
Expand Down Expand Up @@ -140,7 +141,7 @@ pub fn post_process_html<T: PageLike>(
.unwrap_or(&href);
let no_locale = strip_locale_from_url(href).0.is_none();
let maybe_prefixed_href = if no_locale {
Cow::Owned(format!("/{}{href}", Locale::default().as_url_str()))
Cow::Owned(concat_strs!("/", Locale::default().as_url_str(), href))
} else {
Cow::Borrowed(href)
};
Expand All @@ -156,9 +157,10 @@ pub fn post_process_html<T: PageLike>(
let class = el.get_attribute("class").unwrap_or_default();
el.set_attribute(
"class",
&format!(
"{class}{}page-not-created",
if class.is_empty() { "" } else { " " }
&concat_strs!(
&class,
if class.is_empty() { "" } else { " " },
"page-not-created"
),
)?;
el.set_attribute("title", l10n_json_data("Common", "summary", page.locale())?)?;
Expand All @@ -176,7 +178,7 @@ pub fn post_process_html<T: PageLike>(
if !class.split(' ').any(|s| s == "external") {
el.set_attribute(
"class",
&format!("{class}{}external", if class.is_empty() { "" } else { " " }),
&concat_strs!(&class, if class.is_empty() { "" } else { " " }, "external"),
)?;
}
if !el.has_attribute("target") {
Expand All @@ -189,7 +191,7 @@ pub fn post_process_html<T: PageLike>(
element!("dt[data-add-link]", |el| {
el.remove_attribute("data-add-link");
if let Some(id) = el.get_attribute("id") {
el.prepend(&format!("<a href=\"#{id}\">"), ContentType::Html);
el.prepend(&concat_strs!("<a href=\"#", &id, "\">"), ContentType::Html);
let mut s = open_dt_a.borrow_mut();
*s = true;
let open_dt_a = open_dt_a.clone();
Expand Down Expand Up @@ -225,8 +227,8 @@ pub fn post_process_html<T: PageLike>(
.unwrap_or_default();
if !name.is_empty() && name != "plain" {
el.before(
&format!(
r#"<div class="code-example"><div class='example-header'><span class="language-name">{name}</span></div>"#,
&concat_strs!(
r#"<div class="code-example"><div class='example-header'><span class="language-name">"#, name, "</span></div>"
),
ContentType::Html
);
Expand All @@ -241,29 +243,32 @@ pub fn post_process_html<T: PageLike>(
}),
element!("div.notecard.callout > p:first-child", |el| {
el.prepend(
&format!(
"<strong>{}</strong>",
NoteCard::Callout.prefix_for_locale(page.locale())
&concat_strs!(
"<strong>",
NoteCard::Callout.prefix_for_locale(page.locale()),
"</strong>"
),
ContentType::Html,
);
Ok(())
}),
element!("div.notecard.warning > p:first-child", |el| {
el.prepend(
&format!(
"<strong>{}</strong>",
NoteCard::Warning.prefix_for_locale(page.locale())
&concat_strs!(
"<strong>",
NoteCard::Warning.prefix_for_locale(page.locale()),
"</strong>"
),
ContentType::Html,
);
Ok(())
}),
element!("div.notecard.note > p:first-child", |el| {
el.prepend(
&format!(
"<strong>{}</strong>",
NoteCard::Note.prefix_for_locale(page.locale())
&concat_strs!(
"<strong>",
NoteCard::Note.prefix_for_locale(page.locale()),
"</strong>"
),
ContentType::Html,
);
Expand Down Expand Up @@ -299,7 +304,7 @@ pub fn post_process_html<T: PageLike>(
el.set_attribute(
"href",
&split_href
.map(|s| Cow::Owned(format!("{}#{}", page.url(), s.1)))
.map(|s| Cow::Owned(concat_strs!(page.url(), "#", s.1)))
.unwrap_or(Cow::Borrowed(page.url())),
)?;
}
Expand Down
8 changes: 6 additions & 2 deletions crates/rari-doc/src/pages/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use std::borrow::Cow;
use std::fs;
use std::path::Path;

use concat_in_place::strcat;
use rari_types::fm_types::PageType;
use rari_types::globals::{base_url, content_branch, git_history, popularities};
use rari_types::locale::Locale;
use rari_utils::concat_strs;
use scraper::Html;

use super::json::{
Expand Down Expand Up @@ -339,7 +339,11 @@ pub fn build_generic_page(page: &GenericPage) -> Result<BuiltDocy, DocError> {
title: page.meta.title.clone(),
toc,
},
page_title: strcat!(page.meta.title.as_str() " | " page.meta.title_suffix.as_str()),
page_title: concat_strs!(
page.meta.title.as_str(),
" | ",
page.meta.title_suffix.as_str()
),
url: page.meta.url.clone(),
id: page.meta.page.clone(),
})))
Expand Down
14 changes: 9 additions & 5 deletions crates/rari-doc/src/pages/types/contributors.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::path::{Path, PathBuf};
use std::sync::Arc;

use concat_in_place::strcat;
use rari_md::m2h;
use rari_types::error::EnvError;
use rari_types::fm_types::{FeatureStatus, PageType};
use rari_types::globals::contributor_spotlight_root;
use rari_types::locale::Locale;
use rari_types::RariEnv;
use rari_utils::concat_strs;
use rari_utils::io::read_to_string;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -108,11 +108,15 @@ impl ContributorBuildMeta {
usernames,
quote,
} = fm;
let slug = strcat!("spotlight/" folder_name.as_str());
let slug = concat_strs!("spotlight/", folder_name.as_str());
Ok(Self {
url: strcat!("/" locale.as_url_str() "/community/" slug.as_str()),
url: concat_strs!("/", locale.as_url_str(), "/community/", slug.as_str()),
locale,
title: strcat!("Contributor Spotlight - " contributor_name.as_str() " | MDN"),
title: concat_strs!(
"Contributor, Spotlight - ",
contributor_name.as_str(),
" | MDN"
),
slug,
contributor_name,
folder_name,
Expand Down Expand Up @@ -142,7 +146,7 @@ impl ContributorSpotlight {
content_start,
} = self.clone();
meta.locale = locale;
meta.url = strcat!("/" locale.as_url_str() "/community/" meta.slug.as_str());
meta.url = concat_strs!("/", locale.as_url_str(), "/community/", meta.slug.as_str());
Self {
meta,
raw,
Expand Down
19 changes: 13 additions & 6 deletions crates/rari-doc/src/pages/types/generic.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::path::{Path, PathBuf};
use std::sync::Arc;

use concat_in_place::strcat;
use rari_types::fm_types::{FeatureStatus, PageType};
use rari_types::globals::generic_pages_root;
use rari_types::locale::Locale;
use rari_types::RariEnv;
use rari_utils::concat_strs;
use rari_utils::io::read_to_string;
use serde::Deserialize;

Expand Down Expand Up @@ -41,7 +41,14 @@ impl GenericPageMeta {
title_suffix: &str,
page: String,
) -> Result<Self, DocError> {
let url = strcat!("/" locale.as_url_str() "/" slug.as_str() "/" page.as_str());
let url = concat_strs!(
"/",
locale.as_url_str(),
"/",
slug.as_str(),
"/",
page.as_str()
);
Ok(GenericPageMeta {
title: fm.title,
locale,
Expand Down Expand Up @@ -90,7 +97,7 @@ pub struct GenericPage {

impl GenericPage {
pub fn from_slug(slug: &str, locale: Locale) -> Option<Page> {
let url = strcat!("/" locale.as_url_str() "/" slug).to_ascii_lowercase();
let url = concat_strs!("/", locale.as_url_str(), "/", slug).to_ascii_lowercase();
generic_pages_files().get(&url).cloned()
}

Expand All @@ -101,7 +108,7 @@ impl GenericPage {
content_start,
} = self.clone();
meta.locale = locale;
meta.url = strcat!("/" locale.as_url_str() "/" meta.slug.as_str());
meta.url = concat_strs!("/", locale.as_url_str(), "/", meta.slug.as_str());
Self {
meta,
raw,
Expand All @@ -110,7 +117,7 @@ impl GenericPage {
}

pub fn is_generic(slug: &str, locale: Locale) -> bool {
let url = strcat!("/" locale.as_url_str() "/" slug).to_ascii_lowercase();
let url = concat_strs!("/", locale.as_url_str(), "/", slug).to_ascii_lowercase();
generic_pages_files().contains_key(&url)
}
}
Expand Down Expand Up @@ -198,7 +205,7 @@ fn read_generic_page(
let path = full_path.strip_prefix(root)?.to_path_buf();
let page = path.with_extension("");
let page = page.to_string_lossy();
let slug = strcat!(slug "/" page.as_ref());
let slug = concat_strs!(slug, "/", page.as_ref());

Ok(GenericPage {
meta: GenericPageMeta::from_fm(
Expand Down
37 changes: 23 additions & 14 deletions crates/rari-doc/src/pages/types/spa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use std::borrow::Cow;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use concat_in_place::strcat;
use constcat::concat;
use phf::{phf_map, Map};
use rari_types::fm_types::{FeatureStatus, PageType};
use rari_types::globals::content_translated_root;
use rari_types::locale::Locale;
use rari_types::RariEnv;
use rari_utils::concat_strs;

use super::spa_homepage::{
featured_articles, featured_contributor, lastet_news, recent_contributions,
Expand Down Expand Up @@ -57,17 +57,26 @@ impl SPA {

pub fn from_slug(slug: &str, locale: Locale) -> Option<Page> {
BASIC_SPAS.get(slug).and_then(|build_spa| {
if build_spa.en_us_only && locale != Locale::EnUs { None } else {
Some(Page::SPA(Arc::new(SPA {
page_title: build_spa.page_title,
slug: build_spa.slug,
url: strcat!("/" locale.as_url_str() "/" build_spa.slug if build_spa.trailing_slash { "/" } else { "" }),
locale,
page_type: PageType::SPA,
data: build_spa.data,
base_slug: Cow::Owned(strcat!("/" locale.as_url_str() "/")),
page_description: build_spa.page_description,
})))}
if build_spa.en_us_only && locale != Locale::EnUs {
None
} else {
Some(Page::SPA(Arc::new(SPA {
page_title: build_spa.page_title,
slug: build_spa.slug,
url: concat_strs!(
"/",
locale.as_url_str(),
"/",
build_spa.slug,
if build_spa.trailing_slash { "/" } else { "" }
),
locale,
page_type: PageType::SPA,
data: build_spa.data,
base_slug: Cow::Owned(concat_strs!("/", locale.as_url_str(), "/")),
page_description: build_spa.page_description,
})))
}
})
}

Expand Down Expand Up @@ -129,10 +138,10 @@ impl SPA {
page_description: self.page_description,
only_follow: basic_spa.only_follow,
no_indexing: basic_spa.no_indexing,
url: strcat!(self.base_slug.as_ref() self.slug),
url: concat_strs!(self.base_slug.as_ref(), self.slug),
}))),
SPAData::HomePage => Ok(BuiltDocy::HomePageSPA(Box::new(JsonHomePageSPA {
url: strcat!("/" self.locale().as_url_str() "/" self.slug),
url: concat_strs!("/", self.locale().as_url_str(), "/", self.slug),
page_title: self.page_title,
hy_data: JsonHomePageSPAHyData {
page_description: self.page_description,
Expand Down
Loading

0 comments on commit 67aba1e

Please sign in to comment.