mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 22:12:21 -04:00
Refactor reviews_for_fsrs function for improved performance
Replaced the previous implementation with a more efficient approach using a single loop and pre-allocated vectors. This change reduces the complexity of creating FSRSItems and enhances overall performance, especially for larger datasets.
This commit is contained in:
parent
b97fb45e06
commit
ecd1cf45e9
1 changed files with 16 additions and 18 deletions
|
@ -481,24 +481,22 @@ pub(crate) fn reviews_for_fsrs(
|
||||||
let skip = if training { 1 } else { 0 };
|
let skip = if training { 1 } else { 0 };
|
||||||
// Convert the remaining entries into separate FSRSItems, where each item
|
// Convert the remaining entries into separate FSRSItems, where each item
|
||||||
// contains all reviews done until then.
|
// contains all reviews done until then.
|
||||||
let items: Vec<(RevlogId, FSRSItem)> = entries
|
let mut items = Vec::with_capacity(entries.len());
|
||||||
.iter()
|
let mut current_reviews = Vec::with_capacity(entries.len());
|
||||||
.enumerate()
|
for (idx, (entry, &delta_t)) in entries.iter().zip(delta_ts.iter()).enumerate() {
|
||||||
.skip(skip)
|
current_reviews.push(FSRSReview {
|
||||||
.map(|(outer_idx, entry)| {
|
rating: entry.button_chosen as u32,
|
||||||
let reviews = entries
|
delta_t,
|
||||||
.iter()
|
});
|
||||||
.take(outer_idx + 1)
|
if idx >= skip {
|
||||||
.enumerate()
|
if !training || current_reviews.last().unwrap().delta_t > 0 {
|
||||||
.map(|(inner_idx, r)| FSRSReview {
|
let item = FSRSItem {
|
||||||
rating: r.button_chosen as u32,
|
reviews: current_reviews.clone(),
|
||||||
delta_t: delta_ts[inner_idx],
|
};
|
||||||
})
|
items.push((entry.id, item));
|
||||||
.collect();
|
}
|
||||||
(entry.id, FSRSItem { reviews })
|
}
|
||||||
})
|
}
|
||||||
.filter(|(_, item)| !training || item.reviews.last().unwrap().delta_t > 0)
|
|
||||||
.collect_vec();
|
|
||||||
if items.is_empty() {
|
if items.is_empty() {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue