Skip to content

Commit 63c5660

Browse files
authored
perf: Update TOML (#669)
For more context on the performance improvements, see https://epage.github.io/blog/2025/07/toml-09/ I held off on switching to `DeTable` (avoiding serde overhead) because `toml::de::DeInteger` doesn't expose enough of an API
2 parents 7213a4c + f15ef49 commit 63c5660

File tree

4 files changed

+53
-55
lines changed

4 files changed

+53
-55
lines changed

Cargo.lock

Lines changed: 30 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,13 @@ toml = ["dep:toml"]
135135
serde = "1.0"
136136

137137
async-trait = { version = "0.1", optional = true }
138-
toml = { version = "0.8", optional = true, default-features = false, features = ["parse"] }
138+
toml = { version = "0.9", optional = true, default-features = false, features = ["parse", "serde"] }
139139
serde_json = { version = "1.0", optional = true }
140140
yaml-rust2 = { version = "0.10", optional = true }
141141
rust-ini = { version = "0.21", optional = true }
142142
ron = { version = "0.8", optional = true }
143143
json5_rs = { version = "0.4", optional = true, package = "json5" }
144-
indexmap = { version = "2.2", features = ["serde"], optional = true }
144+
indexmap = { version = "2.10.0", features = ["serde"], optional = true }
145145
convert_case = { version = "0.6", optional = true }
146146
pathdiff = "0.2"
147147
winnow = "0.7.0"

src/file/format/toml.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::error::Error;
22

3-
use crate::format;
43
use crate::map::Map;
54
use crate::value::Value;
65

@@ -9,28 +8,33 @@ pub(crate) fn parse(
98
text: &str,
109
) -> Result<Map<String, Value>, Box<dyn Error + Send + Sync>> {
1110
// Parse a TOML value from the provided text
12-
let value = from_toml_value(uri, &toml::from_str(text)?);
13-
format::extract_root_table(uri, value)
11+
let table = from_toml_table(uri, toml::from_str(text)?);
12+
Ok(table)
1413
}
1514

16-
fn from_toml_value(uri: Option<&String>, value: &toml::Value) -> Value {
17-
match *value {
18-
toml::Value::String(ref value) => Value::new(uri, value.clone()),
15+
fn from_toml_table(uri: Option<&String>, table: toml::Table) -> Map<String, Value> {
16+
let mut m = Map::new();
17+
18+
for (key, value) in table {
19+
m.insert(key, from_toml_value(uri, value));
20+
}
21+
22+
m
23+
}
24+
25+
fn from_toml_value(uri: Option<&String>, value: toml::Value) -> Value {
26+
match value {
27+
toml::Value::String(value) => Value::new(uri, value),
1928
toml::Value::Float(value) => Value::new(uri, value),
2029
toml::Value::Integer(value) => Value::new(uri, value),
2130
toml::Value::Boolean(value) => Value::new(uri, value),
2231

23-
toml::Value::Table(ref table) => {
24-
let mut m = Map::new();
25-
26-
for (key, value) in table {
27-
m.insert(key.clone(), from_toml_value(uri, value));
28-
}
29-
32+
toml::Value::Table(table) => {
33+
let m = from_toml_table(uri, table);
3034
Value::new(uri, m)
3135
}
3236

33-
toml::Value::Array(ref array) => {
37+
toml::Value::Array(array) => {
3438
let mut l = Vec::new();
3539

3640
for value in array {
@@ -40,6 +44,6 @@ fn from_toml_value(uri: Option<&String>, value: &toml::Value) -> Value {
4044
Value::new(uri, l)
4145
}
4246

43-
toml::Value::Datetime(ref datetime) => Value::new(uri, datetime.to_string()),
47+
toml::Value::Datetime(datetime) => Value::new(uri, datetime.to_string()),
4448
}
4549
}

tests/testsuite/file_toml.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,8 @@ error = tru
161161
TOML parse error at line 3, column 9
162162
|
163163
3 | error = tru
164-
| ^
165-
invalid string
166-
expected `"`, `'`
164+
| ^^^
165+
invalid boolean, expected `true`
167166
168167
"#]]
169168
);

0 commit comments

Comments
 (0)