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

Prepare release of debouncer full 0.3.2 #643

Merged
merged 4 commits into from
Oct 14, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve sort_events performance
  • Loading branch information
dfaust committed Sep 29, 2024
commit f5c47023a6cd331715f8b43c904d1fb51ba94d80
43 changes: 27 additions & 16 deletions notify-debouncer-full/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@
//! - `crossbeam` enabled by default, adds [`DebounceEventHandler`](DebounceEventHandler) support for crossbeam channels.
//! Also enables crossbeam-channel in the re-exported notify. You may want to disable this when using the tokio async runtime.
//! - `serde` enables serde support for events.
//!
//!
//! # Caveats
//!
//!
//! As all file events are sourced from notify, the [known problems](https://docs.rs/notify/latest/notify/#known-problems) section applies here too.

mod cache;
Expand All @@ -69,7 +69,8 @@ mod debounced_event;
mod testing;

use std::{
collections::{HashMap, VecDeque},
cmp::Reverse,
collections::{BinaryHeap, HashMap, VecDeque},
path::PathBuf,
sync::{
atomic::{AtomicBool, Ordering},
Expand Down Expand Up @@ -657,21 +658,31 @@ fn sort_events(events: Vec<DebouncedEvent>) -> Vec<DebouncedEvent> {
});

// push events for different paths in chronological order and keep the order of events with the same path
while !events_by_path.is_empty() {
let min_time = events_by_path
.values()
.map(|events| events[0].time)
.min()
.unwrap();

for events in events_by_path.values_mut() {
while events.front().is_some_and(|event| event.time <= min_time) {
let event = events.pop_front().unwrap();
sorted.push(event);
}

let mut min_time_heap = events_by_path
.iter()
.map(|(path, events)| Reverse((events[0].time, path.clone())))
.collect::<BinaryHeap<_>>();

while let Some(Reverse((min_time, path))) = min_time_heap.pop() {
// unwrap is safe because only paths from `events_by_path` are added to `min_time_heap`
// and they are never removed from `events_by_path`.
let events = events_by_path.get_mut(&path).unwrap();

let mut push_next = false;

while events.front().is_some_and(|event| event.time <= min_time) {
// unwrap is safe beause `pop_front` mus return some in order to enter the loop
let event = events.pop_front().unwrap();
sorted.push(event);
push_next = true;
}

events_by_path.retain(|_, events| !events.is_empty());
if push_next {
if let Some(event) = events.front() {
min_time_heap.push(Reverse((event.time, path)));
}
}
}

sorted
Expand Down