Skip to content

Commit b7d2fd4

Browse files
committed
perf: use str::make_ascii_lowercase instead of str::to_lowercase
`str::to_lowercase` had been used for the purposes of case-insensitive comparisons of command names, but `str::make_ascii_lowercase` is substantially more performant. Using: $ redis-benchmark -c 1 -r 128 -t set -n 100000 ...with `str::to_lowercase`: throughput summary: 6651.59 requests per second latency summary (msec): avg min p50 p95 p99 max 0.136 0.072 0.135 0.223 0.287 2.807 ...with `str::make_ascii_lowercase`: throughput summary: 9102.49 requests per second latency summary (msec): avg min p50 p95 p99 max 0.099 0.064 0.095 0.127 0.191 0.815 While micro-optimizations are usually not appropriate for mini-redis, this pattern of case-insensitive string comparisons is common, and using `str::to_lowercase` sets a bad example.
1 parent a7a9bf5 commit b7d2fd4

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/cmd/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ impl Command {
5353

5454
// All redis commands begin with the command name as a string. The name
5555
// is read and converted to lower cases in order to do case sensitive
56-
// matching.
57-
let command_name = parse.next_string()?.to_lowercase();
56+
// matching. Doing this in-place with `str::make_ascii_lowercase` is
57+
// substantially more performant than `str::to_lowercase`.
58+
let mut command_name = parse.next_string()?;
59+
command_name.make_ascii_lowercase();
5860

5961
// Match the command name, delegating the rest of the parsing to the
6062
// specific command.

0 commit comments

Comments
 (0)