[−][src]Function kul::source_stream::to_rc_box_str
pub fn to_rc_box_str(s: String) -> Rc<Box<str>>
Converting a String
to a boxed str
slice gives the program a chance to
use minimal memory by freeing the String
's extra capacity.
This might cheaply convert in place, because it only shrinks the allocated
size if the String
's capacity is greater than its length (but this is
likely), and the allocator has a chance to not move it if reallocation is
done and a chance to free and reuse the extra capacity (depending on the
allocator used). But the allocator might move it, which is a cost, but this
might be acceptable for the application because that one-time performance
cost could be worth giving it the chance to reclaim unused memory from these
now-immutable strings which might live a long time.
But the additional disadvantage of Rc<Box<str>>
is that there is an extra
level of pointer indirection compared to Rc<str>
, which could impact cache
locality. However, converting to Rc<str>
always must
move the string contents and never has the chance of cheaply converting in
place.