Csv iteration hangs after adding 1 more row

I have a csv file with 14 rows. I iterate over it using fold to convert each row to a single line of text delimited by a comma. I appended the accumulator in the fold which was a mistake but I observed 14 rows takes few seconds to complete and 15 rows hang.

How does 1 more iteration cause a hang?

14 row csv file

hello,hello,hello,hello
hello,hello,hello,hello
hello,hello,hello,hello
hello,hello,hello,hello
hello,hello,hello,hello
hello,hello,hello,hello
hello,hello,hello,hello
hello,hello,hello,hello
hello,hello,hello,hello
hello,hello,hello,hello
hello,hello,hello,hello
hello,hello,hello,hello
hello,hello,hello,hello
hello,hello,hello,hello

Code

fn main() {
    let mut hello1 = include_str!("../hello1.csv");
    hello1.lines().fold(String::new(),|acc,b|{
        let delim = match acc.as_str() {
            "" => "",
            _ => ","
        };
        let k2 = b.split(",").into_iter().fold(String::new(),|x1,x2|{
            // println!("acc={}//",b);
            format!("{}{}{} {}",acc,delim,x1,x2)
        });
        k2

    });
}

Probably because that’s the point where it eats up all of your system’s RAM? (So your system will start swapping, which is really slow.) Each line puts another 4 copies of acc into k2, which becomes the new acc. So RAM usage more-than-quadruples with each additional line of 4 hellos.

On my machine the 14 lines version takes more than 4GB of RAM, so the extra line would take RAM usage over 16GB.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.