Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

[インデックス 15903] ファイルの概要

このコミットは、Go言語の標準ライブラリおよびツールチェインから、到達不能なコード(デッドコード)を削除することを目的としています。具体的には、様々なパッケージに散在する冗長な return ステートメントや、論理的に到達しないはずの panic("unreachable") といったデバッグ/プレースホルダーコードが削除されています。また、テストの実行効率を向上させるための変更も含まれています。

コミット

build: remove dead code

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/7877045

GitHub上でのコミットページへのリンク

https://github.com/golang/go/commit/f6d0adedb663306af179b4f6e2d6745b49fe881f

元コミット内容

commit f6d0adedb663306af179b4f6e2d6745b49fe881f
Author: Russ Cox <rsc@golang.org>
Date:   Fri Mar 22 17:28:22 2013 -0400

    build: remove dead code
    
    R=golang-dev, r
    CC=golang-dev
    https://golang.org/cl/7877045
---
 src/cmd/go/discovery.go                   | 1 -
 src/cmd/yacc/yacc.go                      | 1 -
 src/pkg/compress/flate/inflate.go         | 1 -
 src/pkg/compress/gzip/gunzip.go           | 1 -
 src/pkg/compress/lzw/reader.go            | 1 -
 src/pkg/crypto/dsa/dsa_test.go            | 5 +++--
 src/pkg/crypto/rand/util.go               | 4 ----
 src/pkg/encoding/asn1/marshal.go          | 1 -
 src/pkg/encoding/csv/reader.go            | 2 --
 src/pkg/encoding/gob/decode.go            | 1 -
 src/pkg/encoding/gob/type.go              | 1 -
 src/pkg/encoding/json/decode.go           | 1 -
 src/pkg/html/template/transition.go       | 2 --
 src/pkg/math/big/nat.go                   | 2 --
 src/pkg/net/http/httputil/reverseproxy.go | 1 -
 src/pkg/net/ip.go                         | 1 -
 src/pkg/net/unixsock_posix.go             | 1 -
 src/pkg/os/file_posix.go                  | 2 --
 src/pkg/os/getwd.go                       | 2 --
 src/pkg/reflect/tostring_test.go          | 1 -
 src/pkg/strconv/extfloat.go               | 1 -
 src/pkg/syscall/exec_bsd.go               | 5 -----
 src/pkg/testing/quick/quick.go            | 2 --
 src/pkg/text/template/parse/parse.go      | 1 -
 24 files changed, 3 insertions(+), 38 deletions(-)

diff --git a/src/cmd/go/discovery.go b/src/cmd/go/discovery.go
index d9f930867e..0478340505 100644
--- a/src/cmd/go/discovery.go
+++ b/src/cmd/go/discovery.go
@@ -48,7 +48,6 @@ func parseMetaGoImports(r io.Reader) (imports []metaImport) {
 			})\n \t\t}\n \t}\n-\treturn\n }\n \n // attrValue returns the attribute value for the case-insensitive key
diff --git a/src/cmd/yacc/yacc.go b/src/cmd/yacc/yacc.go
index 0c18f93b6c..76b3aeac5d 100644
--- a/src/cmd/yacc/yacc.go
+++ b/src/cmd/yacc/yacc.go
@@ -3164,7 +3164,6 @@ func ungetrune(f *bufio.Reader, c rune) {
 
 func write(f *bufio.Writer, b []byte, n int) int {
 	panic("write")
-\treturn 0\n}\n \n func open(s string) *bufio.Reader {
diff --git a/src/pkg/compress/flate/inflate.go b/src/pkg/compress/flate/inflate.go
index 7fe1749631..beca34b4d8 100644
--- a/src/pkg/compress/flate/inflate.go
+++ b/src/pkg/compress/flate/inflate.go
@@ -640,7 +640,6 @@ func (f *decompressor) huffSym(h *huffmanDecoder) (int, error) {
 			return int(chunk >> huffmanValueShift), nil
 		}\n \t}\n-\treturn 0, CorruptInputError(f.roffset)\n}\n \n // Flush any buffered output to the underlying writer.
diff --git a/src/pkg/compress/gzip/gunzip.go b/src/pkg/compress/gzip/gunzip.go
index 33736f6350..1fb9b0964c 100644
--- a/src/pkg/compress/gzip/gunzip.go
+++ b/src/pkg/compress/gzip/gunzip.go
@@ -120,7 +120,6 @@ func (z *Reader) readString() (string, error) {
 			return string(z.buf[0:i]), nil
 		}\n \t}\n-\tpanic("not reached")\n}\n \n func (z *Reader) read2() (uint32, error) {
diff --git a/src/pkg/compress/lzw/reader.go b/src/pkg/compress/lzw/reader.go
index cf8ca7c788..efbc758f94 100644
--- a/src/pkg/compress/lzw/reader.go
+++ b/src/pkg/compress/lzw/reader.go
@@ -202,7 +202,6 @@ func (d *decoder) decode() {
 			return\n \t\t}\n \t}\n-\tpanic("unreachable")\n}\n \n func (d *decoder) flush() {
diff --git a/src/pkg/crypto/dsa/dsa_test.go b/src/pkg/crypto/dsa/dsa_test.go
index 177aa444df..568416d0df 100644
--- a/src/pkg/crypto/dsa/dsa_test.go
+++ b/src/pkg/crypto/dsa/dsa_test.go
@@ -63,8 +63,9 @@ func testParameterGeneration(t *testing.T, sizes ParameterSizes, L, N int) {
 }\n \n func TestParameterGeneration(t *testing.T) {
-\t// This test is too slow to run all the time.\n-\treturn\n+\tif testing.Short() {\n+\t\tt.Skip("skipping parameter generation test in short mode")\n+\t}\n \n \ttestParameterGeneration(t, L1024N160, 1024, 160)\n \ttestParameterGeneration(t, L2048N224, 2048, 224)
diff --git a/src/pkg/crypto/rand/util.go b/src/pkg/crypto/rand/util.go
index 50e5b162bd..21608dbaca 100644
--- a/src/pkg/crypto/rand/util.go
+++ b/src/pkg/crypto/rand/util.go
@@ -98,8 +98,6 @@ func Prime(rand io.Reader, bits int) (p *big.Int, err error) {
 			return\n \t\t}\n \t}\n-\n-\treturn\n}\n \n // Int returns a uniform random value in [0, max).
@@ -130,6 +128,4 @@ func Int(rand io.Reader, max *big.Int) (n *big.Int, err error) {
 			return\n \t\t}\n \t}\n-\n-\treturn\n}\ndiff --git a/src/pkg/encoding/asn1/marshal.go b/src/pkg/encoding/asn1/marshal.go
index 0c216fdb3c..adaf80dcdb 100644
--- a/src/pkg/encoding/asn1/marshal.go
+++ b/src/pkg/encoding/asn1/marshal.go
@@ -460,7 +460,6 @@ func marshalBody(out *forkableWriter, value reflect.Value, params fieldParameter
 		default:\n \t\t\treturn marshalUTF8String(out, v.String())\n \t\t}\n-\t\treturn\n}\n \n \treturn StructuralError{"unknown Go type"}
diff --git a/src/pkg/encoding/csv/reader.go b/src/pkg/encoding/csv/reader.go
index 336dd6540d..b099caf60a 100644
--- a/src/pkg/encoding/csv/reader.go
+++ b/src/pkg/encoding/csv/reader.go
@@ -212,7 +212,6 @@ func (r *Reader) skip(delim rune) error {
 			return nil\n \t\t}\n \t}\n-\tpanic("unreachable")\n}\n \n // parseRecord reads and parses a single csv record from r.
@@ -249,7 +248,6 @@ func (r *Reader) parseRecord() (fields []string, err error) {
 			return nil, err\n \t\t}\n \t}\n-\tpanic("unreachable")\n}\n \n // parseField parses the next field in the record.  The read field is
diff --git a/src/pkg/encoding/gob/decode.go b/src/pkg/encoding/gob/decode.go
index a80d9f9195..7cc7565409 100644
--- a/src/pkg/encoding/gob/decode.go
+++ b/src/pkg/encoding/gob/decode.go
@@ -1066,7 +1066,6 @@ func (dec *Decoder) compatibleType(fr reflect.Type, fw typeId, inProgress map[re
 	case reflect.Struct:\n \t\treturn true\n \t}\n-\treturn true\n}\n \n // typeString returns a human-readable description of the type identified by remoteId.
diff --git a/src/pkg/encoding/gob/type.go b/src/pkg/encoding/gob/type.go
index ea0db4eac4..7fa0b499f0 100644
--- a/src/pkg/encoding/gob/type.go
+++ b/src/pkg/encoding/gob/type.go
@@ -526,7 +526,6 @@ func newTypeObject(name string, ut *userTypeInfo, rt reflect.Type) (gobType, err
 	default:\n \t\treturn nil, errors.New("gob NewTypeObject can't handle type: " + rt.String())\n \t}\n-\treturn nil, nil\n}\n \n // isExported reports whether this is an exported - upper case - name.
diff --git a/src/pkg/encoding/json/decode.go b/src/pkg/encoding/json/decode.go
index 722037c309..62ac294b89 100644
--- a/src/pkg/encoding/json/decode.go
+++ b/src/pkg/encoding/json/decode.go
@@ -868,7 +868,6 @@ func (d *decodeState) literalInterface() interface{} {
 		}\n \t\treturn n\n \t}\n-\tpanic("unreachable")\n}\n \n // getu4 decodes \\uXXXX from the beginning of s, returning the hex value,
diff --git a/src/pkg/html/template/transition.go b/src/pkg/html/template/transition.go
index 2ea2089c6b..564eb20207 100644
--- a/src/pkg/html/template/transition.go
+++ b/src/pkg/html/template/transition.go
@@ -429,7 +429,6 @@ func tCSS(c context, s []byte) (context, int) {
 		}\n \t\tk = i + 1\n \t}\n-\tpanic("unreachable")\n}\n \n // tCSSStr is the context transition function for the CSS string and URL states.
@@ -470,7 +469,6 @@ func tCSSStr(c context, s []byte) (context, int) {
 	\tc, _ = tURL(c, decodeCSS(s[:i+1]))\n \t\tk = i + 1\n \t}\n-\tpanic("unreachable")\n}\n \n // tError is the context transition function for the error state.
diff --git a/src/pkg/math/big/nat.go b/src/pkg/math/big/nat.go
index 9d09f97b77..6874900d0b 100644
--- a/src/pkg/math/big/nat.go
+++ b/src/pkg/math/big/nat.go
@@ -1021,8 +1021,6 @@ func trailingZeroBits(x Word) uint {
 	default:\n \t\tpanic("unknown word size")\n \t}\n-\n-\treturn 0\n}\n \n // trailingZeroBits returns the number of consecutive least significant zero
diff --git a/src/pkg/net/http/httputil/reverseproxy.go b/src/pkg/net/http/httputil/reverseproxy.go
index 5099d973ff..1990f64dbd 100644
--- a/src/pkg/net/http/httputil/reverseproxy.go
+++ b/src/pkg/net/http/httputil/reverseproxy.go
@@ -202,7 +202,6 @@ func (m *maxLatencyWriter) flushLoop() {
 		\tm.lk.Unlock()\n \t\t}\n \t}\n-\tpanic("unreached")\n}\n \n func (m *maxLatencyWriter) stop() { m.done <- true }
diff --git a/src/pkg/net/ip.go b/src/pkg/net/ip.go
index aee4064161..b92b948784 100644
--- a/src/pkg/net/ip.go
+++ b/src/pkg/net/ip.go
@@ -222,7 +222,6 @@ func (ip IP) DefaultMask() IPMask {
 	default:\n \t\treturn classCMask\n \t}\n-\treturn nil // not reached\n}\n \n func allFF(b []byte) bool {
diff --git a/src/pkg/net/unixsock_posix.go b/src/pkg/net/unixsock_posix.go
index 6d6ce3f5e2..8d57dacbd6 100644
--- a/src/pkg/net/unixsock_posix.go
+++ b/src/pkg/net/unixsock_posix.go
@@ -99,7 +99,6 @@ func sotypeToNet(sotype int) string {
 	default:\n \t\tpanic("sotypeToNet unknown socket type")\n \t}\n-\treturn ""\n}\n \n // UnixConn is an implementation of the Conn interface for connections
diff --git a/src/pkg/os/file_posix.go b/src/pkg/os/file_posix.go
index b979fed97f..3df43feaa1 100644
--- a/src/pkg/os/file_posix.go
+++ b/src/pkg/os/file_posix.go
@@ -46,8 +46,6 @@ func Readlink(name string) (string, error) {
 			return string(b[0:n]), nil\n \t\t}\n \t}\n-\t// Silence 6g.\n-\treturn "", nil\n}\n \n // Rename renames a file.
diff --git a/src/pkg/os/getwd.go b/src/pkg/os/getwd.go
index 1b22123068..0235c5d779 100644
--- a/src/pkg/os/getwd.go
+++ b/src/pkg/os/getwd.go
@@ -90,8 +90,6 @@ func Getwd() (pwd string, err error) {
 				}\n \t\t\t}\n \t\t}\n-\t\tfd.Close()\n-\t\treturn "", ErrNotExist\n \n \tFound:\n \t\tpd, err := fd.Stat()
diff --git a/src/pkg/reflect/tostring_test.go b/src/pkg/reflect/tostring_test.go
index 7486a9bfca..e416fd84da 100644
--- a/src/pkg/reflect/tostring_test.go
+++ b/src/pkg/reflect/tostring_test.go
@@ -92,5 +92,4 @@ func valueToString(val Value) string {
 	default:\n \t\tpanic("valueToString: can't print type " + typ.String())\n \t}\n-\treturn "valueToString: can't happen"\n}\ndiff --git a/src/pkg/strconv/extfloat.go b/src/pkg/strconv/extfloat.go
index b7eaaa61bf..bed8b16bda 100644
--- a/src/pkg/strconv/extfloat.go
+++ b/src/pkg/strconv/extfloat.go
@@ -636,7 +636,6 @@ func (f *extFloat) ShortestDecimal(d *decimalSlice, lower, upper *extFloat) bool
 			\t1<<shift, multiplier*2)\n \t\t}\n \t}\n-\treturn false\n}\n \n // adjustLastDigit modifies d = x-currentDiff*ε, to get closest to
diff --git a/src/pkg/syscall/exec_bsd.go b/src/pkg/syscall/exec_bsd.go
index 318cbc060c..bc644d97a4 100644
--- a/src/pkg/syscall/exec_bsd.go
+++ b/src/pkg/syscall/exec_bsd.go
@@ -215,11 +215,6 @@ childerror:\n \tfor {\n \t\tRawSyscall(SYS_EXIT, 253, 0, 0)\n \t}\n-\n-\t// Calling panic is not actually safe,\n-\t// but the for loop above won't break\n-\t// and this shuts up the compiler.\n-\tpanic("unreached")\n}\n \n // Try to open a pipe with O_CLOEXEC set on both file descriptors.
diff --git a/src/pkg/testing/quick/quick.go b/src/pkg/testing/quick/quick.go
index 2427098228..761a6471b5 100644
--- a/src/pkg/testing/quick/quick.go
+++ b/src/pkg/testing/quick/quick.go
@@ -140,8 +140,6 @@ func Value(t reflect.Type, rand *rand.Rand) (value reflect.Value, ok bool) {
 	default:\n \t\treturn reflect.Value{}, false\n \t}\n-\n-\treturn\n}\n \n // A Config structure contains options for running a test.
diff --git a/src/pkg/text/template/parse/parse.go b/src/pkg/text/template/parse/parse.go
index 03bd22d95c..802e298c23 100644
--- a/src/pkg/text/template/parse/parse.go
+++ b/src/pkg/text/template/parse/parse.go
@@ -429,7 +429,6 @@ func (t *Tree) pipeline(context string) (pipe *PipeNode) {
 		\tt.unexpected(token, context)\n \t\t}\n \t}\n-\treturn\n}\n \n func (t *Tree) parseControl(context string) (pos Pos, line int, pipe *PipeNode, list, elseList *ListNode) {

変更の背景

このコミットの主な背景は、Go言語のコンパイラとランタイムの成熟に伴うコードベースのクリーンアップと最適化です。初期のGo言語開発段階では、特定のコードパスが論理的に到達しないことを明示するために panic("unreachable") のようなステートメントが挿入されたり、関数の最後に明示的な return が記述されたりすることがありました。しかし、コンパイラの最適化能力が向上し、これらのコードパスが実際に到達不能であることを静的に解析できるようになったため、これらの冗長なコードは不要になりました。

また、src/pkg/crypto/dsa/dsa_test.go の変更は、テストスイートの実行効率を向上させるためのものです。Goのテストフレームワークには、testing.Short() という関数があり、go test -short コマンドでテストを実行した場合に true を返します。これにより、開発者は時間のかかるテストをスキップし、より迅速なフィードバックを得ることができます。このコミットは、このようなテスト最適化のパターンを標準ライブラリのテストにも適用した例と言えます。

全体として、このコミットはGo言語のコードベースをより簡潔で効率的に保ち、ビルド時間やテスト実行時間の短縮に貢献することを目的としています。

前提知識の解説

  • デッドコード (Dead Code): プログラムの実行フローにおいて、決して実行されることのないコードブロックを指します。デッドコードは、プログラムのサイズを不必要に大きくし、可読性を低下させ、時には誤解を招く可能性があります。コンパイラは通常、デッドコードを検出して最終的なバイナリから削除する最適化を行います。
  • Go言語のコンパイラ: Go言語のコンパイラは、ソースコードを機械語に変換する際に様々な最適化を行います。これには、到達不能なコードの削除(デッドコードエリミネーション)も含まれます。コンパイラがコードパスが到達不能であることを確実に判断できる場合、そのコードは生成されるバイナリから除外されます。
  • panic ステートメント: Go言語における panic は、回復不能なエラーが発生した際にプログラムの実行を停止させるメカニズムです。通常、予期せぬ状態やプログラマの論理的誤りを示すために使用されます。panic("unreachable") のような記述は、開発者が「このコードは実行されるはずがない」という意図を明示するために一時的に挿入することがあります。
  • return ステートメント: Go言語の関数は、関数の最後に到達すると暗黙的に return します。したがって、関数の最後の行が return ステートメントである場合、その return は冗長となることがあります。
  • testing.Short(): Goの標準 testing パッケージが提供する関数です。go test -short コマンドでテストを実行すると true を返します。これにより、開発者は時間のかかるテスト(例: ネットワークアクセス、大規模なデータ処理、乱数生成に基づく統計的テストなど)を、通常のテスト実行時にはスキップし、CI/CD環境や完全なテスト実行時のみ実行するといった制御が可能になります。
  • Gerrit Change-ID (https://golang.org/cl/7877045): Goプロジェクトは、コードレビューにGerritというシステムを使用しています。golang.org/cl/ に続く番号は、Gerrit上の特定の変更セット(Change List)を指します。これは、このコミットがGerritを通じてレビューされ、承認されたことを示しています。

技術的詳細

このコミットで行われた変更は、主に以下の2つのカテゴリに分類できます。

  1. 到達不能なコードの削除:

    • 多くのファイルで panic("unreachable")panic("not reached") といったステートメントが削除されています。これらは、開発者が特定のコードパスが実行時に到達しないことを保証するために挿入したものです。Goコンパイラの静的解析能力が向上したことで、これらの panic が実際に到達不能であることがコンパイラによって認識され、コードベースから安全に削除できるようになったことを示唆しています。これにより、コンパイル後のバイナリサイズが削減され、コードのクリーンさが向上します。
    • 同様に、関数の最後に記述された冗長な return ステートメントも削除されています。Goでは、関数が最後に到達すると暗黙的に return されるため、明示的な return は不要です。これもコンパイラの最適化とコードの簡潔化の一環です。
    • src/pkg/os/file_posix.go// Silence 6g. というコメントと共に return "", nil が削除されている箇所は、かつてのGoコンパイラ(6gはGoの初期のコンパイラの一つ)が特定のコードパスで return がないことを警告していたため、その警告を抑制するために一時的に追加されていたコードが、コンパイラの改善により不要になったことを示しています。
  2. テストの条件付きスキップの導入:

    • src/pkg/crypto/dsa/dsa_test.go では、DSAパラメータ生成テストが testing.Short() を使用して条件付きでスキップされるように変更されました。このテストは「常に実行するには遅すぎる」とコメントされており、go test -short フラグが指定された場合にのみスキップされるようになります。これにより、開発者は日常的なテスト実行を高速化し、CI/CDパイプラインでは完全なテストを実行するといった柔軟な運用が可能になります。これは、大規模なプロジェクトにおけるテスト戦略の典型的な最適化手法です。

これらの変更は、Go言語の進化と成熟を示すものであり、コンパイラのインテリジェンス向上、コードベースの保守性向上、そして開発ワークフローの効率化に貢献しています。

コアとなるコードの変更箇所

このコミットは、Go標準ライブラリの広範なファイルにわたるデッドコードの削除を含んでいます。以下に主要な変更箇所を抜粋して示します。

  • src/cmd/go/discovery.go:

    --- a/src/cmd/go/discovery.go
    +++ b/src/cmd/go/discovery.go
    @@ -48,7 +48,6 @@ func parseMetaGoImports(r io.Reader) (imports []metaImport) {
     			})\n \t\t}\n \t}\n-\treturn\n }\n 
    

    関数の最後にあった冗長な return が削除されました。

  • src/cmd/yacc/yacc.go:

    --- a/src/cmd/yacc/yacc.go
    +++ b/src/cmd/yacc/yacc.go
    @@ -3164,7 +3164,6 @@ func ungetrune(f *bufio.Reader, c rune) {
     
     func write(f *bufio.Writer, b []byte, n int) int {
     	panic("write")
    -\treturn 0\n}\n 
    

    panic の後に続く到達不能な return 0 が削除されました。

  • src/pkg/compress/gzip/gunzip.go:

    --- a/src/pkg/compress/gzip/gunzip.go
    +++ b/src/pkg/compress/gzip/gunzip.go
    @@ -120,7 +120,6 @@ func (z *Reader) readString() (string, error) {
     			return string(z.buf[0:i]), nil
     		}\n \t}\n-\tpanic("not reached")\n}\n 
    

    panic("not reached") という到達不能な panic が削除されました。

  • src/pkg/crypto/dsa/dsa_test.go:

    --- a/src/pkg/crypto/dsa/dsa_test.go
    +++ b/src/pkg/crypto/dsa/dsa_test.go
    @@ -63,8 +63,9 @@ func testParameterGeneration(t *testing.T, sizes ParameterSizes, L, N int) {
     }\n \n func TestParameterGeneration(t *testing.T) {
    -\t// This test is too slow to run all the time.\n-\treturn\n+\tif testing.Short() {\n+\t\tt.Skip("skipping parameter generation test in short mode")\n+\t}\n 
     \ttestParameterGeneration(t, L1024N160, 1024, 160)\n \ttestParameterGeneration(t, L2048N224, 2048, 224)
    

    テストが testing.Short() モードでスキップされるように変更されました。

  • src/pkg/crypto/rand/util.go:

    --- a/src/pkg/crypto/rand/util.go
    +++ b/src/pkg/crypto/rand/util.go
    @@ -98,8 +98,6 @@ func Prime(rand io.Reader, bits int) (p *big.Int, err error) {
     			return\n \t\t}\n \t}\n-\n-\treturn\n}\n 
    

    冗長な return が削除されました。

  • src/pkg/syscall/exec_bsd.go:

    --- a/src/pkg/syscall/exec_bsd.go
    +++ b/src/pkg/syscall/exec_bsd.go
    @@ -215,11 +215,6 @@ childerror:\n \tfor {\n \t\tRawSyscall(SYS_EXIT, 253, 0, 0)\n \t}\n-\n-\t// Calling panic is not actually safe,\n-\t// but the for loop above won't break\n-\t// and this shuts up the compiler.\n-\tpanic("unreached")\n}\n 
    

    無限ループの後に続く到達不能な panic("unreached") とそのコメントが削除されました。

コアとなるコードの解説

このコミットで削除されたコードのほとんどは、Goコンパイラの最適化能力の向上によって不要になったものです。

  • 冗長な return ステートメントの削除: Go言語では、関数が最後に到達した場合、明示的な return ステートメントがなくても、定義された戻り値があればそのゼロ値(または名前付き戻り値変数の現在の値)を返して関数を終了します。例えば、src/cmd/go/discovery.gosrc/pkg/crypto/rand/util.go のように、関数の最後の行が単に return であった場合、それは冗長であり、コンパイラが自動的に処理するため削除されました。

  • panic("unreachable") などの削除: src/pkg/compress/gzip/gunzip.gosrc/pkg/encoding/csv/reader.go など、多くのファイルで panic("unreachable")panic("not reached") といったコードが削除されています。これらの panic は、開発者がコードを書いた時点では、そのコードパスが実行されることは「ありえない」という論理的な保証を示すために挿入されたものです。Goコンパイラがより洗練され、静的解析によってこれらのパスが本当に到達不能であることを証明できるようになったため、これらの panic は不要となり、コードベースから取り除かれました。これにより、コンパイルされたバイナリから不要な命令が削減され、わずかながらパフォーマンス向上やバイナリサイズの縮小に寄与します。

  • src/pkg/syscall/exec_bsd.go の変更: このファイルでは、childerror: ラベルの後に無限ループ for { RawSyscall(SYS_EXIT, 253, 0, 0) } が存在します。このループは、子プロセスがエラー状態になった場合にシステムコール SYS_EXIT を繰り返し呼び出してプロセスを終了させようとするものです。この無限ループの後に panic("unreached") が存在していましたが、これは論理的に到達不能であるため削除されました。コメント // Calling panic is not actually safe, // but the for loop above won't break // and this shuts up the compiler. は、かつてコンパイラが到達不能なコードパスについて警告を発していたため、その警告を抑制するために panic が置かれていたことを示唆しています。コンパイラの改善により、このような「警告抑制のためのデッドコード」も不要になったわけです。

  • src/pkg/crypto/dsa/dsa_test.go のテスト最適化: この変更は、デッドコードの削除とは少し異なりますが、ビルドとテストの効率化という点で関連しています。DSAパラメータ生成テストは計算コストが高く、毎回実行するとテストスイート全体の時間が長くなります。if testing.Short() { t.Skip(...) } というイディオムを導入することで、開発者は go test -short コマンドを使用して、この時間のかかるテストをスキップし、より迅速なテストフィードバックを得られるようになりました。これは、大規模なGoプロジェクトでテスト時間を管理するための一般的なプラクティスです。

これらの変更は、Go言語のコードベースが継続的に改善され、よりクリーンで効率的になるようにメンテナンスされていることを示しています。

関連リンク

参考にした情報源リンク