Skip to content

Commit

Permalink
perf: ⚡️ switched to Rc<T>s instead of Arc<T>s
Browse files Browse the repository at this point in the history
BREAKING CHANGE: all `Arc<T>`s are now `Rc<T>`s
  • Loading branch information
arctic-hen7 committed Sep 9, 2021
1 parent 803b4f6 commit 8d70599
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 17 deletions.
4 changes: 2 additions & 2 deletions examples/cli/src/pages/about.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use perseus::Template;
use std::sync::Arc;
use std::rc::Rc;
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};

#[component(AboutPage<G>)]
Expand All @@ -14,7 +14,7 @@ pub fn get_page<G: GenericNode>() -> Template<G> {
}

pub fn template_fn<G: GenericNode>() -> perseus::template::TemplateFn<G> {
Arc::new(|_, _| {
Rc::new(|_, _| {
template! {
AboutPage()
}
Expand Down
6 changes: 3 additions & 3 deletions examples/cli/src/pages/index.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use perseus::{StringResultWithCause, Template};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::rc::Rc;
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};

#[derive(Serialize, Deserialize, Debug)]
Expand All @@ -18,7 +18,7 @@ pub fn index_page(props: IndexPageProps) -> SycamoreTemplate<G> {

pub fn get_page<G: GenericNode>() -> Template<G> {
Template::new("index")
.build_state_fn(Arc::new(get_static_props))
.build_state_fn(Rc::new(get_static_props))
.template(template_fn())
}

Expand All @@ -30,7 +30,7 @@ pub async fn get_static_props(_path: String) -> StringResultWithCause<String> {
}

pub fn template_fn<G: GenericNode>() -> perseus::template::TemplateFn<G> {
Arc::new(|props: Option<String>, _| {
Rc::new(|props: Option<String>, _| {
template! {
IndexPage(
serde_json::from_str::<IndexPageProps>(&props.unwrap()).unwrap()
Expand Down
3 changes: 1 addition & 2 deletions examples/i18n/src/templates/about.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use perseus::{Template, Translator};
use std::rc::Rc;
use std::sync::Arc;
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};

#[component(AboutPage<G>)]
Expand All @@ -12,7 +11,7 @@ pub fn about_page(translator: Rc<Translator>) -> SycamoreTemplate<G> {
}

pub fn template_fn<G: GenericNode>() -> perseus::template::TemplateFn<G> {
Arc::new(|_, translator: Rc<Translator>| {
Rc::new(|_, translator: Rc<Translator>| {
template! {
AboutPage(translator)
}
Expand Down
3 changes: 1 addition & 2 deletions examples/i18n/src/templates/index.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use perseus::{Template, Translator};
use std::rc::Rc;
use std::sync::Arc;
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};

#[component(IndexPage<G>)]
Expand All @@ -20,7 +19,7 @@ pub fn index_page(translator: Rc<Translator>) -> SycamoreTemplate<G> {
}

pub fn template_fn<G: GenericNode>() -> perseus::template::TemplateFn<G> {
Arc::new(|_, translator: Rc<Translator>| {
Rc::new(|_, translator: Rc<Translator>| {
template! {
IndexPage(translator)
}
Expand Down
15 changes: 7 additions & 8 deletions packages/perseus/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use futures::Future;
use std::collections::HashMap;
use std::pin::Pin;
use std::rc::Rc;
use std::sync::Arc;
use sycamore::prelude::{GenericNode, Template as SycamoreTemplate};

/// Represents all the different states that can be generated for a single template, allowing amalgamation logic to be run with the knowledge
Expand Down Expand Up @@ -112,17 +111,17 @@ make_async_trait!(ShouldRevalidateFnType, StringResultWithCause<bool>);
// A series of closure types that should not be typed out more than once
/// The type of functions that are given a state and render a page. If you've defined state for your page, it's safe to `.unwrap()` the
/// given `Option`. If you're using i18n, this will also be given an `Rc<Translator>` (all templates share ownership of the translations).
pub type TemplateFn<G> = Arc<dyn Fn(Option<String>, Rc<Translator>) -> SycamoreTemplate<G>>;
pub type TemplateFn<G> = Rc<dyn Fn(Option<String>, Rc<Translator>) -> SycamoreTemplate<G>>;
/// The type of functions that get build paths.
pub type GetBuildPathsFn = Arc<dyn GetBuildPathsFnType>;
pub type GetBuildPathsFn = Rc<dyn GetBuildPathsFnType>;
/// The type of functions that get build state.
pub type GetBuildStateFn = Arc<dyn GetBuildStateFnType>;
pub type GetBuildStateFn = Rc<dyn GetBuildStateFnType>;
/// The type of functions that get request state.
pub type GetRequestStateFn = Arc<dyn GetRequestStateFnType>;
pub type GetRequestStateFn = Rc<dyn GetRequestStateFnType>;
/// The type of functions that check if a template sghould revalidate.
pub type ShouldRevalidateFn = Arc<dyn ShouldRevalidateFnType>;
pub type ShouldRevalidateFn = Rc<dyn ShouldRevalidateFnType>;
/// The type of functions that amalgamate build and request states.
pub type AmalgamateStatesFn = Arc<dyn Fn(States) -> StringResultWithCause<Option<String>>>;
pub type AmalgamateStatesFn = Rc<dyn Fn(States) -> StringResultWithCause<Option<String>>>;

/// This allows the specification of all the template templates in an app and how to render them. If no rendering logic is provided at all,
/// the template will be prerendered at build-time with no state. All closures are stored on the heap to avoid hellish lifetime specification.
Expand Down Expand Up @@ -170,7 +169,7 @@ impl<G: GenericNode> Template<G> {
pub fn new(path: impl Into<String> + std::fmt::Display) -> Self {
Self {
path: path.to_string(),
template: Arc::new(|_: Option<String>, _: Rc<Translator>| sycamore::template! {}),
template: Rc::new(|_: Option<String>, _: Rc<Translator>| sycamore::template! {}),
get_build_paths: None,
incremental_path_rendering: false,
get_build_state: None,
Expand Down

0 comments on commit 8d70599

Please sign in to comment.