Skip to content

Commit 1411571

Browse files
committed
ser: don’t unnecessarily allocate a String when serialising a character
Use on-stack buffer to encode a character rather than allocating a new String. It’s faster and more memory-efficient.
1 parent ccb8fbd commit 1411571

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

src/ser/map.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ impl<'a> ser::Serializer for MapKeySerializer<'a> {
166166
}
167167

168168
fn serialize_char(self, value: char) -> Result<()> {
169-
self.ser.serialize_str(&value.to_string())
169+
let mut buf = [0u8; 4];
170+
self.ser.serialize_str(value.encode_utf8(&mut buf))
170171
}
171172

172173
fn serialize_bytes(self, _value: &[u8]) -> Result<()> {

0 commit comments

Comments
 (0)