@@ -57,6 +57,12 @@ const (
5757// All data access is performed through transactions which can be obtained through the DB.
5858// All the functions on DB will return a ErrDatabaseNotOpen if accessed before Open() is called.
5959type DB struct {
60+ // Put `stats` at the first field to ensure it's 64-bit aligned. Note that
61+ // the first word in an allocated struct can be relied upon to be 64-bit
62+ // aligned. Refer to https://pkg.go.dev/sync/atomic#pkg-note-BUG. Also
63+ // refer to discussion in https://github.com/etcd-io/bbolt/issues/577.
64+ stats Stats
65+
6066 // When enabled, the database will perform a Check() after every commit.
6167 // A panic is issued if the database is in an inconsistent state. This
6268 // flag has a large performance impact so it should only be used for
@@ -147,7 +153,6 @@ type DB struct {
147153 opened bool
148154 rwtx * Tx
149155 txs []* Tx
150- stats Stats
151156
152157 freelist * freelist
153158 freelistLoad sync.Once
@@ -1282,6 +1287,12 @@ var DefaultOptions = &Options{
12821287
12831288// Stats represents statistics about the database.
12841289type Stats struct {
1290+ // Put `TxStats` at the first field to ensure it's 64-bit aligned. Note
1291+ // that the first word in an allocated struct can be relied upon to be
1292+ // 64-bit aligned. Refer to https://pkg.go.dev/sync/atomic#pkg-note-BUG.
1293+ // Also refer to discussion in https://github.com/etcd-io/bbolt/issues/577.
1294+ TxStats TxStats // global, ongoing stats.
1295+
12851296 // Freelist stats
12861297 FreePageN int // total number of free pages on the freelist
12871298 PendingPageN int // total number of pending pages on the freelist
@@ -1291,8 +1302,6 @@ type Stats struct {
12911302 // Transaction stats
12921303 TxN int // total number of started read transactions
12931304 OpenTxN int // number of currently open read transactions
1294-
1295- TxStats TxStats // global, ongoing stats.
12961305}
12971306
12981307// Sub calculates and returns the difference between two sets of database stats.
0 commit comments