Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

design question: Should transform_buffer be incremental? #42

Open
tesuji opened this issue Jun 20, 2024 · 3 comments
Open

design question: Should transform_buffer be incremental? #42

tesuji opened this issue Jun 20, 2024 · 3 comments

Comments

@tesuji
Copy link
Contributor

tesuji commented Jun 20, 2024

Hi, I use vi-rs for my linux input engine with a IBUS framework.
But IBus passes a single char each time a key typed.
For every key typed, I should display the preview string (as in Vietnameses) to users.
So I couldn't cache each/a word (Vec<char>) and pass it to transform_buffer.
So transform_buffer is called for every key typed.

So I wonder if we could create a new incremental transform_buffer.

fn transform_buffer_incremental<'def, 'buf>(
    def: &'def Definition,
    output: &'buf mut String,
) -> IncrementalBuffer;

struct IncrementalBuffer<'def, 'buf> {
    def: &'def Definition,
    output: &'buf mut String,
    input: Vec<char>,
    // cache something for example a Word.
}

impl IncrementalBuffer {
    pub fn push(&mut self, ch: char) -> TransformResult {
        self.input.push(ch);
        // ...
    }
    pub fn view(&self) -> &str;
    pub fn clear(&mut self) {
        self.output.clear();
        // clear cache
    }
}
@ZeroX-DG
Copy link
Owner

ZeroX-DG commented Jun 24, 2024

Woah. Nice work with the input engine!

Just curious, what is preventing you from introducing this incremental buffer in your input engine? 🤔

@tesuji
Copy link
Contributor Author

tesuji commented Jun 24, 2024

I'm sorry for my misleading question. I could cache typed keys. However, look at vi::transfrom_buffer code

vi-rs/src/methods.rs

Lines 182 to 196 in 6c82a29

pub fn transform_buffer<I>(
definition: &Definition,
buffer: I,
output: &mut String,
) -> TransformResult
where
I: IntoIterator<Item = char>,
{
let mut word = Word::empty();
let mut tone_mark_removed = false;
let mut letter_modification_removed = false;
let mut last_executed_action = None;
for ch in buffer {

A for loop is called from start to end of the buffer for every new typed key, like

transform_buffer(&self.buffer, &mut self.output);
// while "chaof" is typing, equivalent to 
transform_buffer(&"c", &mut self.output);   
transform_buffer(&"ch", &mut self.output);
transform_buffer(&"cha", &mut self.output);
transform_buffer(&"chao", &mut self.output);
transform_buffer(&"chaof", &mut self.output);

If we could cache every things outside that for loop, we reduce allocations for Word, and avoid looping from the start of each word/buffer.

@ZeroX-DG
Copy link
Owner

Ah I see what you mean. That's a great idea. We should definitely add something like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants