-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
runtime: do map growth work on reads #19094
Comments
I'd rather implement do-grow-work-on-read. It's tricky, as you have to use atomic operations and whatnot, but I think it is possible and it doesn't expose any new API. |
That sounds great. |
Changed from proposal to regular bug and retitled. |
I falsely assumed years ago that this is the case. Since then, IIRC, in several places people were assured concurrent read operations on maps are safe. That may imply the expectation of no map mutations in such scenarios and that means no atomics/no lock with the good performance implications. If that's actually the status quo then I am slightly worried about losing this property, regardless of it had never been documented or guaranteed to such level of implementation details. |
Concurrent read operations on maps do have to work. But that doesn't mean that we can't modify the map on read. It just means that we have to do it in ways that programs can not detect. Hence the reference to atomic operations and whatnot. For example, we wouldn't do this because of the performance implications, but it would be perfectly fine to add a mutex to a map, acquire the mutex on every read, modify the map, and release the mutex when the read was complete. That would work for all existing programs except for the changes in performance. |
@ianlancetaylor I'm sorry that I was apparently not able to communicate clearly, but I'm now confused because what you wrote is precisely what I was trying to say, with emphasizes on "except for the changes in performance". |
My apologies for misunderstanding. When you said "I am slightly worried about losing this property" I am not sure what property you are worried about losing. |
I'm pretty sure we can get the common case for grow-on-read down to a single additional atomic load (which on x86 at least is just a regular load). |
Issue #51410 has a good explanation and benchmark results showing how the current situation (not doing growth work on reads) can lead to confusing and significant performance cliffs. |
CLs 37011 and 37012 suggest that finishing map growth can be important. If an author knows that a particular map is done growing and will henceforth be read-only, and they can spend some cycles optimizing it, it would be useful for them to be able to ask the runtime to finish any ongoing map growth.
This is kinda sort possible now:
However, this is really slow, particularly for a large map. It does way more work than is necessary, and it does it very inefficiently.
The compiler could recognize this idiom and convert it into a runtime call.
Another option is to add API surface:
runtime.OptimizeMap(m interface{})
or some such, which would be documented to be a slow, expensive, blocking call that makes subsequent reads more efficient.Another idiomatic option, if
copy
worked on maps :) would becopy(m, m)
.Other suggestions welcomed. I don't particularly like any of these, but it'd be nice to find something.
The text was updated successfully, but these errors were encountered: