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

strings: add Builder.Cap method #26269

Closed
mvdan opened this issue Jul 8, 2018 · 4 comments
Closed

strings: add Builder.Cap method #26269

mvdan opened this issue Jul 8, 2018 · 4 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. Proposal-Accepted
Milestone

Comments

@mvdan
Copy link
Member

mvdan commented Jul 8, 2018

This would mirror bytes.Buffer.Cap, which returns the capacity of the underlying []byte.

In particular, I need access to the capacity to calculate if a number of bytes will fit in the Builder without an allocation. That would look like:

func canFitBytes(b *strings.Builder, n int) bool {
    remaining := b.Cap() - b.Len()
    return remaining >= n
}

One possible use case is to control how the allocations happen. For example, I'd like to "batch" []byte to string allocations, to amortize their cost until the compiler gets smart enough to remove the allocs when possible.

My code could look like:

// amortizedString is like string(p), but for small strings, allocations are batched via a strings.Builder.
func amortizedString(b *strings.Builder, p []byte) string {
    if len(p) >= 2048 {
        return string(p)
    }
    if !canFitBytes(b, len(p)) {
        b.Reset()
        b.Grow(2048)
    }
    start := b.Len()
    b.Write(p)
    return b.String()[start:]
}

As far as I can tell, writing such a function is not simple without a Cap method. And adding such a method shouldn't add any problems, other than adding to the API. Unless there is a particular reason why the method was left out that I'm not aware of.

/cc @cespare @bradfitz @ianlancetaylor

@gopherbot gopherbot added this to the Proposal milestone Jul 8, 2018
@mvdan
Copy link
Member Author

mvdan commented Jul 8, 2018

Also - I realise that this use case is only temporary, but still, I imagine that Cap would be useful for other things. As a similar example, batching string allocations in cases where copying the bytes is still required.

@rsc
Copy link
Contributor

rsc commented Jul 9, 2018

Seems to have been an oversight / harmless to add. Accepted for proposal-review.

@rsc rsc modified the milestones: Proposal, Go1.12 Jul 9, 2018
@rsc rsc changed the title proposal: strings: add Builder.Cap method strings: add Builder.Cap method Jul 9, 2018
@ianlancetaylor ianlancetaylor added NeedsFix The path to resolution is known, but the work has not been done. help wanted and removed Proposal labels Jul 9, 2018
@mvdan mvdan self-assigned this Jul 9, 2018
@mvdan
Copy link
Member Author

mvdan commented Jul 9, 2018

Thanks - that was a quick resolution :)

@gopherbot
Copy link

Change https://golang.org/cl/122835 mentions this issue: strings: add Builder.Cap

@mvdan mvdan removed the help wanted label Jul 16, 2018
@golang golang locked and limited conversation to collaborators Aug 20, 2019
@rsc rsc unassigned mvdan Jun 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. Proposal-Accepted
Projects
None yet
Development

No branches or pull requests

4 participants