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

runtime: optimize for special case where two-return-value map access discards the value #12776

Closed
glittershark opened this issue Sep 28, 2015 · 2 comments

Comments

@glittershark
Copy link

A pattern that I'm coming up on reasonably frequently in my code is the following:

if _, ok := myMap[key]; ok {
    // do some stuff
}

It seems to me like there would be some (albeit small) gains to be had in the compiler detecting this particular case of discarding the value result of map-lookup in favor of only returning the boolean, and avoid doing the pointer arithmetic necessary to find that value.

@bradfitz
Copy link
Contributor

It's already optimized for in src/cmd/compile/internal/gc/walk.go:

                // don't generate a = *var if a is _                                                                                                                              
                if !isblank(a) {
                        var_ := temp(Ptrto(t.Type))
                        var_.Typecheck = 1
                        n.List.N = var_
                        walkexpr(&n, init)
                        *init = list(*init, n)
                        n = Nod(OAS, a, Nod(OIND, var_, nil))
                }

And the "pointer arithmetic necessary to find that value" has already been done at the point it's determined whether it's in the map or not.

There is perhaps an addition or multiplication that could be removed, but it's so minor compared to the work already done up to that point. And I imagine plumbing down a "skip this one addition and multiple" would be heavier than just doing it.

@minux
Copy link
Member

minux commented Sep 28, 2015 via email

@golang golang locked and limited conversation to collaborators Sep 27, 2016
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

4 participants