From 1fbeccb15a56957919e087639e255b4df64062f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Wed, 27 Sep 2017 11:30:10 +0100 Subject: [PATCH] cmd/compile: deduplicate a few lines in swt.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Noticed while reading some code that the two branches in this loop body shared the last statements. Rewrite it in a way that they are not duplicated. Passes toolstash -cmp on std. Change-Id: I3356ca9fa37c32eee496e221d7830bfc581dade1 Reviewed-on: https://go-review.googlesource.com/66470 Run-TryBot: Daniel Martí TryBot-Result: Gobot Gobot Reviewed-by: Matthew Dempsky --- src/cmd/compile/internal/gc/swt.go | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/cmd/compile/internal/gc/swt.go b/src/cmd/compile/internal/gc/swt.go index 1f401f5356..8d425506d3 100644 --- a/src/cmd/compile/internal/gc/swt.go +++ b/src/cmd/compile/internal/gc/swt.go @@ -273,21 +273,15 @@ func (s *exprSwitch) walk(sw *Node) { // handle the cases in order for len(cc) > 0 { - // deal with expressions one at a time - if !okforcmp[t.Etype] || !cc[0].isconst { - a := s.walkCases(cc[:1]) - cas = append(cas, a) - cc = cc[1:] - continue + run := 1 + if okforcmp[t.Etype] && cc[0].isconst { + // do binary search on runs of constants + for ; run < len(cc) && cc[run].isconst; run++ { + } + // sort and compile constants + sort.Sort(caseClauseByConstVal(cc[:run])) } - // do binary search on runs of constants - var run int - for run = 1; run < len(cc) && cc[run].isconst; run++ { - } - - // sort and compile constants - sort.Sort(caseClauseByConstVal(cc[:run])) a := s.walkCases(cc[:run]) cas = append(cas, a) cc = cc[run:]