Skip to content

Commit 9743630

Browse files
committed
use split_once instead of split to parse lsn strings
[`str::split`](https://doc.rust-lang.org/std/primitive.str.html#method.split) allocates a vector and generates considerably more instructions when compiled than [`str::split_once`](https://doc.rust-lang.org/std/primitive.str.html#method.split_once). [`u64::from_str_radix(split_lo, 16)`](https://doc.rust-lang.org/std/primitive.u64.html#method.from_str_radix) will error if the `lsn_str` contains more than one `/` so this change should result in the same behavior as the current implementation despite not explicitly checking this.
1 parent 270a29b commit 9743630

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

postgres-types/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Log
22

3+
## Unreleased
4+
5+
### Changed
6+
7+
* `FromStr` implementation for `PgLsn` no longer allocates a `Vec` when splitting an lsn string on it's `/`.
8+
39
## v0.2.6 - 2023-08-19
410

511
### Fixed

postgres-types/src/pg_lsn.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,14 @@ impl FromStr for PgLsn {
3333
type Err = ParseLsnError;
3434

3535
fn from_str(lsn_str: &str) -> Result<Self, Self::Err> {
36-
let split: Vec<&str> = lsn_str.split('/').collect();
37-
if split.len() == 2 {
38-
let (hi, lo) = (
39-
u64::from_str_radix(split[0], 16).map_err(|_| ParseLsnError(()))?,
40-
u64::from_str_radix(split[1], 16).map_err(|_| ParseLsnError(()))?,
41-
);
42-
Ok(PgLsn((hi << 32) | lo))
43-
} else {
44-
Err(ParseLsnError(()))
45-
}
36+
let Some((split_hi, split_lo)) = lsn_str.split_once('/') else {
37+
return Err(ParseLsnError(()));
38+
};
39+
let (hi, lo) = (
40+
u64::from_str_radix(split_hi, 16).map_err(|_| ParseLsnError(()))?,
41+
u64::from_str_radix(split_lo, 16).map_err(|_| ParseLsnError(()))?,
42+
);
43+
Ok(PgLsn((hi << 32) | lo))
4644
}
4745
}
4846

0 commit comments

Comments
 (0)