Skip to content

Commit

Permalink
replace dependencies with long build times when used together (closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
chippers committed Mar 27, 2022
1 parent dbc2873 commit 8661e3e
Show file tree
Hide file tree
Showing 16 changed files with 475 additions and 196 deletions.
16 changes: 16 additions & 0 deletions .changes/no-long-c-build-deps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
"tauri": patch
"tauri-codegen": patch
"tauri-macros": patch
"tauri-utils": patch
---

Replace multiple dependencies who's C code compiled concurrently and caused
the other ones to bloat compile time significantly.

* `zstd` -> `brotli`
* `blake3` -> a vendored version of the blake3 reference
* `ring` -> `getrandom`

See https://github.com/tauri-apps/tauri/pull/3773 for more information about
these specific choices.
6 changes: 2 additions & 4 deletions core/tauri-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ readme = "README.md"
[dependencies]
sha2 = "0.10"
base64 = "0.13"
blake3 = { version = "1.3", features = [ "rayon" ] }
proc-macro2 = "1"
quote = "1"
serde = { version = "1", features = [ "derive" ] }
serde_json = "1"
tauri-utils = { version = "1.0.0-rc.3", path = "../tauri-utils", features = [ "build" ] }
thiserror = "1"
walkdir = "2"
zstd = { version = "0.11", optional = true }
brotli = { version = "3", optional = true, default-features = false, features = ["std"] }
regex = { version = "1.5.5", optional = true }
uuid = { version = "0.8", features = [ "v4" ] }

Expand All @@ -35,8 +34,7 @@ png = "0.17"

[features]
default = [ "compression" ]
compression = [ "zstd", "tauri-utils/compression" ]
compression = [ "brotli", "tauri-utils/compression" ]
isolation = [ "tauri-utils/isolation" ]
__isolation-docs = [ "tauri-utils/__isolation-docs" ]
shell-scope = [ "regex" ]
config-json5 = [ "tauri-utils/config-json5" ]
8 changes: 4 additions & 4 deletions core/tauri-codegen/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn load_csp(document: &mut NodeRef, key: &AssetKey, csp_hashes: &mut CspHashes)
fn map_core_assets(
options: &AssetOptions,
) -> impl Fn(&AssetKey, &Path, &mut Vec<u8>, &mut CspHashes) -> Result<(), EmbeddedAssetsError> {
#[cfg(any(feature = "isolation", feature = "__isolation-docs"))]
#[cfg(feature = "isolation")]
let pattern = tauri_utils::html::PatternObject::from(&options.pattern);
let csp = options.csp;
move |key, path, input, csp_hashes| {
Expand All @@ -60,7 +60,7 @@ fn map_core_assets(
if csp {
load_csp(&mut document, key, csp_hashes);

#[cfg(any(feature = "isolation", feature = "__isolation-docs"))]
#[cfg(feature = "isolation")]
if let tauri_utils::html::PatternObject::Isolation { .. } = &pattern {
// create the csp for the isolation iframe styling now, to make the runtime less complex
let mut hasher = Sha256::new();
Expand All @@ -78,7 +78,7 @@ fn map_core_assets(
}
}

#[cfg(any(feature = "isolation", feature = "__isolation-docs"))]
#[cfg(feature = "isolation")]
fn map_isolation(
_options: &AssetOptions,
dir: PathBuf,
Expand Down Expand Up @@ -284,7 +284,7 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE

let pattern = match &options.pattern {
PatternKind::Brownfield => quote!(#root::Pattern::Brownfield(std::marker::PhantomData)),
#[cfg(any(feature = "isolation", feature = "__isolation-docs"))]
#[cfg(feature = "isolation")]
PatternKind::Isolation { dir } => {
let dir = config_parent.join(dir);
if !dir.exists() {
Expand Down
63 changes: 40 additions & 23 deletions core/tauri-codegen/src/embedded_assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use quote::{quote, ToTokens, TokenStreamExt};
use sha2::{Digest, Sha256};
use std::{
collections::HashMap,
fmt::Write,
fs::File,
path::{Path, PathBuf},
};
Expand All @@ -15,12 +16,12 @@ use tauri_utils::config::PatternKind;
use thiserror::Error;
use walkdir::{DirEntry, WalkDir};

#[cfg(feature = "compression")]
use brotli::enc::backward_references::BrotliEncoderParams;

/// The subdirectory inside the target directory we want to place assets.
const TARGET_PATH: &str = "tauri-codegen-assets";

/// The minimum size needed for the hasher to use multiple threads.
const MULTI_HASH_SIZE_LIMIT: usize = 131_072; // 128KiB

/// (key, (original filepath, compressed bytes))
type Asset = (AssetKey, (PathBuf, PathBuf));

Expand All @@ -40,6 +41,9 @@ pub enum EmbeddedAssetsError {
error: std::io::Error,
},

#[error("failed to create hex from bytes because {0}")]
Hex(std::fmt::Error),

#[error("invalid prefix {prefix} used while including path {path}")]
PrefixInvalid { prefix: PathBuf, path: PathBuf },

Expand Down Expand Up @@ -182,7 +186,7 @@ pub struct AssetOptions {
pub(crate) csp: bool,
pub(crate) pattern: PatternKind,
pub(crate) freeze_prototype: bool,
#[cfg(any(feature = "isolation", feature = "__isolation-docs"))]
#[cfg(feature = "isolation")]
pub(crate) isolation_schema: String,
}

Expand All @@ -193,7 +197,7 @@ impl AssetOptions {
csp: false,
pattern,
freeze_prototype: false,
#[cfg(any(feature = "isolation", feature = "__isolation-docs"))]
#[cfg(feature = "isolation")]
isolation_schema: format!("isolation-{}", uuid::Uuid::new_v4()),
}
}
Expand Down Expand Up @@ -246,13 +250,19 @@ impl EmbeddedAssets {

/// Use highest compression level for release, the fastest one for everything else
#[cfg(feature = "compression")]
fn compression_level() -> i32 {
let levels = zstd::compression_level_range();
fn compression_settings() -> BrotliEncoderParams {
let mut settings = BrotliEncoderParams::default();

// the following compression levels are hand-picked and are not min-maxed.
// they have a good balance of runtime vs size for the respective profile goals.
// see the "brotli" section of this comment https://github.com/tauri-apps/tauri/issues/3571#issuecomment-1054847558
if cfg!(debug_assertions) {
*levels.start()
settings.quality = 2
} else {
*levels.end()
settings.quality = 9
}

settings
}

/// Compress a file and spit out the information in a [`HashMap`] friendly form.
Expand Down Expand Up @@ -291,20 +301,24 @@ impl EmbeddedAssets {

// get a hash of the input - allows for caching existing files
let hash = {
let mut hasher = blake3::Hasher::new();
if input.len() < MULTI_HASH_SIZE_LIMIT {
hasher.update(&input);
} else {
hasher.update_rayon(&input);
let mut hasher = crate::vendor::blake3_reference::Hasher::default();
hasher.update(&input);

let mut bytes = [0u8; 32];
hasher.finalize(&mut bytes);

let mut hex = String::with_capacity(2 * bytes.len());
for b in bytes {
write!(hex, "{:02x}", b).map_err(EmbeddedAssetsError::Hex)?;
}
hasher.finalize().to_hex()
hex
};

// use the content hash to determine filename, keep extensions that exist
let out_path = if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
out_dir.join(format!("{}.{}", hash, ext))
} else {
out_dir.join(hash.to_string())
out_dir.join(hash)
};

// only compress and write to the file if it doesn't already exist.
Expand All @@ -328,13 +342,16 @@ impl EmbeddedAssets {
}

#[cfg(feature = "compression")]
// entirely write input to the output file path with compression
zstd::stream::copy_encode(&*input, out_file, Self::compression_level()).map_err(|error| {
EmbeddedAssetsError::AssetWrite {
path: path.to_owned(),
error,
}
})?;
{
let mut input = std::io::Cursor::new(input);
// entirely write input to the output file path with compression
brotli::BrotliCompress(&mut input, &mut out_file, &Self::compression_settings()).map_err(
|error| EmbeddedAssetsError::AssetWrite {
path: path.to_owned(),
error,
},
)?;
}
}

Ok((key, (path.into(), out_path)))
Expand Down
2 changes: 2 additions & 0 deletions core/tauri-codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub use tauri_utils::config::{parse::ConfigError, Config};

mod context;
pub mod embedded_assets;
#[doc(hidden)]
pub mod vendor;

/// Represents all the errors that can happen while reading the config during codegen.
#[derive(Debug, thiserror::Error)]
Expand Down
Loading

0 comments on commit 8661e3e

Please sign in to comment.