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

kqueue: Make watcher.Close() O(n) instead of O(n^2) #233

Merged
merged 3 commits into from
Jul 21, 2022

Conversation

nicks
Copy link
Contributor

@nicks nicks commented Jan 17, 2018

What does this pull request do?

Fixes a performance problem in the kqueue-based implementation of watcher.

In the old implementation, watcher.Close() would clone the list of watches, then run Remove. Each run of Remove would also iterate over the full list of watches.

This indexes the watches better so that Remove doesn't need to iterate over the full list.

How should this be manually tested?

There should be no functional changes in this PR

kqueue.go Outdated
@@ -119,6 +121,16 @@ func (w *Watcher) Remove(name string) error {
w.mu.Lock()
isDir := w.paths[watchfd].isDir
delete(w.watches, name)

parentName := filepath.Clean(filepath.Dir(name))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the filepath.Clean() is unneeded. From my look at https://golang.org/src/path/filepath/path.go?s=13376:13404#L452 it seems Dir() already cleans it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right! fixed

kqueue.go Outdated Show resolved Hide resolved
kqueue.go Outdated Show resolved Hide resolved
kqueue.go Outdated Show resolved Hide resolved
kqueue.go Outdated Show resolved Hide resolved
kqueue.go Show resolved Hide resolved
kqueue.go Show resolved Hide resolved
@nhooyr
Copy link
Contributor

nhooyr commented Jan 17, 2018

does this PR work if you try and watch /?

@nicks
Copy link
Contributor Author

nicks commented Jan 18, 2018

afaict it should still work fine if you watch "/"? filepath.Dir("/") == "/". So it will add an entry to the map on Add, and remove one on Remove.

kqueue.go Outdated Show resolved Hide resolved
kqueue.go Show resolved Hide resolved
nhooyr
nhooyr previously approved these changes Jan 19, 2018
@nhooyr
Copy link
Contributor

nhooyr commented Jan 19, 2018

@nicks thanks so much for this change <3 😍

nathany
nathany previously approved these changes Oct 5, 2019
Copy link
Contributor

@nathany nathany left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @nicks and @nhooyr.

@nicks
Copy link
Contributor Author

nicks commented May 5, 2020

Two years may have passed, but we're still using this code and interested in this getting merged! Is there anything I need to do to get it approved?

@nicks nicks requested a review from nathany May 5, 2020 19:47
@nathany
Copy link
Contributor

nathany commented May 6, 2020

  • Has this been rebased on master?
  • Can someone test this out on macOS/BSD?

@nicks
Copy link
Contributor Author

nicks commented May 6, 2020

yep, it's rebased. we've been using this in deployed binaries for a while now with no issues. if there are tests and/or benchmarks you'd like me to add that would be helpful, happy to add them.

theckman
theckman previously approved these changes May 9, 2020
Copy link

@theckman theckman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks to work on macOS 10.15.4. I see there are some open comments, but it seems like they are largely resolved. 👍 from me.

@nicks
Copy link
Contributor Author

nicks commented Jan 19, 2022

rebased on latest main branch!

@arp242 arp242 merged commit 6ae56b7 into fsnotify:main Jul 21, 2022
@shogo82148 shogo82148 mentioned this pull request Mar 6, 2024
25 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants