language: Clean up allocations (#36418)

- Correctly pre-allocate `Vec` when deserializing regexes
- Simplify manual `Vec::with_capacity` calls by using `Iterator::unzip`
- Collect directly into `Arc<[T]>` (uses `Vec` internally anyway, but
simplifies code)
- Remove unnecessary `LazyLock` around Atomics by not using const
incompatible `Default` for initialization.

Release Notes:

- N/A
This commit is contained in:
tidely 2025-08-18 18:58:12 +03:00 committed by GitHub
parent 768b2de368
commit e1d8e3bf6d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 23 additions and 29 deletions

View file

@ -121,8 +121,8 @@ where
func(cursor.deref_mut()) func(cursor.deref_mut())
} }
static NEXT_LANGUAGE_ID: LazyLock<AtomicUsize> = LazyLock::new(Default::default); static NEXT_LANGUAGE_ID: AtomicUsize = AtomicUsize::new(0);
static NEXT_GRAMMAR_ID: LazyLock<AtomicUsize> = LazyLock::new(Default::default); static NEXT_GRAMMAR_ID: AtomicUsize = AtomicUsize::new(0);
static WASM_ENGINE: LazyLock<wasmtime::Engine> = LazyLock::new(|| { static WASM_ENGINE: LazyLock<wasmtime::Engine> = LazyLock::new(|| {
wasmtime::Engine::new(&wasmtime::Config::new()).expect("Failed to create Wasmtime engine") wasmtime::Engine::new(&wasmtime::Config::new()).expect("Failed to create Wasmtime engine")
}); });
@ -964,11 +964,11 @@ where
fn deserialize_regex_vec<'de, D: Deserializer<'de>>(d: D) -> Result<Vec<Regex>, D::Error> { fn deserialize_regex_vec<'de, D: Deserializer<'de>>(d: D) -> Result<Vec<Regex>, D::Error> {
let sources = Vec::<String>::deserialize(d)?; let sources = Vec::<String>::deserialize(d)?;
let mut regexes = Vec::new(); sources
for source in sources { .into_iter()
regexes.push(regex::Regex::new(&source).map_err(de::Error::custom)?); .map(|source| regex::Regex::new(&source))
} .collect::<Result<_, _>>()
Ok(regexes) .map_err(de::Error::custom)
} }
fn regex_vec_json_schema(_: &mut SchemaGenerator) -> schemars::Schema { fn regex_vec_json_schema(_: &mut SchemaGenerator) -> schemars::Schema {
@ -1034,12 +1034,10 @@ impl<'de> Deserialize<'de> for BracketPairConfig {
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
let result = Vec::<BracketPairContent>::deserialize(deserializer)?; let result = Vec::<BracketPairContent>::deserialize(deserializer)?;
let mut brackets = Vec::with_capacity(result.len()); let (brackets, disabled_scopes_by_bracket_ix) = result
let mut disabled_scopes_by_bracket_ix = Vec::with_capacity(result.len()); .into_iter()
for entry in result { .map(|entry| (entry.bracket_pair, entry.not_in))
brackets.push(entry.bracket_pair); .unzip();
disabled_scopes_by_bracket_ix.push(entry.not_in);
}
Ok(BracketPairConfig { Ok(BracketPairConfig {
pairs: brackets, pairs: brackets,
@ -1379,16 +1377,14 @@ impl Language {
let grammar = self.grammar_mut().context("cannot mutate grammar")?; let grammar = self.grammar_mut().context("cannot mutate grammar")?;
let query = Query::new(&grammar.ts_language, source)?; let query = Query::new(&grammar.ts_language, source)?;
let mut extra_captures = Vec::with_capacity(query.capture_names().len()); let extra_captures: Vec<_> = query
.capture_names()
for name in query.capture_names().iter() { .iter()
let kind = if *name == "run" { .map(|&name| match name {
RunnableCapture::Run "run" => RunnableCapture::Run,
} else { name => RunnableCapture::Named(name.to_string().into()),
RunnableCapture::Named(name.to_string().into()) })
}; .collect();
extra_captures.push(kind);
}
grammar.runnable_config = Some(RunnableConfig { grammar.runnable_config = Some(RunnableConfig {
extra_captures, extra_captures,

View file

@ -385,12 +385,10 @@ pub fn deserialize_undo_map_entry(
/// Deserializes selections from the RPC representation. /// Deserializes selections from the RPC representation.
pub fn deserialize_selections(selections: Vec<proto::Selection>) -> Arc<[Selection<Anchor>]> { pub fn deserialize_selections(selections: Vec<proto::Selection>) -> Arc<[Selection<Anchor>]> {
Arc::from(
selections selections
.into_iter() .into_iter()
.filter_map(deserialize_selection) .filter_map(deserialize_selection)
.collect::<Vec<_>>(), .collect()
)
} }
/// Deserializes a [`Selection`] from the RPC representation. /// Deserializes a [`Selection`] from the RPC representation.