Skip to content

Commit

Permalink
refactor: improve integer performance
Browse files Browse the repository at this point in the history
  • Loading branch information
tyrchen committed May 25, 2024
1 parent 1f71e5b commit 2464fe2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
13 changes: 12 additions & 1 deletion benches/resp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@ use bytes::BytesMut;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use simple_redis::{parse_frame, parse_frame_length, RespFrame};

const DATA: &str = "+OK\r\n-ERR\r\n:1000\r\n$6\r\nfoobar\r\n$-1\r\n*2\r\n+hello\r\n$5\r\nworld\r\n+foo\r\n$3\r\nbar\r\n%2\r\n+foo\r\n,-123456.789\r\n+hello\r\n$5\r\nworld\r\n*3\r\n$3\r\nset\r\n$5\r\nhello\r\n$5\r\nworld\r\n%2\r\n+hello\r\n$5\r\nworld\r\n+foo\r\n$3\r\nbar\r\n";
// resp frames covers all kinds of real-world redis requests and responses
// cmd 1: set key value
// cmd 1 response: OK
// cmd 2: get key
// cmd 2 response: value
// cmd 3: hset key field value
// cmd 3 response: ERR
// cmd 4: hget key field
// cmd 4 response: value
// cmd 5: sadd key member
// cmd 5 response: 1
const DATA: &str = "*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n*1\r\n+OK\r\n*2\r\n$3\r\nGET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n*4\r\n$4\r\nHSET\r\n$3\r\nkey\r\n$5\r\nfield\r\n$5\r\nvalue\r\n*1\r\n-ERR\r\n*3\r\n$4\r\nHGET\r\n$3\r\nkey\r\n$5\r\nfield\r\n$5\r\nvalue\r\n*3\r\n$4\r\nSADD\r\n$3\r\nkey\r\n$6\r\nmember\r\n:1\r\n";

fn v1_decode(buf: &mut BytesMut) -> Result<Vec<RespFrame>> {
use simple_redis::RespDecode;
Expand Down
7 changes: 3 additions & 4 deletions src/respv2/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,11 @@ fn error(input: &mut &[u8]) -> PResult<SimpleError> {
parse_string.map(SimpleError).parse_next(input)
}

// - integer: ":1234\r\n"
// - integer: ":-1234\r\n", need to take care of the sign
fn integer(input: &mut &[u8]) -> PResult<i64> {
let sign = opt(alt(('+', '-'))).parse_next(input)?.unwrap_or('+');
let sign: i64 = if sign == '+' { 1 } else { -1 };
let sign = opt('-').parse_next(input)?.is_some();
let v: i64 = terminated(digit1.parse_to(), CRLF).parse_next(input)?;
Ok(sign * v)
Ok(if sign { -v } else { v })
}

// - null bulk string: "$-1\r\n"
Expand Down

0 comments on commit 2464fe2

Please sign in to comment.