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

proposal: Go 2: variable length channel #64855

Closed
coder1966 opened this issue Dec 24, 2023 · 9 comments
Closed

proposal: Go 2: variable length channel #64855

coder1966 opened this issue Dec 24, 2023 · 9 comments
Labels
LanguageChange Suggested changes to the Go language Proposal Proposal-FinalCommentPeriod v2 An incompatible library change
Milestone

Comments

@coder1966
Copy link

coder1966 commented Dec 24, 2023

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>
}

@randall77
Copy link
Contributor

What's the point of this exactly? Just saving memory when the channel is not being used much?

@coder1966
Copy link
Author

What's the point of this exactly? Just saving memory when the channel is not being used much?

Yes, this is I want.

@Jorropo
Copy link
Member

Jorropo commented Dec 24, 2023

I'm unclear in what conditions the channel would grow.
Wouldn't cap(c) always be MaxLen because it would always grow on demand ? The actual ring being smaller would be an implementation detail.


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 select compatibility, but the timeout isn't free either.

@seankhliao seankhliao changed the title Variable length channel proposal: Go 2: variable length channel Dec 24, 2023
@gopherbot gopherbot added this to the Proposal milestone Dec 24, 2023
@seankhliao seankhliao added LanguageChange Suggested changes to the Go language v2 An incompatible library change WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. labels Dec 24, 2023
@seankhliao
Copy link
Member

Please fill out https://github.com/golang/proposal/blob/master/go2-language-changes.md when proposing language changes

@adonovan
Copy link
Member

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.

@yangjinheng
Copy link

I have also encountered such a need before

@yangjinheng
Copy link

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)
}

@qiulaidongfeng
Copy link
Member

@yangjinheng The code you show seems to make chan use just the right amount of memory.
If a variable length chan is supported, the memory used by chan may not be just right.

@seankhliao seankhliao removed the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jan 13, 2024
@findleyr
Copy link
Member

findleyr commented Feb 7, 2024

No change in consensus, so declined.
— rfindley for the language proposal review group

@findleyr findleyr closed this as completed Feb 7, 2024
@findleyr findleyr closed this as not planned Won't fix, can't repro, duplicate, stale Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LanguageChange Suggested changes to the Go language Proposal Proposal-FinalCommentPeriod v2 An incompatible library change
Projects
None yet
Development

No branches or pull requests

9 participants