Skip to content

Commit 52d39b7

Browse files
Better string re-use
1 parent aa8eb94 commit 52d39b7

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

ext/hyper_ruby/src/request.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use magnus::{value::{qnil, ReprValue}, RString, Value};
55
use bytes::Bytes;
66
use hyper::Request as HyperRequest;
77

8-
use rb_sys::{rb_str_resize, rb_str_cat, VALUE};
8+
use rb_sys::{rb_str_cat, rb_str_resize, RSTRING_PTR, VALUE};
99

1010
// Type passed to ruby giving access to the request properties.
1111
#[magnus::wrap(class = "HyperRuby::Request")]
@@ -53,9 +53,11 @@ impl Request {
5353
unsafe {
5454
let rb_value = buffer.as_value();
5555
let inner: VALUE = std::ptr::read(&rb_value as *const _ as *const VALUE);
56-
rb_str_resize(inner, 0);
56+
rb_str_resize(inner, body_len.try_into().unwrap());
5757
if body_len > 0 {
58-
rb_str_cat(inner, body.as_ptr() as *const c_char, body.len().try_into().unwrap());
58+
let body_ptr = body.as_ptr() as *const c_char;
59+
let rb_string_ptr = RSTRING_PTR(inner) as *mut c_char;
60+
std::ptr::copy(body_ptr, rb_string_ptr, body_len);
5961
}
6062
}
6163

0 commit comments

Comments
 (0)