Skip to content

Commit dde0442

Browse files
committed
re-use string allocation where possible
1 parent f7d2cf9 commit dde0442

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

usdt-impl/src/common.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,10 @@ fn asm_type_convert(typ: &DataType, input: TokenStream) -> (TokenStream, TokenSt
171171
// data in a result-like JSON blob, mapping the `Result`'s variants to the keys "ok"
172172
// and "err".
173173
quote! {
174-
[
175-
match ::usdt::to_json(&#input) {
176-
Ok(json) => format!("{{\"ok\":{}}}", json),
177-
Err(e) => format!("{{\"err\":\"{}\"}}", e.to_string()),
178-
}.as_bytes(),
179-
&[0_u8]
180-
].concat()
174+
match ::usdt::to_json(&#input) {
175+
Ok(json) => format!("{{\"ok\":{}}}\0", json).into_bytes(),
176+
Err(e) => format!("{{\"err\":\"{}\"}}\0", e.to_string()).into_bytes(),
177+
}
181178
},
182179
quote! { .as_ptr() as usize },
183180
),
@@ -186,9 +183,7 @@ fn asm_type_convert(typ: &DataType, input: TokenStream) -> (TokenStream, TokenSt
186183
quote! { .as_ptr() as usize },
187184
),
188185
DataType::Native(dtrace_parser::DataType::String) => (
189-
quote! {
190-
[(#input.as_ref() as &str).as_bytes(), &[0_u8]].concat()
191-
},
186+
quote! { ::usdt::SerializeString::serialize(#input) },
192187
quote! { .as_ptr() as usize },
193188
),
194189
DataType::Native(_) => {
@@ -362,7 +357,7 @@ mod tests {
362357

363358
let expected = quote! {
364359
let arg_0 = (*<_ as ::std::borrow::Borrow<*const u8>>::borrow(&args.0) as usize);
365-
let arg_1 = [(args.1.as_ref() as &str).as_bytes(), &[0_u8]].concat();
360+
let arg_1 = { let mut v = Vec::from(args.1); v.push(0); v };
366361
};
367362
assert_eq!(args.to_string(), expected.to_string());
368363

@@ -404,7 +399,7 @@ mod tests {
404399
);
405400
assert_eq!(
406401
out.to_string(),
407-
quote! { [(foo.as_ref() as &str).as_bytes(), &[0_u8]].concat() }.to_string()
402+
quote! {{ let mut v = Vec::from(foo); v.push(0); v }}.to_string()
408403
);
409404
assert_eq!(post.to_string(), quote! { .as_ptr() as usize }.to_string());
410405

usdt-impl/src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,27 @@ impl Clone for UniqueId {
421421
}
422422
}
423423

424+
#[doc(hidden)]
425+
pub trait SerializeString {
426+
fn serialize(self) -> Vec<u8>;
427+
}
428+
429+
#[doc(hidden)]
430+
impl<S: AsRef<str> + ?Sized> SerializeString for &S {
431+
#[inline]
432+
fn serialize(self) -> Vec<u8> {
433+
[self.as_ref().as_bytes(), b"0"].concat()
434+
}
435+
}
436+
437+
#[doc(hidden)]
438+
impl SerializeString for String {
439+
fn serialize(mut self) -> Vec<u8> {
440+
self.push('\0');
441+
self.into_bytes()
442+
}
443+
}
444+
424445
#[cfg(test)]
425446
mod test {
426447
use super::*;

usdt/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ use std::{env, fs};
295295

296296
pub use usdt_attr_macro::provider;
297297
#[doc(hidden)]
298-
pub use usdt_impl::to_json;
298+
pub use usdt_impl::{to_json, SerializeString};
299299
pub use usdt_impl::{Error, UniqueId};
300300
pub use usdt_macro::dtrace_provider;
301301

0 commit comments

Comments
 (0)