I’m working on a very fast algorithm to solve the Advent of Code 2023 Day 8 part 2 challenge without the least-common-multiple approach.
One of my solutions works with HashMap
s internally. As keys and values, they contain strings or references to strings that are present in the input data.
So basically, I’m taking apart the input data and spread it across a bunch of derived data structures.
I can now either copy these bits using .to_owned()
, or just use references to sections within the original input, which brings runtime down from 2m12s to 1m38s.
My trouble is that I cannot use the fast reference approach without tying the lifetime of my data-structures to the lifetime of the reference to the input data, which means that the caller has to deal with that.
What I would prefer is to consume the input data, so the caller can forget about it, and still work with references internally. But that just doesn’t work out.
Let some dummy code highlight the issue:
use std::fs;
/**
* Works fine, but needs to duplicate data, which makes it a bit slower
*/
pub fn read_file_and_analyze_little_slow(file_name: &str) -> (String, Vec<String>) {
let content = fs::read_to_string(file_name).expect("Could not read the file you told me to analyze");
let lines: Vec<String> = content.split("n").map(|x| { x.to_owned() }).collect();
(content, lines)
}
/**
* I wish I could do this, but the read content of the file is dropped
* at the end of the file (or so the borrow checker thinks), invalidating
* the references
*/
pub fn read_file_and_analyze_fast(file_name: &str) -> (String, Vec<&str>) {
let content = fs::read_to_string(file_name).expect("Could not read the file you told me to analyze");
let lines: Vec<&str> = content.split("n").collect();
// Would need to make content live on somehow, so the references remain valid
(content, lines) // Error: cannot move out of `content` because it is borrowed
}
/**
* This works super well, but we've just transferred the issue to the calling function,
* because now our result is tied to the lifetime of input string, meaning the parent
* function can't pass it as _its_ result, unless it also works with lifetimes
*/
pub fn read_file_and_analyze_unwieldy<'a>(already_read_file: &'a str) -> Vec<&'a str> {
already_read_file.split("n").collect()
}
I’m looking for solutions to make read_file_and_analyze_fast
‘s API work. I suspect it’s probably possible but may require unsafe
, which I haven’t worked with before.
This is a very interesting problem, so some general guidance to deal with issues like this one would also be appreciated