-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
proposal: Go 2: variable length channel #64855
Comments
What's the point of this exactly? Just saving memory when the channel is not being used much? |
Yes, this is I want. |
I'm unclear in what conditions the channel would grow. That hell a lot of knobs and the performance impact of getting this wrong could be very bad compared to the gains. It also seems to me you could implement this in a third party librairy, you would need a background go-routine per channel if you want |
Please fill out https://github.com/golang/proposal/blob/master/go2-language-changes.md when proposing language changes |
Channel buffers are rarely a significant fraction of total memory usage; most are of size zero, or one, or the number of worker goroutines. Making a language change for such a minor space optimization would be all cost and no significant benefit. Therefore, this is a likely decline. Leaving open for four weeks for final comments. |
I have also encountered such a need before |
probably like this, but this code may not be a good way package main
import (
"fmt"
"time"
)
type User struct {
Name string
keys []string
}
func main() {
users := []User{
{Name: "foo", keys: []string{"a", "b", "c", "e"}},
{Name: "bar", keys: []string{"f", "g", "h", "i"}},
}
// if there is a variable length channel
// no need to calculate total
total := 0
for _, u := range users {
for range u.keys {
total += 1
}
}
buffer := make(chan string, total)
for _, u := range users {
for _, k := range u.keys {
go func(d string) {
time.Sleep(time.Second)
buffer <- d
fmt.Println("Push", d)
}(k)
}
}
// close(buffer)
// for range buffer {}
for total > 0 {
data, ok := <-buffer
fmt.Println("Pop", data, ok)
total--
}
close(buffer)
} |
@yangjinheng The code you show seems to make chan use just the right amount of memory. |
No change in consensus, so declined. |
Proposal Details
The channel length can variable.
c := make(chan int ,
<MaxLen>
[,<MinLen>
] [,<TimeDuration>
])first:
cap(c) =
<MinLen>
if full:
cap(c) = cap(c) * 2 // Max
<MaxLen>
if len(c) < cap(c) /4 && timeout
<TimeDuration>
{cap(c) = cap(c) / 2 // Min
<MinLen>
}
The text was updated successfully, but these errors were encountered: