[インデックス 16024] ファイルの概要
このコミットは、Go言語の公式ドキュメントである doc/go1.1.html
の更新に関するものです。具体的には、Go 1.1 リリースノートのベンチマークに関するセクションに、(*testing.B).ReportAllocs()
メソッドに関する記述を追加し、ベンチマークにおけるメモリ割り当て統計の報告機能について言及しています。また、ドキュメント内の軽微なタイポ("aggresive" を "aggressive" に修正)も同時に修正されています。
コミット
commit 6e054190f77d467e27f3fe64c30661acdc15f02b
Author: Shenghou Ma <minux.ma@gmail.com>
Date: Sun Mar 31 02:17:25 2013 +0800
doc/go1.1: mention (*testing.B).ReportAllocs()
R=golang-dev, r
CC=golang-dev
https://golang.org/cl/8198043
GitHub上でのコミットページへのリンク
https://github.com/golang/go/commit/6e054190f77d467e27f3fe64c30661acdc15f02b
元コミット内容
doc/go1.1: mention (*testing.B).ReportAllocs()
変更の背景
このコミットは、Go 1.1 のリリースに向けて、新しい機能や改善点をまとめた公式ドキュメント doc/go1.1.html
を最新の状態に保つために行われました。特に、Go 1.1 では testing
パッケージにベンチマーク時のメモリ割り当て統計を自動生成する機能が導入されました。これには AllocsPerRun
関数や BenchmarkResult.AllocsPerOp
メソッドが含まれます。このコミットの目的は、これらの新しいベンチマーク機能の一部として追加された (*testing.B).ReportAllocs()
メソッドの存在を、Go 1.1 のリリースノートに明記することです。これにより、開発者がこの新しい機能の存在を認識し、ベンチマークの際にメモリ割り当てのパフォーマンスをより詳細に分析できるようになります。また、ドキュメントの品質向上の一環として、既存のタイポも修正されています。
前提知識の解説
Go言語のベンチマーク
Go言語には、標準ライブラリの testing
パッケージにベンチマーク機能が組み込まれています。これにより、開発者はコードのパフォーマンスを測定し、最適化のボトルネックを特定することができます。ベンチマーク関数は BenchmarkXxx(*testing.B)
というシグネチャを持ち、go test -bench=.
コマンドで実行されます。
testing.B
型
testing.B
はベンチマーク関数に渡される構造体で、ベンチマークの実行回数 (b.N
) や、タイマーの開始/停止 (b.StartTimer()
, b.StopTimer()
)、メモリ割り当ての計測 (b.ReportAllocs()
) など、ベンチマークの制御と報告のためのメソッドを提供します。
メモリ割り当て統計
プログラムのパフォーマンスを評価する上で、CPU時間だけでなくメモリの割り当ても重要な要素です。特に、頻繁なメモリ割り当てはガベージコレクションのオーバーヘッドを増加させ、アプリケーションのレイテンシやスループットに悪影響を与える可能性があります。Go 1.1 で導入されたベンチマークにおけるメモリ割り当て統計機能は、ベンチマーク実行中に発生したメモリ割り当ての回数やバイト数を測定し、報告することを可能にします。
(*testing.B).ReportAllocs()
メソッド
このメソッドは、ベンチマーク関数内で呼び出されると、そのベンチマークの実行中に発生したメモリ割り当ての統計を報告するように testing
パッケージに指示します。これにより、go test -bench=. -benchmem
コマンドでベンチマークを実行した際に、各操作あたりのメモリ割り当て回数 (allocs/op
) と割り当てバイト数 (B/op
) が出力されるようになります。
AllocsPerRun
関数
testing
パッケージの AllocsPerRun
関数は、特定の関数が実行されるたびに発生するメモリ割り当ての回数を測定するために使用されます。これは、ベンチマークのコンテキスト外で、より細かい粒度でメモリ割り当てを分析する際に役立ちます。
BenchmarkResult.AllocsPerOp
メソッド
BenchmarkResult
はベンチマークの結果を保持する構造体で、AllocsPerOp
メソッドは、ベンチマークの各操作あたりの平均メモリ割り当て回数を返します。
技術的詳細
このコミットは、Go 1.1 のリリースノートである doc/go1.1.html
ファイルを直接編集しています。変更内容は以下の2点です。
-
(*testing.B).ReportAllocs()
の追加記述:testing
パッケージの新しい機能として、ベンチマークにおけるメモリ割り当て統計の自動生成に関する記述が更新されました。既存のAllocsPerRun
関数とAllocsPerOp
メソッドの言及に加え、(*testing.B).ReportAllocs()
メソッドが追加されました。このメソッドは、ベンチマーク内で呼び出されることで、そのベンチマークのメモリ割り当て統計の出力を有効にする役割を担います。これにより、開発者はベンチマーク結果からメモリ使用効率に関する洞察を得ることができます。変更前:
The <a href="/pkg/testing/"><code>testing</code></a> package now automates the generation of allocation statistics in benchmarks using the new <a href="/pkg/testing/#AllocsPerRun"><code>AllocsPerRun</code></a> function and the
変更後:
The <a href="/pkg/testing/"><code>testing</code></a> package now automates the generation of allocation statistics in tests and benchmarks using the new <a href="/pkg/testing/#AllocsPerRun"><code>AllocsPerRun</code></a> function. And the <a href="/pkg/testing/#B.ReportAllocs"><code>ReportAllocs</code></a> method on <a href="/pkg/testing/#B"><code>testing.B</code></a> will enable printing of memory allocation statistics for the calling benchmark. It also introduces the
この変更により、
testing.B
のReportAllocs
メソッドが、呼び出し元のベンチマークのメモリ割り当て統計の出力を有効にすることが明確に記述されました。 -
タイポの修正:
Finally, the Transport is now more aggresive at closing TCP connections when
という文中の "aggresive" というスペルミスが "aggressive" に修正されました。これはドキュメントの品質向上を目的とした軽微な修正です。
これらの変更は、Go 1.1 の新機能に関するドキュメントの正確性と完全性を高めるためのものです。
コアとなるコードの変更箇所
このコミットで変更されたファイルは doc/go1.1.html
のみです。これはGo 1.1のリリースノートのHTMLドキュメントであり、Go言語のランタイムやライブラリのソースコード自体に変更はありません。
--- a/doc/go1.1.html
+++ b/doc/go1.1.html
@@ -828,7 +828,7 @@ The <code>ServeMux</code> type now has a
<code>Handler</code> without executing it.
The <code>Transport</code> can now cancel an in-flight request with
<a href="/pkg/net/http/#Transport.CancelRequest"><code>CancelRequest</code></a>.
-Finally, the Transport is now more aggresive at closing TCP connections when
+Finally, the Transport is now more aggressive at closing TCP connections when
a <a href="/pkg/net/http/#Response"><code>Response.Body</code></a> is closed before
being fully consumed.
</li>
@@ -917,8 +917,11 @@ The <a href="/pkg/syscall/"><code>syscall</code></a> package has received many u
<li>
The <a href="/pkg/testing/"><code>testing</code></a> package now automates the generation of allocation
-statistics in benchmarks using the new
-<a href="/pkg/testing/#AllocsPerRun"><code>AllocsPerRun</code></a> function and the
+statistics in tests and benchmarks using the new
+<a href="/pkg/testing/#AllocsPerRun"><code>AllocsPerRun</code></a> function. And the
+<a href="/pkg/testing/#B.ReportAllocs"><code>ReportAllocs</code></a>
+method on <a href="/pkg/testing/#B"><code>testing.B</code></a> will enable printing of
+memory allocation statistics for the calling benchmark. It also introduces the
<a href="/pkg/testing/#BenchmarkResult.AllocsPerOp"><code>AllocsPerOp</code></a> method of
<a href="/pkg/testing/#BenchmarkResult"><code>BenchmarkResult</code></a>.
There is also a new
コアとなるコードの解説
変更されたのはHTMLドキュメントのテキストコンテンツです。
-
行 828-831:
Finally, the Transport is now more aggresive at closing TCP connections when
がFinally, the Transport is now more aggressive at closing TCP connections when
に修正されています。これは単なるスペルミス("aggresive" -> "aggressive")の修正であり、機能的な変更ではありません。 -
行 917-924:
testing
パッケージに関する記述が拡張されています。 元々はAllocsPerRun
関数とAllocsPerOp
メソッドについてのみ言及されていましたが、この変更により(*testing.B).ReportAllocs()
メソッドが追加されました。 この追加は、Go 1.1 で導入されたベンチマークにおけるメモリ割り当て統計機能の完全な説明を意図しています。ReportAllocs()
メソッドは、ベンチマーク実行時にメモリ割り当ての統計情報を出力させるための重要なトリガーとなります。これにより、開発者はgo test -bench=. -benchmem
コマンドを実行した際に、ベンチマークのパフォーマンスだけでなく、メモリ使用量に関する詳細なデータも得られるようになります。
これらの変更は、Go 1.1 のリリースノートの正確性と網羅性を向上させるためのドキュメンタリーな修正です。
関連リンク
- Go 1.1 Release Notes (公式ドキュメント): このコミットが修正しているドキュメントの最終版。
- Go 1.1 のリリースノートは、Go言語の公式ウェブサイトで確認できます。
- Go
testing
パッケージのドキュメント:testing.B
やReportAllocs
などの詳細なAPIドキュメント。
参考にした情報源リンク
- Go言語の公式ドキュメント
- Go言語の
testing
パッケージのソースコードとドキュメント - Go言語のコミット履歴と関連するコードレビュー (CL: Change List)
- https://golang.org/cl/8198043 (コミットメッセージに記載されているCLへのリンク)