Skip to content

Commit

Permalink
Heap allocate readline_state objects
Browse files Browse the repository at this point in the history
The readline_state object that reach Readline instance carries around is
fairly sizable, weighing in at 512 bytes each. We end up copying it
around whenever we move such an object, which is work that can easily be
avoided.
With this change we heap allocate the data, shrinking the size of a
Readline object significantly and causing moves to have much less data
to copy.
  • Loading branch information
d-e-s-o committed Mar 30, 2024
1 parent 104b9af commit f58cc41
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/lib.rs
Expand Up @@ -161,7 +161,7 @@ impl<T> Locked for Mutex<T> {
/// A wrapper for `MutexGuard` ensuring that our libreadline state is read back before dropping.
struct ReadlineGuard<'data> {
_guard: MutexGuard<'data, Id>,
state: RefMut<'data, readline_state>,
state: RefMut<'data, Box<readline_state>>,
}

impl Drop for ReadlineGuard<'_> {
Expand All @@ -184,7 +184,7 @@ type Key = [u8];
#[derive(Debug)]
pub struct Readline {
id: Id,
state: RefCell<readline_state>,
state: RefCell<Box<readline_state>>,
}

impl Readline {
Expand Down Expand Up @@ -232,7 +232,7 @@ impl Readline {
pub fn new() -> Self {
let rl = Self {
id: Id::new(),
state: RefCell::new(Self::initial().clone()),
state: RefCell::new(Box::new(Self::initial().clone())),
};

{
Expand Down

0 comments on commit f58cc41

Please sign in to comment.