Skip to content

Commit abc30ff

Browse files
authored
Move args from Vec<String> to Vec<RedisString> to reduce allocations (#157)
* change args to Vec<RedisString> * add utf error text on RediString to str
1 parent 6d0986a commit abc30ff

File tree

19 files changed

+195
-237
lines changed

19 files changed

+195
-237
lines changed

examples/block.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#[macro_use]
22
extern crate redis_module;
33

4-
use redis_module::{Context, RedisResult, RedisValue, ThreadSafeContext};
4+
use redis_module::{Context, RedisResult, RedisString, RedisValue, ThreadSafeContext};
55
use std::thread;
66
use std::time::Duration;
77

8-
fn block(ctx: &Context, _args: Vec<String>) -> RedisResult {
8+
fn block(ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
99
let blocked_client = ctx.block_client();
1010

1111
thread::spawn(move || {

examples/data_type.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
extern crate redis_module;
33

44
use redis_module::native_types::RedisType;
5-
use redis_module::{raw, Context, NextArg, RedisResult};
5+
use redis_module::{raw, Context, NextArg, RedisResult, RedisString};
66
use std::os::raw::c_void;
77

88
#[derive(Debug)]
@@ -34,19 +34,15 @@ static MY_REDIS_TYPE: RedisType = RedisType::new(
3434
copy: None,
3535
defrag: None,
3636
},
37-
38-
39-
40-
4137
);
4238

4339
unsafe extern "C" fn free(value: *mut c_void) {
4440
Box::from_raw(value as *mut MyType);
4541
}
4642

47-
fn alloc_set(ctx: &Context, args: Vec<String>) -> RedisResult {
43+
fn alloc_set(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
4844
let mut args = args.into_iter().skip(1);
49-
let key = args.next_string()?;
45+
let key = args.next_arg()?;
5046
let size = args.next_i64()?;
5147

5248
ctx.log_debug(format!("key: {}, size: {}", key, size).as_str());
@@ -69,9 +65,9 @@ fn alloc_set(ctx: &Context, args: Vec<String>) -> RedisResult {
6965
Ok(size.into())
7066
}
7167

72-
fn alloc_get(ctx: &Context, args: Vec<String>) -> RedisResult {
68+
fn alloc_get(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
7369
let mut args = args.into_iter().skip(1);
74-
let key = args.next_string()?;
70+
let key = args.next_arg()?;
7571

7672
let key = ctx.open_key(&key);
7773

examples/events.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[macro_use]
22
extern crate redis_module;
33

4-
use redis_module::{Context, NotifyEvent, RedisError, RedisResult, Status};
4+
use redis_module::{Context, NotifyEvent, RedisError, RedisResult, RedisString, Status};
55

66
fn on_event(ctx: &Context, event_type: NotifyEvent, event: &str, key: &str) {
77
let msg = format!(
@@ -15,12 +15,13 @@ fn on_stream(ctx: &Context, _event_type: NotifyEvent, _event: &str, _key: &str)
1515
ctx.log_debug("Stream event received!");
1616
}
1717

18-
fn event_send(ctx: &Context, args: Vec<String>) -> RedisResult {
18+
fn event_send(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
1919
if args.len() > 1 {
2020
return Err(RedisError::WrongArity);
2121
}
2222

23-
let status = ctx.notify_keyspace_event(NotifyEvent::GENERIC, "events.send", "mykey");
23+
let key_name = RedisString::create(ctx.ctx, "mykey");
24+
let status = ctx.notify_keyspace_event(NotifyEvent::GENERIC, "events.send", &key_name);
2425
match status {
2526
Status::Ok => Ok("Event sent".into()),
2627
Status::Err => Err(RedisError::Str("Generic error")),

examples/hello.rs

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#[macro_use]
22
extern crate redis_module;
33

4-
use redis_module::{parse_integer, Context, RedisError, RedisResult};
4+
use redis_module::{Context, RedisError, RedisResult, RedisString};
55

6-
fn hello_mul(_: &Context, args: Vec<String>) -> RedisResult {
6+
fn hello_mul(_: &Context, args: Vec<RedisString>) -> RedisResult {
77
if args.len() < 2 {
88
return Err(RedisError::WrongArity);
99
}
1010

1111
let nums = args
1212
.into_iter()
1313
.skip(1)
14-
.map(|s| parse_integer(&s))
14+
.map(|s| s.parse_integer())
1515
.collect::<Result<Vec<i64>, RedisError>>()?;
1616

1717
let product = nums.iter().product();
@@ -32,48 +32,3 @@ redis_module! {
3232
["hello.mul", hello_mul, "", 0, 0, 0],
3333
],
3434
}
35-
36-
//////////////////////////////////////////////////////
37-
38-
#[cfg(test)]
39-
mod tests {
40-
use super::*;
41-
use redis_module::RedisValue;
42-
43-
fn run_hello_mul(args: &[&str]) -> RedisResult {
44-
hello_mul(
45-
&Context::dummy(),
46-
args.iter().map(|v| String::from(*v)).collect(),
47-
)
48-
}
49-
50-
#[test]
51-
fn hello_mul_valid_integer_args() {
52-
let result = run_hello_mul(&vec!["hello.mul", "10", "20", "30"]);
53-
54-
match result {
55-
Ok(RedisValue::Array(v)) => {
56-
assert_eq!(
57-
v,
58-
vec![10, 20, 30, 6000]
59-
.into_iter()
60-
.map(RedisValue::Integer)
61-
.collect::<Vec<_>>()
62-
);
63-
}
64-
_ => assert!(false, "Bad result: {:?}", result),
65-
}
66-
}
67-
68-
#[test]
69-
fn hello_mul_bad_integer_args() {
70-
let result = run_hello_mul(&vec!["hello.mul", "10", "xx", "30"]);
71-
72-
match result {
73-
Err(RedisError::String(s)) => {
74-
assert_eq!(s, "Couldn't parse as integer: xx");
75-
}
76-
_ => assert!(false, "Bad result: {:?}", result),
77-
}
78-
}
79-
}

examples/keys_pos.rs

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#[macro_use]
22
extern crate redis_module;
33

4-
use redis_module::{Context, RedisError, RedisResult, RedisValue};
4+
use redis_module::{Context, RedisError, RedisResult, RedisString, RedisValue};
55

6-
fn keys_pos(ctx: &Context, args: Vec<String>) -> RedisResult {
6+
fn keys_pos(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
77
// Number of args (excluding command name) must be even
88
if (args.len() - 1) % 2 != 0 {
99
return Err(RedisError::WrongArity);
@@ -33,49 +33,3 @@ redis_module! {
3333
["keys_pos", keys_pos, "getkeys-api", 1, 1, 1],
3434
],
3535
}
36-
37-
//////////////////////////////////////////////////////
38-
39-
#[cfg(test)]
40-
mod tests {
41-
use super::*;
42-
use redis_module::RedisValue;
43-
44-
fn into_string_vec(args: &[&str]) -> Vec<String> {
45-
args.iter().map(|v| String::from(*v)).collect()
46-
}
47-
48-
fn into_redisvalue_vec(args: &[&str]) -> Vec<RedisValue> {
49-
args.iter()
50-
.map(|&s| RedisValue::BulkString(s.to_string()))
51-
.collect()
52-
}
53-
54-
#[test]
55-
fn test_keys_pos() {
56-
let result = keys_pos(
57-
&Context::dummy(),
58-
into_string_vec(&["keys_pos", "a", "1", "b", "2"]),
59-
);
60-
61-
match result {
62-
Ok(RedisValue::Array(v)) => {
63-
assert_eq!(v, into_redisvalue_vec(&["a", "b"]));
64-
}
65-
_ => assert!(false, "Bad result: {:?}", result),
66-
}
67-
}
68-
69-
#[test]
70-
fn test_keys_pos_bad_args() {
71-
let result = keys_pos(
72-
&Context::dummy(),
73-
into_string_vec(&["keys_pos", "a", "1", "b"]),
74-
);
75-
76-
match result {
77-
Err(RedisError::WrongArity) => (),
78-
_ => assert!(false, "Bad result: {:?}", result),
79-
}
80-
}
81-
}

examples/lists.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
extern crate redis_module;
33

44
use redis_module::raw::KeyType;
5-
use redis_module::{Context, NextArg, RedisError, RedisResult, RedisValue};
5+
use redis_module::{Context, NextArg, RedisError, RedisResult, RedisString, RedisValue};
66

77
// LPOPRPUSH source destination
88
// Pops and returns the first element (head) of the list stored at 'source'
99
// and pushes the element to the last position (tail) of the list stored at
1010
// 'destination'.
11-
fn lpoprpush(ctx: &Context, args: Vec<String>) -> RedisResult {
11+
fn lpoprpush(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
1212
let mut args = args.into_iter().skip(1);
1313

14-
let src = args.next_string()?;
15-
let dst = args.next_string()?;
14+
let src = args.next_arg()?;
15+
let dst = args.next_arg()?;
1616

1717
let src_key = ctx.open_key_writable(&src);
1818
let dst_key = ctx.open_key_writable(&dst);

examples/load_unload.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#[macro_use]
22
extern crate redis_module;
33

4-
use redis_module::{Context, LogLevel, Status};
4+
use redis_module::{Context, LogLevel, RedisString, Status};
55

66
static mut GLOBAL_STATE: Option<String> = None;
77

8-
fn init(ctx: &Context, args: &Vec<String>) -> Status {
8+
fn init(ctx: &Context, args: &Vec<RedisString>) -> Status {
99
let (before, after) = unsafe {
1010
let before = GLOBAL_STATE.clone();
1111
GLOBAL_STATE.replace(format!("Args passed: {}", args.join(", ")));

examples/threads.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#[macro_use]
22
extern crate redis_module;
33

4-
use redis_module::{Context, RedisResult, ThreadSafeContext};
4+
use redis_module::{Context, RedisResult, RedisString, ThreadSafeContext};
55
use std::mem::drop;
66
use std::thread;
77
use std::time::Duration;
88

9-
fn threads(_: &Context, _args: Vec<String>) -> RedisResult {
9+
fn threads(_: &Context, _args: Vec<RedisString>) -> RedisResult {
1010
thread::spawn(move || {
1111
let thread_ctx = ThreadSafeContext::new();
1212

examples/timer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[macro_use]
22
extern crate redis_module;
33

4-
use redis_module::{Context, NextArg, RedisResult};
4+
use redis_module::{Context, NextArg, RedisResult, RedisString};
55
use std::time::Duration;
66

77
fn callback(ctx: &Context, data: String) {
@@ -10,7 +10,7 @@ fn callback(ctx: &Context, data: String) {
1010

1111
type MyData = String;
1212

13-
fn timer_create(ctx: &Context, args: Vec<String>) -> RedisResult {
13+
fn timer_create(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
1414
let mut args = args.into_iter().skip(1);
1515
let duration = args.next_i64()?;
1616
let data: MyData = args.next_string()?;
@@ -20,7 +20,7 @@ fn timer_create(ctx: &Context, args: Vec<String>) -> RedisResult {
2020
return Ok(format!("{}", timer_id).into());
2121
}
2222

23-
fn timer_info(ctx: &Context, args: Vec<String>) -> RedisResult {
23+
fn timer_info(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
2424
let mut args = args.into_iter().skip(1);
2525
let timer_id = args.next_u64()?;
2626

@@ -30,7 +30,7 @@ fn timer_info(ctx: &Context, args: Vec<String>) -> RedisResult {
3030
return Ok(reply.into());
3131
}
3232

33-
fn timer_stop(ctx: &Context, args: Vec<String>) -> RedisResult {
33+
fn timer_stop(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
3434
let mut args = args.into_iter().skip(1);
3535
let timer_id = args.next_u64()?;
3636

src/context/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ impl Context {
153153
.into()
154154
},
155155

156+
Ok(RedisValue::BulkRedisString(s)) => unsafe {
157+
raw::RedisModule_ReplyWithString.unwrap()(self.ctx, s.inner).into()
158+
},
159+
156160
Ok(RedisValue::Array(array)) => {
157161
unsafe {
158162
// According to the Redis source code this always succeeds,
@@ -199,15 +203,11 @@ impl Context {
199203
}
200204
}
201205

202-
pub fn open_key(&self, key: &str) -> RedisKey {
206+
pub fn open_key(&self, key: &RedisString) -> RedisKey {
203207
RedisKey::open(self.ctx, key)
204208
}
205209

206-
pub fn open_with_redis_string(&self, key: *mut raw::RedisModuleString) -> RedisKeyWritable {
207-
RedisKeyWritable::open_with_redis_string(self.ctx, key)
208-
}
209-
210-
pub fn open_key_writable(&self, key: &str) -> RedisKeyWritable {
210+
pub fn open_key_writable(&self, key: &RedisString) -> RedisKeyWritable {
211211
RedisKeyWritable::open(self.ctx, key)
212212
}
213213

@@ -237,7 +237,7 @@ impl Context {
237237
&self,
238238
event_type: raw::NotifyEvent,
239239
event: &str,
240-
keyname: &str,
240+
keyname: &RedisString,
241241
) -> raw::Status {
242242
raw::notify_keyspace_event(self.ctx, event_type, event, keyname)
243243
}

0 commit comments

Comments
 (0)