Skip to content

Commit 4d4091c

Browse files
authored
Merge pull request #7 from nephi-dev/speed-increase
Speed increase
2 parents 4d9d478 + d4434b2 commit 4d4091c

File tree

6 files changed

+97
-46
lines changed

6 files changed

+97
-46
lines changed

Cargo.lock

Lines changed: 53 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rxml"
3-
version = "2.3.0"
3+
version = "2.3.1"
44
edition = "2021"
55

66
[lib]
@@ -10,3 +10,4 @@ crate-type = ["cdylib"]
1010
[dependencies]
1111
pyo3 = "0.23.5"
1212
quick-xml = "0.37.2"
13+
rayon = "1.10"

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ classifiers = [
2222
"Typing :: Typed",
2323
"License :: OSI Approved :: MIT License",
2424
]
25-
version = "2.3.0"
25+
version = "2.3.1"
2626

2727

2828
[tool.maturin]

src/entities.rs

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![allow(clippy::only_used_in_recursion)]
22
use crate::f_str;
33
use pyo3::{prelude::*, types::PyType};
4+
use rayon::prelude::*;
45
use std::collections::HashMap;
56

67
#[derive(Clone, FromPyObject, IntoPyObject, Eq, PartialEq, Debug)]
@@ -154,28 +155,24 @@ impl Node {
154155
Some(HashmapTypes::String(n)) => n,
155156
_ => return Err(pyo3::exceptions::PyValueError::new_err("Invalid name")),
156157
}
157-
.to_owned();
158-
let temp_attrs = match dict_.get("attrs") {
158+
.clone();
159+
let attrs = match dict_.get("attrs") {
159160
Some(HashmapTypes::Map(a)) => a,
160161
_ => return Err(pyo3::exceptions::PyValueError::new_err("Invalid attrs")),
161-
};
162-
let temp_children = match dict_.get("children") {
162+
}
163+
.clone();
164+
let children = match dict_.get("children") {
163165
Some(HashmapTypes::Vec(c)) => c,
164166
_ => return Err(pyo3::exceptions::PyValueError::new_err("Invalid children")),
165-
};
167+
}
168+
.iter()
169+
.map(|child| Node::from_dict(cls, child.clone()))
170+
.collect::<PyResult<Vec<Node>>>()?;
166171
let text = match dict_.get("text") {
167-
Some(HashmapTypes::NullableString(t)) => t.to_owned(),
168-
Some(HashmapTypes::String(t)) => Some(t.to_owned()),
172+
Some(HashmapTypes::NullableString(t)) => t.clone(),
173+
Some(HashmapTypes::String(t)) => Some(t.clone()),
169174
_ => None,
170175
};
171-
let mut attrs = HashMap::new();
172-
for (k, v) in temp_attrs {
173-
attrs.insert(k.to_owned(), v.to_owned());
174-
}
175-
let mut children = Vec::new();
176-
for child in temp_children {
177-
children.push(Node::from_dict(cls, child.clone())?);
178-
}
179176
Ok(Self {
180177
name,
181178
attrs,
@@ -185,18 +182,23 @@ impl Node {
185182
}
186183

187184
pub fn to_dict(&self) -> HashMap<String, HashmapTypes> {
188-
let mut map = HashMap::new();
189-
map.insert(f_str!("name"), HashmapTypes::String(self.name.clone()));
190-
map.insert(f_str!("attrs"), HashmapTypes::Map(self.attrs.clone()));
191-
map.insert(
192-
f_str!("children"),
193-
HashmapTypes::Vec(self.children.iter().map(|child| child.to_dict()).collect()),
194-
);
195-
map.insert(
196-
f_str!("text"),
197-
HashmapTypes::NullableString(self.text.clone()),
198-
);
199-
map
185+
HashMap::from([
186+
(f_str!("name"), HashmapTypes::String(self.name.clone())),
187+
(f_str!("attrs"), HashmapTypes::Map(self.attrs.clone())),
188+
(
189+
f_str!("children"),
190+
HashmapTypes::Vec(
191+
self.children
192+
.par_iter()
193+
.map(|child| child.to_dict())
194+
.collect(),
195+
),
196+
),
197+
(
198+
f_str!("text"),
199+
HashmapTypes::NullableString(self.text.clone()),
200+
),
201+
])
200202
}
201203
}
202204

@@ -234,7 +236,7 @@ mod tests {
234236
.unwrap();
235237
let second_child_node = Node::new(
236238
f_str!("test new"),
237-
Some(attrs.clone()),
239+
Some(attrs),
238240
Some(Vec::new()),
239241
Some(f_str!("test")),
240242
)

src/read.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ fn get_attrs(attrs: Attributes) -> HashMap<String, String> {
2222
fn read_node(root_tag: String, reader: &mut Reader<&[u8]>) -> Node {
2323
let mut buf = Vec::new();
2424
let mut root = Node {
25-
name: root_tag.clone(),
25+
name: root_tag,
2626
attrs: HashMap::new(),
2727
children: Vec::new(),
2828
text: None,
2929
};
3030
loop {
3131
match reader.read_event_into(&mut buf) {
3232
Ok(Event::Start(e)) => match e.name().as_ref() {
33-
_e if _e == root_tag.as_bytes() => {
33+
_e if _e == root.name.as_bytes() => {
3434
root.attrs = get_attrs(e.attributes());
3535
}
3636
_ => {
@@ -52,7 +52,7 @@ fn read_node(root_tag: String, reader: &mut Reader<&[u8]>) -> Node {
5252
Ok(Event::Text(e)) => {
5353
root.text = Some(f_str!(e.unescape().unwrap()));
5454
}
55-
Ok(Event::End(e)) if e.name().as_ref() == root_tag.as_bytes() => {
55+
Ok(Event::End(e)) if e.name().as_ref() == root.name.as_bytes() => {
5656
break;
5757
}
5858
Ok(Event::Eof) => break,

src/write.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,29 +64,30 @@ pub fn write_string(node: Node, indent: Option<usize>, default_xml_def: Option<b
6464
#[cfg(test)]
6565
mod tests {
6666
use crate::entities::Node;
67+
use crate::f_str;
6768
use crate::write::{write_file, write_node_to_string, write_string};
6869
use std::collections::HashMap;
6970
use std::fs::{read_to_string, remove_file};
7071
fn root_node() -> Node {
7172
let mut attrs = HashMap::new();
72-
attrs.insert("test".to_string(), "test".to_string());
73+
attrs.insert(f_str!("test"), f_str!("test"));
7374
let mut root = Node {
74-
name: "root".to_string(),
75+
name: f_str!("root"),
7576
attrs: attrs.clone(),
7677
children: Vec::new(),
7778
text: None,
7879
};
7980
let mut child = Node {
80-
name: "child".to_string(),
81-
attrs: attrs.clone(),
81+
name: f_str!("child"),
82+
attrs,
8283
children: Vec::new(),
8384
text: None,
8485
};
8586
child.children.push(Node {
86-
name: "child".to_string(),
87+
name: f_str!("child"),
8788
attrs: HashMap::new(),
8889
children: Vec::new(),
89-
text: Some("test".to_string()),
90+
text: Some(f_str!("test")),
9091
});
9192
root.children.push(child);
9293
root
@@ -112,12 +113,7 @@ mod tests {
112113
fn test_write_file() {
113114
let root = root_node();
114115
let expected = expected_file();
115-
write_file(
116-
root,
117-
"tests/test_write.xml".to_string(),
118-
Some(4),
119-
Some(true),
120-
);
116+
write_file(root, f_str!("tests/test_write.xml"), Some(4), Some(true));
121117
let file_str = read_to_string("tests/test_write.xml").unwrap();
122118
remove_file("tests/test_write.xml").unwrap();
123119
assert_eq!(file_str, expected);

0 commit comments

Comments
 (0)