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

cmd/compile: inline harder to optimize closure allocation #16759

Closed
hirochachacha opened this issue Aug 17, 2016 · 4 comments
Closed

cmd/compile: inline harder to optimize closure allocation #16759

hirochachacha opened this issue Aug 17, 2016 · 4 comments

Comments

@hirochachacha
Copy link
Contributor

hirochachacha commented Aug 17, 2016

I'm not sure but sometimes using closure causes unwanted allocations.

Please see https://go-review.googlesource.com/#/c/27211/ and https://go-review.googlesource.com/#/c/27210/.

I also don't know this is fixable or not, sorry.

@bradfitz bradfitz changed the title cmd/compile: optimize closure allocation cmd/compile: inline harder to optimize closure allocation Aug 17, 2016
@bradfitz
Copy link
Contributor

Note that this is about all the strings.Trim* and bytes.Trim* functions.

It looks like if the compiler could inline the super-trivial single-line makeCutsetFunc, then the escape analysis does work.

This patch makes removes the only allocation from strings.TrimLeft:

diff --git a/src/strings/strings.go b/src/strings/strings.go
index 738c493..7faa447 100644
--- a/src/strings/strings.go
+++ b/src/strings/strings.go
@@ -600,7 +600,7 @@ func TrimLeft(s string, cutset string) string {
        if s == "" || cutset == "" {
                return s
        }
-       return TrimLeftFunc(s, makeCutsetFunc(cutset))
+       return TrimLeftFunc(s, func(r rune) bool { return IndexRune(cutset, r) >= 0 })
 }       

where makeCutsetFunc is just:

func makeCutsetFunc(cutset string) func(rune) bool {
        return func(r rune) bool { return IndexRune(cutset, r) >= 0 }
}

I guess that is too "hairy" to be inlined, but it probably should be.

/cc @dr2chase @randall77 @mdempsky @josharian

@bradfitz bradfitz added this to the Unplanned milestone Aug 17, 2016
@josharian
Copy link
Contributor

I started on inlining closures during the last freeze and got stuck. I'll mail some of the finished preliminary work; it's just waiting for me to get to it. Lots to mail, little time.

@ALTree
Copy link
Member

ALTree commented Aug 17, 2016

This is #10292 (which also cites Trim performance) and also #15561 I guess?

@hirochachacha
Copy link
Contributor Author

True. this is exactly dup.

@golang golang locked and limited conversation to collaborators Aug 17, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants