Skip to content

Commit

Permalink
perf: use itoa for integer to string conversion (#7762)
Browse files Browse the repository at this point in the history
  • Loading branch information
shulaoda authored Sep 3, 2024
1 parent 2e1af48 commit cd962e5
Show file tree
Hide file tree
Showing 27 changed files with 112 additions and 65 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ async-scoped = { version = "0.9.0" }
async-trait = { version = "0.1.79" }
bitflags = { version = "2.5.0" }
camino = { version = "1.1.8" }
concat-string = "1.0.1"
css-module-lexer = "0.0.14"
concat-string = { version = "1.0.1" }
css-module-lexer = { version = "0.0.14" }
dashmap = { version = "5.5.3" }
derivative = { version = "2.2.0" }
futures = { version = "0.3.30" }
Expand All @@ -39,7 +39,7 @@ linked_hash_set = { version = "0.1.4" }
mimalloc = { version = "0.1.43" }
mime_guess = { version = "2.0.4" }
once_cell = { version = "1.19.0" }
parcel_sourcemap = "2.1.1"
parcel_sourcemap = { version = "2.1.1" }
paste = { version = "1.0" }
path-clean = { version = "1.0.1" }
pathdiff = { version = "0.2.1" }
Expand Down
7 changes: 4 additions & 3 deletions crates/rspack_binding_values/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use rspack_napi::{
},
Ref,
};
use rspack_util::itoa;
use rustc_hash::FxHashMap as HashMap;

use super::ToJsCompatSource;
Expand Down Expand Up @@ -328,7 +329,7 @@ impl From<(String, rspack_core::LogType)> for JsStatsLogging {
args: Some(vec![format!(
"{}: {} ms",
label,
secs * 1000 + subsec_nanos as u64 / 1000000
itoa!(secs * 1000 + subsec_nanos as u64 / 1000000)
)]),
trace: None,
},
Expand All @@ -355,8 +356,8 @@ impl From<(String, rspack_core::LogType)> for JsStatsLogging {
} else {
hit as f32 / total as f32 * 100_f32
},
hit,
total,
itoa!(hit),
itoa!(total),
)]),
trace: None,
},
Expand Down
12 changes: 8 additions & 4 deletions crates/rspack_core/src/build_chunk_graph/code_splitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use rspack_collections::{
};
use rspack_collections::{IdentifierIndexSet, IdentifierMap};
use rspack_error::{error, Diagnostic, Error, Result};
use rspack_util::itoa;
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet, FxHasher};

use crate::dependencies_block::AsyncDependenciesToInitialChunkError;
Expand Down Expand Up @@ -857,19 +858,22 @@ Or do you want to use the entrypoints '{name}' and '{runtime}' independently on

logger.log(format!(
"{} queue items processed ({} blocks)",
self.stat_processed_queue_items, self.stat_processed_blocks
itoa!(self.stat_processed_queue_items),
itoa!(self.stat_processed_blocks)
));
logger.log(format!(
"{} chunk groups connected",
self.stat_connected_chunk_groups,
itoa!(self.stat_connected_chunk_groups),
));
logger.log(format!(
"{} chunk groups processed for merging ({} module sets)",
self.stat_processed_chunk_groups_for_merging, self.stat_merged_available_module_sets,
itoa!(self.stat_processed_chunk_groups_for_merging),
itoa!(self.stat_merged_available_module_sets),
));
logger.log(format!(
"{} chunk group info updated ({} already connected chunk groups reconnected)",
self.stat_chunk_group_info_updated, self.stat_child_chunk_groups_reconnected,
itoa!(self.stat_chunk_group_info_updated),
itoa!(self.stat_child_chunk_groups_reconnected),
));

Ok(())
Expand Down
3 changes: 2 additions & 1 deletion crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use rspack_futures::FuturesResults;
use rspack_hash::{RspackHash, RspackHashDigest};
use rspack_hook::define_hook;
use rspack_sources::{BoxSource, CachedSource, SourceExt};
use rspack_util::itoa;
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet, FxHasher};
use tracing::instrument;

Expand Down Expand Up @@ -457,7 +458,7 @@ impl Compilation {
let import_var = match import_var_map_of_module.entry(module_id) {
hash_map::Entry::Occupied(occ) => occ.get().clone(),
hash_map::Entry::Vacant(vac) => {
let import_var = format!("{}__WEBPACK_IMPORTED_MODULE_{}__", user_request, len);
let import_var = format!("{}__WEBPACK_IMPORTED_MODULE_{}__", user_request, itoa!(len));
vac.insert(import_var.clone());
import_var
}
Expand Down
8 changes: 4 additions & 4 deletions crates/rspack_core/src/concatenated_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use rspack_error::{Diagnosable, Diagnostic, DiagnosticKind, Result, TraceableErr
use rspack_hash::{HashDigest, HashFunction, RspackHash};
use rspack_hook::define_hook;
use rspack_sources::{CachedSource, ConcatSource, RawSource, ReplaceSource, Source, SourceExt};
use rspack_util::{ext::DynHash, source_map::SourceMapKind, swc::join_atom};
use rspack_util::{ext::DynHash, itoa, source_map::SourceMapKind, swc::join_atom};
use rustc_hash::FxHasher;
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
use swc_core::{
Expand Down Expand Up @@ -508,7 +508,7 @@ impl Module for ConcatenatedModule {
Cow::Owned(format!(
"{} + {} modules",
self.root_module_ctxt.readable_identifier,
self.modules.len() - 1
itoa!(self.modules.len() - 1)
))
}

Expand Down Expand Up @@ -2260,14 +2260,14 @@ pub fn find_new_name(
}

let mut i = 0;
let mut name_with_number = to_identifier(&format!("{}_{}", name, i)).into();
let mut name_with_number = to_identifier(&format!("{}_{}", name, itoa!(i))).into();
while used_names1.contains(&name_with_number)
|| used_names2
.map(|map| map.contains(&name_with_number))
.unwrap_or_default()
{
i += 1;
name_with_number = to_identifier(&format!("{}_{}", name, i)).into();
name_with_number = to_identifier(&format!("{}_{}", name, itoa!(i))).into();
}

name_with_number
Expand Down
3 changes: 2 additions & 1 deletion crates/rspack_core/src/context_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use rspack_macros::impl_source_map_config;
use rspack_paths::{AssertUtf8, Utf8Path, Utf8PathBuf};
use rspack_regex::RspackRegex;
use rspack_sources::{BoxSource, ConcatSource, RawSource, SourceExt};
use rspack_util::itoa;
use rspack_util::{fx_hash::FxIndexMap, json_stringify, source_map::SourceMapKind};
use rustc_hash::FxHashMap as HashMap;
use rustc_hash::FxHashSet as HashSet;
Expand Down Expand Up @@ -497,7 +498,7 @@ impl ContextModule {
format!(
"{}(ids[{}])",
RuntimeGlobals::ENSURE_CHUNK,
chunks_start_position
itoa!(chunks_start_position)
)
};
let return_module_object = self.get_return_module_object_source(
Expand Down
3 changes: 2 additions & 1 deletion crates/rspack_core/src/utils/concatenation_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::sync::LazyLock;

use regex::Regex;
use rspack_collections::IdentifierIndexMap;
use rspack_util::itoa;
use swc_core::atoms::Atom;

use crate::concatenated_module::{ConcatenatedModuleInfo, ModuleInfo};
Expand Down Expand Up @@ -110,7 +111,7 @@ impl ConcatenationScope {

format!(
"__WEBPACK_MODULE_REFERENCE__{}_{}{}{}{}__._",
info.index(),
itoa!(info.index()),
export_data,
call_flag,
direct_import_flag,
Expand Down
9 changes: 5 additions & 4 deletions crates/rspack_ids/src/id_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use rspack_core::{
compare_runtime, BoxModule, Chunk, ChunkGraph, ChunkUkey, Compilation, ModuleGraph,
ModuleIdentifier,
};
use rspack_util::itoa;
use rspack_util::{
comparators::{compare_ids, compare_numbers},
identifier::make_paths_relative,
Expand Down Expand Up @@ -229,10 +230,10 @@ pub fn assign_names_par<T: Copy + Send>(
items.sort_unstable_by(&comparator);
let mut i = 0;
for item in items {
let mut formatted_name = format!("{name}{i}");
let mut formatted_name = format!("{name}{}", itoa!(i));
while name_to_items_keys.contains(&formatted_name) && used_ids.contains(&formatted_name) {
i += 1;
formatted_name = format!("{name}{i}");
formatted_name = format!("{name}{}", itoa!(i));
}
assign_name(item, formatted_name.clone());
used_ids.insert(formatted_name);
Expand Down Expand Up @@ -275,10 +276,10 @@ pub fn assign_deterministic_ids<T: Copy>(
for item in items {
let ident = get_name(item);
let mut i = salt;
let mut id = get_number_hash(&format!("{ident}{i}"), range);
let mut id = get_number_hash(&format!("{ident}{}", itoa!(i)), range);
while !assign_id(item, id) {
i += 1;
id = get_number_hash(&format!("{ident}{i}"), range);
id = get_number_hash(&format!("{ident}{}", itoa!(i)), range);
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion crates/rspack_loader_swc/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use base64::prelude::*;
use dashmap::DashMap;
use jsonc_parser::parse_to_serde_value;
use rspack_ast::javascript::{Ast as JsAst, Context as JsAstContext, Program as JsProgram};
use rspack_util::itoa;
use serde_json::error::Category;
use swc_config::config_types::BoolOr;
use swc_config::merge::Merge;
Expand Down Expand Up @@ -89,7 +90,9 @@ fn parse_swcrc(s: &str) -> Result<Rc, Error> {
};
Error::new(e).context(format!(
"failed to deserialize .swcrc (json) file: {}: {}:{}",
msg, line, column
msg,
itoa!(line),
itoa!(column)
))
}

Expand Down
3 changes: 2 additions & 1 deletion crates/rspack_plugin_css/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use rspack_error::{DiagnosticExt, RspackSeverity};
use rspack_hash::RspackHash;
use rspack_util::identifier::make_paths_relative;
use rspack_util::infallible::ResultInfallibleExt;
use rspack_util::itoa;
use rspack_util::json_stringify;
use rustc_hash::FxHashSet as HashSet;

Expand Down Expand Up @@ -256,7 +257,7 @@ pub fn css_modules_exports_to_concatenate_module_string<'a>(
let mut identifier = to_identifier(key);
let mut i = 0;
while used_identifiers.contains(&identifier) {
identifier = Cow::Owned(format!("{key}{i}"));
identifier = Cow::Owned(format!("{key}{}", itoa!(i)));
i += 1;
}
// TODO: conditional support `const or var` after we finished runtimeTemplate utils
Expand Down
5 changes: 3 additions & 2 deletions crates/rspack_plugin_extract_css/src/css_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use rspack_error::Result;
use rspack_error::{impl_empty_diagnosable_trait, Diagnostic};
use rspack_hash::{RspackHash, RspackHashDigest};
use rspack_util::ext::DynHash;
use rspack_util::itoa;
use rustc_hash::FxHashSet;

use crate::css_dependency::CssDependency;
Expand Down Expand Up @@ -55,7 +56,7 @@ impl CssModule {
let identifier__ = format!(
"css|{}|{}|{}|{}|{}}}",
dep.identifier,
dep.identifier_index,
itoa!(dep.identifier_index),
dep.layer.as_deref().unwrap_or_default(),
dep.supports.as_deref().unwrap_or_default(),
dep.media.as_deref().unwrap_or_default(),
Expand Down Expand Up @@ -110,7 +111,7 @@ impl Module for CssModule {
"css {}{}{}{}{}",
context.shorten(&self.identifier),
if self.identifier_index > 0 {
format!("({})", self.identifier_index)
format!("({})", itoa!(self.identifier_index))
} else {
"".into()
},
Expand Down
17 changes: 13 additions & 4 deletions crates/rspack_plugin_javascript/src/parser_and_generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use rspack_core::{
};
use rspack_error::miette::Diagnostic;
use rspack_error::{DiagnosticExt, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_util::itoa;
use swc_core::common::comments::Comments;
use swc_core::common::input::SourceFileInput;
use swc_core::common::{FileName, Span, SyntaxContext};
Expand Down Expand Up @@ -370,6 +371,7 @@ impl ParserAndGenerator for JavaScriptParserAndGenerator {
}
}

// Todo(shulaoda): check if this can be removed
fn span_to_location(span: Span, source: &str) -> Option<String> {
let r = ropey::Rope::from_str(source);
let start = span.real_lo();
Expand All @@ -382,12 +384,19 @@ fn span_to_location(span: Span, source: &str) -> Option<String> {
let end_line = r.char_to_line(end_char_offset);
let end_column = end_char_offset - r.line_to_char(end_line);
if start_line == end_line {
Some(format!("{}:{start_column}-{end_column}", start_line + 1))
Some(format!(
"{}:{}-{}",
itoa!(start_line + 1),
itoa!(start_column),
itoa!(end_column)
))
} else {
Some(format!(
"{}:{start_column}-{}:{end_column}",
start_line + 1,
end_line + 1
"{}:{}-{}:{}",
itoa!(start_line + 1),
itoa!(start_column),
itoa!(end_line + 1),
itoa!(end_column)
))
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rspack_core::{
ConstDependency, ContextDependency, RealDependencyLocation, RuntimeGlobals, SpanExt,
};
use rspack_util::itoa;
use swc_core::{common::Spanned, ecma::ast::CallExpr};

use super::JavascriptParserPlugin;
Expand Down Expand Up @@ -107,7 +108,7 @@ impl JavascriptParserPlugin for CompatibilityPlugin {
self.tag_nested_require_data(
parser,
ident.sym.to_string(),
format!("__nested_webpack_require_{start}_{end}__"),
format!("__nested_webpack_require_{}_{}__", itoa!(start), itoa!(end),),
start,
end,
);
Expand Down Expand Up @@ -137,7 +138,7 @@ impl JavascriptParserPlugin for CompatibilityPlugin {
self.tag_nested_require_data(
parser,
ident.sym.to_string(),
format!("__nested_webpack_require_{start}_{end}__"),
format!("__nested_webpack_require_{}_{}__", itoa!(start), itoa!(end),),
start,
end,
);
Expand All @@ -156,7 +157,10 @@ impl JavascriptParserPlugin for CompatibilityPlugin {
self.tag_nested_require_data(
parser,
name.to_string(),
format!("__nested_webpack_require_{}__", fn_decl.span().real_lo()),
format!(
"__nested_webpack_require_{}__",
itoa!(fn_decl.span().real_lo())
),
ident.span().real_lo(),
ident.span().real_hi(),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use rspack_error::{
DiagnosticExt, Result,
};
use rspack_hook::{plugin, plugin_hook};
use rspack_util::itoa;
use serde_json::Value;

use crate::parser_and_generator::JavaScriptParserAndGenerator;
Expand Down Expand Up @@ -72,7 +73,7 @@ async fn compilation(
)
} else if let Some(value) = value.as_array() {
let indexes = (0..value.len())
.map(|index| format!("{}", index))
.map(|index| itoa!(index).to_string())
.collect_vec();
let iter = indexes.iter().zip(value.iter());
walk_definitions(iter, compilation, Cow::Owned(format!("{prefix}{key}.")))
Expand Down
Loading

2 comments on commit cd962e5

@rspack-bot
Copy link

Choose a reason for hiding this comment

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

📝 Benchmark detail: Open

Name Base (2024-09-03 f9820e4) Current Change
10000_development-mode + exec 2.24 s ± 29 ms 2.23 s ± 35 ms -0.69 %
10000_development-mode_hmr + exec 694 ms ± 7.5 ms 685 ms ± 7.8 ms -1.30 %
10000_production-mode + exec 2.85 s ± 30 ms 2.84 s ± 26 ms -0.15 %
arco-pro_development-mode + exec 1.86 s ± 70 ms 1.83 s ± 86 ms -1.27 %
arco-pro_development-mode_hmr + exec 434 ms ± 2.9 ms 435 ms ± 2.7 ms +0.29 %
arco-pro_production-mode + exec 3.3 s ± 96 ms 3.29 s ± 64 ms -0.19 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.32 s ± 90 ms 3.33 s ± 74 ms +0.17 %
threejs_development-mode_10x + exec 1.65 s ± 11 ms 1.65 s ± 11 ms +0.04 %
threejs_development-mode_10x_hmr + exec 805 ms ± 10 ms 799 ms ± 8.1 ms -0.82 %
threejs_production-mode_10x + exec 5.16 s ± 30 ms 5.19 s ± 34 ms +0.57 %

@rspack-bot
Copy link

Choose a reason for hiding this comment

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

📝 Ran ecosystem CI: Open

suite result
modernjs ✅ success
_selftest ✅ success
nx ❌ failure
rspress ✅ success
rslib ✅ success
rsbuild ✅ success
examples ✅ success

Please sign in to comment.