Add load_if_changed to Cache to return a value only if it has changed#91
Add load_if_changed to Cache to return a value only if it has changed#91elichai wants to merge 1 commit into
Conversation
|
Hello I'm not saying no, but can you explain the use case of this? Conceptually, a Cache is for having an up to date value. Why would you need to know it has changed? |
|
@vorner The use case is that I have a program with a bunch of workers that all work on the same task (highly parallelizable tasks) and I use this to swap the task if a new task comes in, In other words, it looks something like that: let mut task = watcher.get_task();
loop {
task.id = ...;
// work
if let Some(new_task) = watcher.load_if_changed() {
task = (*new_task).clone();
}
} |
|
The fact you need to clone the thing inside the Arc kind of suggests arc-swap is the wrong tool for the job, to be honest. The What are your performance characteristics? Is the single iteration of the work so short that the performance of the synchronization primitive actually matters? If not, |
|
Basically what I'm trying to emulate here is a very fast watch channel, |
|
I think I understand what you do and somewhat of why you want to do it this way and why such API would help your case. On the other hand, I'm still reluctant because there seem to be a lot of „this feels wrong on the design level“ smell… both for the API and the requirements… I wouldn't really feel very happy about leading other people towards similar solutions. From your description, the right tool for your job seems to be the
let mut job_copy = ...;
loop {
let central_job = cache.load();
if central_job.generation != job_copy.generation {
job_copy = central_job.clone();
}
} |
This is required when you need to know if you received a new value or an old one you've already seen