mirror of https://github.com/golang/go.git
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/1975
Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
parent
43c87aa481
commit
43e6923131
|
|
@ -794,8 +794,6 @@ walkexpr(Node **np, NodeList **init)
|
||||||
// var,b = mapaccess2*(t, m, i)
|
// var,b = mapaccess2*(t, m, i)
|
||||||
// a = *var
|
// a = *var
|
||||||
a = n->list->n;
|
a = n->list->n;
|
||||||
var = temp(ptrto(t->type));
|
|
||||||
var->typecheck = 1;
|
|
||||||
fn = mapfn(p, t);
|
fn = mapfn(p, t);
|
||||||
r = mkcall1(fn, getoutargx(fn->type), init, typename(t), r->left, key);
|
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;
|
r->type->type->down->type = n->list->next->n->type;
|
||||||
n->rlist = list1(r);
|
n->rlist = list1(r);
|
||||||
n->op = OAS2FUNC;
|
n->op = OAS2FUNC;
|
||||||
n->list->n = var;
|
|
||||||
walkexpr(&n, init);
|
// don't generate a = *var if a is _
|
||||||
*init = list(*init, n);
|
if(!isblank(a)) {
|
||||||
n = nod(OAS, a, nod(OIND, var, N));
|
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);
|
typecheck(&n, Etop);
|
||||||
walkexpr(&n, init);
|
walkexpr(&n, init);
|
||||||
// mapaccess needs a zero value to be at least this big.
|
// 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"
|
_ = &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
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue