Skip to content

Commit

Permalink
Rework a bunch of cfg(feature) flags to be more principled (#2666)
Browse files Browse the repository at this point in the history
* rework a bunch of feature flags to be more principled

* fixup of Collectable

* silence clippy

* unfix Mountable trait

leads to bundle size increase, so stick to the optimized version

* use required-features for function_router

* use --all-features in various places

* disable tokio dependency on wasm tests again

* add some #[inline] annotations

* merge one more feature annotations

* fixup: remove a line connected to trace_hydration
  • Loading branch information
WorldSEnder committed May 7, 2022
1 parent 64e2582 commit 1d579a8
Show file tree
Hide file tree
Showing 49 changed files with 362 additions and 398 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/main-checks.yml
Expand Up @@ -120,7 +120,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
args: -p yew --doc --features doc_test --target wasm32-unknown-unknown
args: -p yew --doc --all-features --target wasm32-unknown-unknown

integration_tests:
name: Integration Tests on ${{ matrix.toolchain }}
Expand Down Expand Up @@ -159,8 +159,8 @@ jobs:
- name: Run tests - yew
run: |
cd packages/yew
CHROMEDRIVER=$(which chromedriver) cargo test --features wasm_test --target wasm32-unknown-unknown
GECKODRIVER=$(which geckodriver) cargo test --features wasm_test --target wasm32-unknown-unknown
CHROMEDRIVER=$(which chromedriver) cargo test --features csr,hydration,ssr --target wasm32-unknown-unknown
GECKODRIVER=$(which geckodriver) cargo test --features csr,hydration,ssr --target wasm32-unknown-unknown
- name: Run tests - yew-router
run: |
Expand Down
5 changes: 3 additions & 2 deletions examples/function_router/Cargo.toml
Expand Up @@ -19,5 +19,6 @@ instant = { version = "0.1", features = ["wasm-bindgen"] }
[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2", features = ["js"] }

[features]
csr = ["yew/csr"]
[[bin]]
name = "function_router"
required-features = ["yew/csr"]
2 changes: 1 addition & 1 deletion examples/function_router/index.html
Expand Up @@ -11,7 +11,7 @@
href="https://cdn.jsdelivr.net/npm/bulma@0.9.0/css/bulma.min.css"
/>
<link data-trunk rel="sass" href="index.scss" />
<link data-trunk rel="rust" data-cargo-features="csr" data-bin="function_router" />
<link data-trunk rel="rust" data-cargo-features="yew/csr" data-bin="function_router" />
</head>

<body></body>
Expand Down
1 change: 0 additions & 1 deletion examples/function_router/src/bin/function_router.rs
Expand Up @@ -2,6 +2,5 @@ pub use function_router::*;

fn main() {
wasm_logger::init(wasm_logger::Config::new(log::Level::Trace));
#[cfg(feature = "csr")]
yew::Renderer::<App>::new().render();
}
2 changes: 1 addition & 1 deletion examples/ssr_router/Cargo.toml
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
yew = { path = "../../packages/yew", features = ["ssr", "hydration", "trace_hydration"] }
yew = { path = "../../packages/yew", features = ["ssr", "hydration"] }
function_router = { path = "../function_router" }
log = "0.4"

Expand Down
2 changes: 1 addition & 1 deletion examples/suspense/Cargo.toml
Expand Up @@ -7,7 +7,7 @@ license = "MIT OR Apache-2.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
yew = { path = "../../packages/yew", features = ["tokio", "csr"] }
yew = { path = "../../packages/yew", features = ["csr"] }
gloo-timers = { version = "0.2.2", features = ["futures"] }
wasm-bindgen-futures = "0.4"
wasm-bindgen = "0.2"
Expand Down
2 changes: 1 addition & 1 deletion examples/suspense/src/main.rs
Expand Up @@ -26,7 +26,7 @@ fn app_content() -> HtmlResult {
})
};

let on_take_a_break = Callback::from(move |_| (resleep.clone())());
let on_take_a_break = Callback::from(move |_| resleep());

Ok(html! {
<div class="content-area">
Expand Down
1 change: 0 additions & 1 deletion packages/yew-macro/Cargo.toml
Expand Up @@ -34,5 +34,4 @@ yew = { path = "../yew" }
[build-dependencies]

[features]
doc_test = []
lints = []
3 changes: 0 additions & 3 deletions packages/yew-router/Cargo.toml
Expand Up @@ -11,9 +11,6 @@ description = "A router implementation for the Yew framework"
repository = "https://github.com/yewstack/yew"
rust-version = "1.56.0"

[features]
wasm_test = []

[dependencies]
yew = { version = "0.19.3", path = "../yew", default-features= false }
yew-router-macro = { version = "0.16.0", path = "../yew-router-macro" }
Expand Down
9 changes: 4 additions & 5 deletions packages/yew/Cargo.toml
Expand Up @@ -87,17 +87,16 @@ features = [
]

[features]
ssr = ["futures", "html-escape"]
# TODO: `dep:` syntax only supported with MSRV 1.60, would be more precise
# tokio = ["dep:tokio"]
ssr = ["futures", "html-escape"] # dep:html-escape
csr = []
hydration = ["csr"]
trace_hydration = ["hydration"]
doc_test = ["csr", "hydration", "ssr"]
wasm_test = ["csr", "hydration", "ssr"]
default = []

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
tokio = { version = "1.15.0", features = ["full"] }

[package.metadata.docs.rs]
features = ["doc_test"]
all-features = true
rustdoc-args = ["--cfg", "documenting"]
3 changes: 2 additions & 1 deletion packages/yew/Makefile.toml
Expand Up @@ -10,14 +10,15 @@ args = [
"--headless",
"--",
"--features",
"wasm_test"
"csr,hydration,ssr",
]

[tasks.ssr-test]
command = "cargo"
args = ["test", "ssr_tests", "--features", "ssr"]

[tasks.test]
args = ["test", "--all-targets", "--all-features"]
dependencies = ["native-test", "wasm-test"]

[tasks.clippy-feature-soundness]
Expand Down
2 changes: 1 addition & 1 deletion packages/yew/src/app_handle.rs
Expand Up @@ -9,8 +9,8 @@ use crate::dom_bundle::BSubtree;
use crate::html::{BaseComponent, NodeRef, Scope, Scoped};

/// An instance of an application.
#[derive(Debug)]
#[cfg_attr(documenting, doc(cfg(feature = "csr")))]
#[derive(Debug)]
pub struct AppHandle<COMP: BaseComponent> {
/// `Scope` holder
pub(crate) scope: Scope<COMP>,
Expand Down
4 changes: 2 additions & 2 deletions packages/yew/src/dom_bundle/bcomp.rs
Expand Up @@ -176,7 +176,7 @@ mod feat_hydration {
}
}

#[cfg(feature = "wasm_test")]
#[cfg(target_arch = "wasm32")]
#[cfg(test)]
mod tests {
use std::ops::Deref;
Expand Down Expand Up @@ -481,7 +481,7 @@ mod tests {
}
}

#[cfg(feature = "wasm_test")]
#[cfg(target_arch = "wasm32")]
#[cfg(test)]
mod layout_tests {
extern crate self as yew;
Expand Down
8 changes: 4 additions & 4 deletions packages/yew/src/dom_bundle/blist.rs
Expand Up @@ -504,11 +504,11 @@ mod feat_hydration {
}
}

#[cfg(all(test, feature = "wasm_test"))]
#[cfg(target_arch = "wasm32")]
#[cfg(test)]
mod layout_tests {
extern crate self as yew;

#[cfg(feature = "wasm_test")]
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};

use crate::html;
Expand Down Expand Up @@ -581,11 +581,11 @@ mod layout_tests {
}
}

#[cfg(all(test, feature = "wasm_test"))]
#[cfg(target_arch = "wasm32")]
#[cfg(test)]
mod layout_tests_keys {
extern crate self as yew;

#[cfg(feature = "wasm_test")]
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
use web_sys::Node;

Expand Down
4 changes: 2 additions & 2 deletions packages/yew/src/dom_bundle/bnode.rs
Expand Up @@ -291,9 +291,9 @@ mod feat_hydration {
}
}

#[cfg(all(test, feature = "wasm_test"))]
#[cfg(target_arch = "wasm32")]
#[cfg(test)]
mod layout_tests {
#[cfg(feature = "wasm_test")]
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};

use super::*;
Expand Down
4 changes: 2 additions & 2 deletions packages/yew/src/dom_bundle/bportal.rs
Expand Up @@ -118,11 +118,11 @@ impl BPortal {
}
}

#[cfg(all(test, feature = "wasm_test"))]
#[cfg(target_arch = "wasm32")]
#[cfg(test)]
mod layout_tests {
extern crate self as yew;

#[cfg(feature = "wasm_test")]
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
use yew::virtual_dom::VPortal;

Expand Down
2 changes: 1 addition & 1 deletion packages/yew/src/dom_bundle/btag/listeners.rs
Expand Up @@ -196,7 +196,7 @@ impl Registry {
}
}

#[cfg(feature = "wasm_test")]
#[cfg(target_arch = "wasm32")]
#[cfg(test)]
mod tests {
use std::marker::PhantomData;
Expand Down
12 changes: 6 additions & 6 deletions packages/yew/src/dom_bundle/btag/mod.rs
Expand Up @@ -265,13 +265,13 @@ impl BTag {
self.key.as_ref()
}

#[cfg(feature = "wasm_test")]
#[cfg(target_arch = "wasm32")]
#[cfg(test)]
fn reference(&self) -> &Element {
&self.reference
}

#[cfg(feature = "wasm_test")]
#[cfg(target_arch = "wasm32")]
#[cfg(test)]
fn children(&self) -> &[BNode] {
match &self.inner {
Expand All @@ -280,7 +280,7 @@ impl BTag {
}
}

#[cfg(feature = "wasm_test")]
#[cfg(target_arch = "wasm32")]
#[cfg(test)]
fn tag(&self) -> &str {
match &self.inner {
Expand Down Expand Up @@ -383,7 +383,7 @@ mod feat_hydration {
}
}

#[cfg(feature = "wasm_test")]
#[cfg(target_arch = "wasm32")]
#[cfg(test)]
mod tests {
use gloo_utils::document;
Expand Down Expand Up @@ -975,11 +975,11 @@ mod tests {
}
}

#[cfg(all(test, feature = "wasm_test"))]
#[cfg(target_arch = "wasm32")]
#[cfg(test)]
mod layout_tests {
extern crate self as yew;

#[cfg(feature = "wasm_test")]
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};

use crate::html;
Expand Down
8 changes: 4 additions & 4 deletions packages/yew/src/dom_bundle/btext.rs
Expand Up @@ -157,12 +157,12 @@ mod feat_hydration {
mod test {
extern crate self as yew;

#[cfg(feature = "wasm_test")]
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};

use crate::html;

#[cfg(feature = "wasm_test")]
#[cfg(target_arch = "wasm32")]
wasm_bindgen_test_configure!(run_in_browser);

#[test]
Expand All @@ -177,11 +177,11 @@ mod test {
}
}

#[cfg(all(test, feature = "wasm_test"))]
#[cfg(target_arch = "wasm32")]
#[cfg(test)]
mod layout_tests {
extern crate self as yew;

#[cfg(feature = "wasm_test")]
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};

use crate::html;
Expand Down
4 changes: 2 additions & 2 deletions packages/yew/src/dom_bundle/fragment.rs
Expand Up @@ -3,13 +3,13 @@ use std::ops::{Deref, DerefMut};

use web_sys::{Element, Node};

use super::BSubtree;
use crate::dom_bundle::BSubtree;
use crate::html::NodeRef;
use crate::virtual_dom::Collectable;

/// A Hydration Fragment
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub(crate) struct Fragment(VecDeque<Node>);
pub struct Fragment(VecDeque<Node>);

impl Deref for Fragment {
type Target = VecDeque<Node>;
Expand Down
22 changes: 10 additions & 12 deletions packages/yew/src/dom_bundle/mod.rs
Expand Up @@ -19,9 +19,6 @@ mod btag;
mod btext;
mod subtree_root;

#[cfg(feature = "hydration")]
mod fragment;

mod traits;
mod utils;

Expand All @@ -32,16 +29,9 @@ use bportal::BPortal;
use bsuspense::BSuspense;
use btag::{BTag, Registry};
use btext::BText;
#[cfg(feature = "hydration")]
pub(crate) use fragment::Fragment;
pub use subtree_root::set_event_bubbling;
pub(crate) use subtree_root::BSubtree;
use subtree_root::EventDescriptor;
#[cfg(feature = "hydration")]
use traits::Hydratable;
pub use subtree_root::{set_event_bubbling, BSubtree};
use traits::{Reconcilable, ReconcileTarget};
#[cfg(feature = "hydration")]
use utils::node_type_str;
use utils::{insert_node, test_log};

/// A Bundle.
Expand Down Expand Up @@ -84,9 +74,15 @@ impl Bundle {
}

#[cfg(feature = "hydration")]
#[path = "."]
mod feat_hydration {
use super::*;
pub(super) use super::traits::Hydratable;
pub(super) use super::utils::node_type_str;
#[path = "./fragment.rs"]
mod fragment;
pub use fragment::Fragment;

use super::*;
impl Bundle {
/// Creates a bundle by hydrating a virtual dom layout.
pub fn hydrate(
Expand All @@ -101,3 +97,5 @@ mod feat_hydration {
}
}
}
#[cfg(feature = "hydration")]
pub(crate) use feat_hydration::*;
4 changes: 2 additions & 2 deletions packages/yew/src/dom_bundle/utils.rs
Expand Up @@ -13,13 +13,13 @@ pub(super) fn insert_node(node: &Node, parent: &Element, next_sibling: Option<&N
};
}

#[cfg(all(test, feature = "wasm_test", verbose_tests))]
#[cfg(all(test, target_arch = "wasm32", verbose_tests))]
macro_rules! test_log {
($fmt:literal, $($arg:expr),* $(,)?) => {
::wasm_bindgen_test::console_log!(concat!("\t ", $fmt), $($arg),*);
};
}
#[cfg(not(all(test, feature = "wasm_test", verbose_tests)))]
#[cfg(not(all(test, target_arch = "wasm32", verbose_tests)))]
macro_rules! test_log {
($fmt:literal, $($arg:expr),* $(,)?) => {
// Only type-check the format expression, do not run any side effects
Expand Down

1 comment on commit 1d579a8

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yew master branch benchmarks (Lower is better)

Benchmark suite Current: 1d579a8 Previous: 00b94d0 Ratio
yew-struct-keyed 01_run1k 154.59699999999998 241.63 0.64
yew-struct-keyed 02_replace1k 163.981 246.188 0.67
yew-struct-keyed 03_update10th1k_x16 249.861 461.602 0.54
yew-struct-keyed 04_select1k 39.646 81.29599999999999 0.49
yew-struct-keyed 05_swap1k 57.77 106.9075 0.54
yew-struct-keyed 06_remove-one-1k 22.75 35.332 0.64
yew-struct-keyed 07_create10k 2640.744 3940.698 0.67
yew-struct-keyed 08_create1k-after1k_x2 348.0135 579.2225 0.60
yew-struct-keyed 09_clear1k_x8 144.3865 229.027 0.63
yew-struct-keyed 21_ready-memory 1.459808349609375 1.459808349609375 1
yew-struct-keyed 22_run-memory 1.6990852355957031 1.659912109375 1.02
yew-struct-keyed 23_update5-memory 1.6686859130859375 1.700092315673828 0.98
yew-struct-keyed 24_run5-memory 1.7175369262695312 1.7315826416015625 0.99
yew-struct-keyed 25_run-clear-memory 1.3325920104980469 1.3325920104980469 1
yew-struct-keyed 31_startup-ci 1729.296 1882.66 0.92
yew-struct-keyed 32_startup-bt 23.675999999999995 41.3 0.57
yew-struct-keyed 33_startup-mainthreadcost 203.70800000000008 260.676 0.78
yew-struct-keyed 34_startup-totalbytes 328.7373046875 328.7412109375 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.