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: better optimization/inlining for sort.Search #43423

Open
yangwenmai opened this issue Dec 29, 2020 · 4 comments
Open

cmd/compile: better optimization/inlining for sort.Search #43423

yangwenmai opened this issue Dec 29, 2020 · 4 comments
Assignees
Labels
FeatureRequest NeedsFix Performance
Milestone

Comments

@yangwenmai
Copy link
Contributor

@yangwenmai yangwenmai commented Dec 29, 2020

What version of Go are you using (go version)?

$ go version
go 1.15.6

Does this issue reproduce with the latest release?

go 1.15.6

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

What did you do?

When I use a helper func SearchLineInfos() and SearchFiles similar as searchInts(), I test those func benchmark

benchmark result:

name               old time/op  new time/op  delta
SearchLineInfos-8  20.7ns ±19%   6.0ns ± 2%  -70.98%  (p=0.000 n=10+10)
SearchFiles-8      9.48ns ±19%  3.22ns ± 1%  -66.05%  (p=0.000 n=10+8)

CL: https://go-review.googlesource.com/c/go/+/279447

What did you expect to see?

In position.go, we impl helper func for sort.Search.

What did you see instead?

if i := sort.Search(len(f.lines), func(i int) bool { return f.lines[i] > offset }) - 1; i>= 0 {
	line, column = i+1, offset-f.lines[i]+1
}

...

Remove func searchInts(a []int, x int) int

cc @mdempsky

@yangwenmai yangwenmai changed the title go/token: maybe we an improve compiler smarter about inlining sort.Search go/token: maybe we can improve compiler smarter about inlining sort.Search Dec 29, 2020
@mdempsky mdempsky self-assigned this Dec 29, 2020
@mdempsky mdempsky added FeatureRequest NeedsFix Performance labels Dec 29, 2020
@mdempsky mdempsky added this to the Go1.17 milestone Dec 29, 2020
@mdempsky mdempsky changed the title go/token: maybe we can improve compiler smarter about inlining sort.Search cmd/compile: better optimization/inlining for sort.Search Dec 29, 2020
@mdempsky
Copy link
Member

@mdempsky mdempsky commented Dec 29, 2020

So sort.Search is a relatively simple function that takes a function argument and then calls it at exactly one call site. The compiler should be able to discount the cost of inlining when sort.Search is called with an inlinable function.

@mdempsky mdempsky removed this from the Go1.17 milestone Apr 29, 2021
@mdempsky mdempsky added this to the Go1.18 milestone Apr 29, 2021
@dr2chase
Copy link
Contributor

@dr2chase dr2chase commented Nov 11, 2021

It's a good idea, but has there been any progress on this? I.e., can we move it to the 1.19 milestone?

A "modest proposal" for how we might approach this in general -- if a function f takes a function-valued parameter g, bump the threshold for possibly inlining f (more detail -- if g is passed to f and f calls g). If (1) that new threshold puts f into the maybe-inline category and (2) an inlineable g is passed to f at a particular call site, then do the inlining.

@mknyszek
Copy link
Contributor

@mknyszek mknyszek commented Nov 12, 2021

Since there hasn't been any movement since 2020 at this point, I think it's fine to put this in the backlog, personally.

@dr2chase
Copy link
Contributor

@dr2chase dr2chase commented Nov 12, 2021

I'd rather re-milestone to 1.19; the hack I contrived above, might just work, likely to have low impact, and improving sort performance would float a lot of boats.

@dr2chase dr2chase removed this from the Go1.18 milestone Nov 12, 2021
@dr2chase dr2chase added this to the Go1.19 milestone Nov 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest NeedsFix Performance
Projects
None yet
Development

No branches or pull requests

4 participants