]> Cypherpunks repositories - gostls13.git/commit
cmd/compile: avoid extra mapaccess in "m[k] op= r"
authorVladimir Kuzmin <vkuzmin@uber.com>
Fri, 2 Feb 2018 05:33:56 +0000 (21:33 -0800)
committerMatthew Dempsky <mdempsky@google.com>
Mon, 12 Mar 2018 19:27:44 +0000 (19:27 +0000)
commit7395083136539331537d46875ab9d196797a2173
tree1442dc0384c3d7916339e84d296d22f554985373
parent85a8d25d535a9b70f6c908e44f8558c207366ff1
cmd/compile: avoid extra mapaccess in "m[k] op= r"

Currently, order desugars map assignment operations like

    m[k] op= r

into

    m[k] = m[k] op r

which in turn is transformed during walk into:

    tmp := *mapaccess(m, k)
    tmp = tmp op r
    *mapassign(m, k) = tmp

However, this is suboptimal, as we could instead produce just:

    *mapassign(m, k) op= r

One complication though is if "r == 0", then "m[k] /= r" and "m[k] %=
r" will panic, and they need to do so *before* calling mapassign,
otherwise we may insert a new zero-value element into the map.

It would be spec compliant to just emit the "r != 0" check before
calling mapassign (see #23735), but currently these checks aren't
generated until SSA construction. For now, it's simpler to continue
desugaring /= and %= into two map indexing operations.

Fixes #23661.

Change-Id: I46e3739d9adef10e92b46fdd78b88d5aabe68952
Reviewed-on: https://go-review.googlesource.com/91557
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
src/cmd/compile/internal/gc/order.go
src/cmd/compile/internal/gc/walk.go
src/runtime/map_test.go
test/fixedbugs/issue19359.go
test/fixedbugs/issue22881.go