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

TextEditor use 2gb memory for rendering a 10mb file #2799

Open
lost22git opened this issue Mar 10, 2023 · 2 comments
Open

TextEditor use 2gb memory for rendering a 10mb file #2799

lost22git opened this issue Mar 10, 2023 · 2 comments
Labels
bug Something is broken

Comments

@lost22git
Copy link

cc

code for reproduce

use eframe::egui::{
    self,
    CentralPanel,
    TextEdit,
};

fn main() -> Result<(), eframe::Error,> {
    let options = eframe::NativeOptions {
        ..Default::default()
    };

    eframe::run_native(
        "editor big file test",
        options,
        Box::new(|_cc| Box::<MyApp,>::new(MyApp::new(),),),
    )
}

struct MyApp {
    text: String,
}

impl MyApp {
    fn new() -> Self {
        let bytes = include_bytes!("../10mb.png"); // a 10mb png file
        let encoding = detect_encoding(bytes,);
        let (str, _, _,) = encoding.decode(bytes,);
        let string: String = str.into();
        MyApp {
            text: string,
        }
    }
}

pub fn detect_encoding(text: &[u8],) -> &'static encoding_rs::Encoding {
    let len = text.len();
    let to = std::cmp::min(1000, len,);
    let mut encoding_detector = chardetng::EncodingDetector::new();
    encoding_detector.feed(&text[0..to], to == len,);
    encoding_detector.guess(None, true,)
}

impl eframe::App for MyApp {
    fn update(
        &mut self,
        ctx: &egui::Context,
        _frame: &mut eframe::Frame,
    ) {
        CentralPanel::default().show(ctx, |ui| {
            let code_editor = TextEdit::multiline(&mut self.text,)
                .code_editor()
                .frame(true,)
                .lock_focus(true,)
                .desired_width(f32::INFINITY,)
                .desired_rows(40,);
            let _output = code_editor.show(ui,);
        },);
    }
}
@lost22git lost22git added the bug Something is broken label Mar 10, 2023
@YgorSouza
Copy link
Contributor

Here is a version of the new method that generates a big string without any dependencies, so it is easier to test:

    fn new() -> Self {
        let bytes = (0..1_000_000).flat_map(|_| (0u8..10));
        let string: String = bytes.map(|b| format!(" {b:02x}")).collect();
        MyApp { text: string }
    }

Anyway, I don't think this is a "bug". The file is just too big for an immediate UI to handle. It is trying to render the entire string as an image because it doesn't know where to start or stop, so the buffer ends up being huge. There would have to be an equivalent to the show_rows method for the text editor, so I guess this is more of a feature request?

@lost22git
Copy link
Author

@YgorSouza I see, thanks for your explaination ;-)

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

No branches or pull requests

2 participants