Skip to content

Commit

Permalink
feat: reduce the amount of heck-related allocations (#4634)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
vrmiguel and lucasfernog authored Jul 11, 2022
1 parent d3e19e3 commit bc370e3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
9 changes: 9 additions & 0 deletions .changes/heck-allocations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"tauri-build": patch
"tauri": patch
"tauri-bundler": patch
"cli.rs": patch
"cli.js": patch
---

Reduce the amount of allocations when converting cases.
12 changes: 5 additions & 7 deletions core/tauri-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#![cfg_attr(doc_cfg, feature(doc_cfg))]

pub use anyhow::Result;
use heck::ToSnakeCase;
use heck::AsShoutySnakeCase;

use tauri_utils::resources::{external_binaries, resource_relpath, ResourcePaths};

use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -68,12 +69,9 @@ fn copy_resources(resources: ResourcePaths<'_>, path: &Path) -> Result<()> {
fn has_feature(feature: &str) -> bool {
// when a feature is enabled, Cargo sets the `CARGO_FEATURE_<name` env var to 1
// https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
std::env::var(format!(
"CARGO_FEATURE_{}",
feature.to_snake_case().to_uppercase()
))
.map(|x| x == "1")
.unwrap_or(false)
std::env::var(format!("CARGO_FEATURE_{}", AsShoutySnakeCase(feature)))
.map(|x| x == "1")
.unwrap_or(false)
}

// creates a cfg alias if `has_feature` is true.
Expand Down
16 changes: 8 additions & 8 deletions core/tauri/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use heck::AsShoutySnakeCase;
use heck::AsSnakeCase;
use heck::ToSnakeCase;

use once_cell::sync::OnceCell;

use std::{path::Path, sync::Mutex};
Expand All @@ -19,12 +22,9 @@ fn has_feature(feature: &str) -> bool {

// when a feature is enabled, Cargo sets the `CARGO_FEATURE_<name` env var to 1
// https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
std::env::var(format!(
"CARGO_FEATURE_{}",
feature.to_snake_case().to_uppercase()
))
.map(|x| x == "1")
.unwrap_or(false)
std::env::var(format!("CARGO_FEATURE_{}", AsShoutySnakeCase(feature)))
.map(|x| x == "1")
.unwrap_or(false)
}

// creates a cfg alias if `has_feature` is true.
Expand Down Expand Up @@ -148,11 +148,11 @@ fn alias_module(module: &str, apis: &[&str], api_all: bool) {
for api in apis {
let has = has_feature(&format!("{}-{}", module, api)) || all;
alias(
&format!("{}_{}", module.to_snake_case(), api.to_snake_case()),
&format!("{}_{}", AsSnakeCase(module), AsSnakeCase(api)),
has,
);
any = any || has;
}

alias(&format!("{}_any", module.to_snake_case()), any);
alias(&format!("{}_any", AsSnakeCase(module)), any);
}
8 changes: 2 additions & 6 deletions tooling/bundler/src/bundle/linux/debian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
use super::super::common;
use crate::Settings;
use anyhow::Context;
use heck::ToKebabCase;
use heck::AsKebabCase;
use image::{self, codecs::png::PngDecoder, ImageDecoder};
use libflate::gzip;
use log::info;
Expand Down Expand Up @@ -170,11 +170,7 @@ fn generate_control_file(
// https://www.debian.org/doc/debian-policy/ch-controlfields.html
let dest_path = control_dir.join("control");
let mut file = common::create_file(&dest_path)?;
writeln!(
file,
"Package: {}",
settings.product_name().to_kebab_case().to_ascii_lowercase()
)?;
writeln!(file, "Package: {}", AsKebabCase(settings.product_name()))?;
writeln!(file, "Version: {}", settings.version_string())?;
writeln!(file, "Architecture: {}", arch)?;
// Installed-Size must be divided by 1024, see https://www.debian.org/doc/debian-policy/ch-controlfields.html#installed-size
Expand Down
4 changes: 2 additions & 2 deletions tooling/cli/src/plugin/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
use anyhow::Context;
use clap::Parser;
use handlebars::{to_json, Handlebars};
use heck::{ToKebabCase, ToSnakeCase};
use heck::{AsKebabCase, ToKebabCase, ToSnakeCase};
use include_dir::{include_dir, Dir};
use log::warn;
use std::{collections::BTreeMap, env::current_dir, fs::remove_dir_all, path::PathBuf};
Expand Down Expand Up @@ -58,7 +58,7 @@ pub fn command(mut options: Options) -> Result<()> {
options.load();
let template_target_path = PathBuf::from(options.directory).join(&format!(
"tauri-plugin-{}",
options.plugin_name.to_kebab_case()
AsKebabCase(&options.plugin_name)
));
let metadata = serde_json::from_str::<VersionMetadata>(include_str!("../../metadata.json"))?;
if template_target_path.exists() {
Expand Down

0 comments on commit bc370e3

Please sign in to comment.