Commit 43e69231 authored by Josh Bleecher Snyder's avatar Josh Bleecher Snyder

cmd/gc: optimize existence-only map lookups

The compiler converts 'val, ok = m[key]' to

        tmp, ok = <runtime call>
        val = *tmp

For lookups of the form '_, ok = m[key]',
the second statement is unnecessary.
By not generating it we save a nil check.

Change-Id: I21346cc195cb3c62e041af8b18770c0940358695
Reviewed-on: https://go-review.googlesource.com/1975Reviewed-by: default avatarRuss Cox <rsc@golang.org>
parent 43c87aa4
......@@ -794,8 +794,6 @@ walkexpr(Node **np, NodeList **init)
// var,b = mapaccess2*(t, m, i)
// a = *var
a = n->list->n;
var = temp(ptrto(t->type));
var->typecheck = 1;
fn = mapfn(p, t);
r = mkcall1(fn, getoutargx(fn->type), init, typename(t), r->left, key);
......@@ -806,10 +804,17 @@ walkexpr(Node **np, NodeList **init)
r->type->type->down->type = n->list->next->n->type;
n->rlist = list1(r);
n->op = OAS2FUNC;
// 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, N));
}
typecheck(&n, Etop);
walkexpr(&n, init);
// mapaccess needs a zero value to be at least this big.
......
......@@ -182,3 +182,8 @@ func f4(x *[10]int) {
_ = &x[9] // ERROR "nil check"
}
func f5(m map[string]struct{}) bool {
// Existence-only map lookups should not generate a nil check
_, ok := m[""]
return ok
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment