iterator_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. // Copyright 2019 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package snapshot
  17. import (
  18. "bytes"
  19. "encoding/binary"
  20. "fmt"
  21. "math/rand"
  22. "testing"
  23. "github.com/VictoriaMetrics/fastcache"
  24. "github.com/ethereum/go-ethereum/common"
  25. "github.com/ethereum/go-ethereum/core/rawdb"
  26. )
  27. // TestIteratorBasics tests some simple single-layer iteration
  28. func TestIteratorBasics(t *testing.T) {
  29. var (
  30. accounts = make(map[common.Hash][]byte)
  31. storage = make(map[common.Hash]map[common.Hash][]byte)
  32. )
  33. // Fill up a parent
  34. for i := 0; i < 100; i++ {
  35. h := randomHash()
  36. data := randomAccount()
  37. accounts[h] = data
  38. if rand.Intn(20) < 10 {
  39. accStorage := make(map[common.Hash][]byte)
  40. value := make([]byte, 32)
  41. rand.Read(value)
  42. accStorage[randomHash()] = value
  43. storage[h] = accStorage
  44. }
  45. }
  46. // Add some (identical) layers on top
  47. parent := newDiffLayer(emptyLayer(), common.Hash{}, accounts, storage)
  48. it := parent.AccountIterator(common.Hash{})
  49. verifyIterator(t, 100, it)
  50. }
  51. type testIterator struct {
  52. values []byte
  53. }
  54. func newTestIterator(values ...byte) *testIterator {
  55. return &testIterator{values}
  56. }
  57. func (ti *testIterator) Seek(common.Hash) {
  58. panic("implement me")
  59. }
  60. func (ti *testIterator) Next() bool {
  61. ti.values = ti.values[1:]
  62. if len(ti.values) == 0 {
  63. return false
  64. }
  65. return true
  66. }
  67. func (ti *testIterator) Error() error {
  68. panic("implement me")
  69. }
  70. func (ti *testIterator) Hash() common.Hash {
  71. return common.BytesToHash([]byte{ti.values[0]})
  72. }
  73. func (ti *testIterator) Account() []byte {
  74. panic("implement me")
  75. }
  76. func (ti *testIterator) Release() {}
  77. func TestFastIteratorBasics(t *testing.T) {
  78. type testCase struct {
  79. lists [][]byte
  80. expKeys []byte
  81. }
  82. for i, tc := range []testCase{
  83. {lists: [][]byte{{0, 1, 8}, {1, 2, 8}, {2, 9}, {4},
  84. {7, 14, 15}, {9, 13, 15, 16}},
  85. expKeys: []byte{0, 1, 2, 4, 7, 8, 9, 13, 14, 15, 16}},
  86. {lists: [][]byte{{0, 8}, {1, 2, 8}, {7, 14, 15}, {8, 9},
  87. {9, 10}, {10, 13, 15, 16}},
  88. expKeys: []byte{0, 1, 2, 7, 8, 9, 10, 13, 14, 15, 16}},
  89. } {
  90. var iterators []*weightedAccountIterator
  91. for i, data := range tc.lists {
  92. it := newTestIterator(data...)
  93. iterators = append(iterators, &weightedAccountIterator{it, i})
  94. }
  95. fi := &fastAccountIterator{
  96. iterators: iterators,
  97. initiated: false,
  98. }
  99. count := 0
  100. for fi.Next() {
  101. if got, exp := fi.Hash()[31], tc.expKeys[count]; exp != got {
  102. t.Errorf("tc %d, [%d]: got %d exp %d", i, count, got, exp)
  103. }
  104. count++
  105. }
  106. }
  107. }
  108. func verifyIterator(t *testing.T, expCount int, it AccountIterator) {
  109. t.Helper()
  110. var (
  111. count = 0
  112. last = common.Hash{}
  113. )
  114. for it.Next() {
  115. if hash := it.Hash(); bytes.Compare(last[:], hash[:]) >= 0 {
  116. t.Errorf("wrong order: %x >= %x", last, hash)
  117. }
  118. count++
  119. }
  120. if count != expCount {
  121. t.Errorf("iterator count mismatch: have %d, want %d", count, expCount)
  122. }
  123. if err := it.Error(); err != nil {
  124. t.Errorf("iterator failed: %v", err)
  125. }
  126. }
  127. // TestIteratorTraversal tests some simple multi-layer iteration.
  128. func TestIteratorTraversal(t *testing.T) {
  129. // Create an empty base layer and a snapshot tree out of it
  130. base := &diskLayer{
  131. diskdb: rawdb.NewMemoryDatabase(),
  132. root: common.HexToHash("0x01"),
  133. cache: fastcache.New(1024 * 500),
  134. }
  135. snaps := &Tree{
  136. layers: map[common.Hash]snapshot{
  137. base.root: base,
  138. },
  139. }
  140. // Stack three diff layers on top with various overlaps
  141. snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"),
  142. randomAccountSet("0xaa", "0xee", "0xff", "0xf0"), nil)
  143. snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"),
  144. randomAccountSet("0xbb", "0xdd", "0xf0"), nil)
  145. snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"),
  146. randomAccountSet("0xcc", "0xf0", "0xff"), nil)
  147. // Verify the single and multi-layer iterators
  148. head := snaps.Snapshot(common.HexToHash("0x04"))
  149. verifyIterator(t, 3, head.(snapshot).AccountIterator(common.Hash{}))
  150. verifyIterator(t, 7, head.(*diffLayer).newBinaryAccountIterator())
  151. it, _ := snaps.AccountIterator(common.HexToHash("0x04"), common.Hash{})
  152. defer it.Release()
  153. verifyIterator(t, 7, it)
  154. }
  155. // TestIteratorTraversalValues tests some multi-layer iteration, where we
  156. // also expect the correct values to show up.
  157. func TestIteratorTraversalValues(t *testing.T) {
  158. // Create an empty base layer and a snapshot tree out of it
  159. base := &diskLayer{
  160. diskdb: rawdb.NewMemoryDatabase(),
  161. root: common.HexToHash("0x01"),
  162. cache: fastcache.New(1024 * 500),
  163. }
  164. snaps := &Tree{
  165. layers: map[common.Hash]snapshot{
  166. base.root: base,
  167. },
  168. }
  169. // Create a batch of account sets to seed subsequent layers with
  170. var (
  171. a = make(map[common.Hash][]byte)
  172. b = make(map[common.Hash][]byte)
  173. c = make(map[common.Hash][]byte)
  174. d = make(map[common.Hash][]byte)
  175. e = make(map[common.Hash][]byte)
  176. f = make(map[common.Hash][]byte)
  177. g = make(map[common.Hash][]byte)
  178. h = make(map[common.Hash][]byte)
  179. )
  180. for i := byte(2); i < 0xff; i++ {
  181. a[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 0, i))
  182. if i > 20 && i%2 == 0 {
  183. b[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 1, i))
  184. }
  185. if i%4 == 0 {
  186. c[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 2, i))
  187. }
  188. if i%7 == 0 {
  189. d[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 3, i))
  190. }
  191. if i%8 == 0 {
  192. e[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 4, i))
  193. }
  194. if i > 50 || i < 85 {
  195. f[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 5, i))
  196. }
  197. if i%64 == 0 {
  198. g[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 6, i))
  199. }
  200. if i%128 == 0 {
  201. h[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 7, i))
  202. }
  203. }
  204. // Assemble a stack of snapshots from the account layers
  205. snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), a, nil)
  206. snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), b, nil)
  207. snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), c, nil)
  208. snaps.Update(common.HexToHash("0x05"), common.HexToHash("0x04"), d, nil)
  209. snaps.Update(common.HexToHash("0x06"), common.HexToHash("0x05"), e, nil)
  210. snaps.Update(common.HexToHash("0x07"), common.HexToHash("0x06"), f, nil)
  211. snaps.Update(common.HexToHash("0x08"), common.HexToHash("0x07"), g, nil)
  212. snaps.Update(common.HexToHash("0x09"), common.HexToHash("0x08"), h, nil)
  213. it, _ := snaps.AccountIterator(common.HexToHash("0x09"), common.Hash{})
  214. defer it.Release()
  215. head := snaps.Snapshot(common.HexToHash("0x09"))
  216. for it.Next() {
  217. hash := it.Hash()
  218. want, err := head.AccountRLP(hash)
  219. if err != nil {
  220. t.Fatalf("failed to retrieve expected account: %v", err)
  221. }
  222. if have := it.Account(); !bytes.Equal(want, have) {
  223. t.Fatalf("hash %x: account mismatch: have %x, want %x", hash, have, want)
  224. }
  225. }
  226. }
  227. // This testcase is notorious, all layers contain the exact same 200 accounts.
  228. func TestIteratorLargeTraversal(t *testing.T) {
  229. // Create a custom account factory to recreate the same addresses
  230. makeAccounts := func(num int) map[common.Hash][]byte {
  231. accounts := make(map[common.Hash][]byte)
  232. for i := 0; i < num; i++ {
  233. h := common.Hash{}
  234. binary.BigEndian.PutUint64(h[:], uint64(i+1))
  235. accounts[h] = randomAccount()
  236. }
  237. return accounts
  238. }
  239. // Build up a large stack of snapshots
  240. base := &diskLayer{
  241. diskdb: rawdb.NewMemoryDatabase(),
  242. root: common.HexToHash("0x01"),
  243. cache: fastcache.New(1024 * 500),
  244. }
  245. snaps := &Tree{
  246. layers: map[common.Hash]snapshot{
  247. base.root: base,
  248. },
  249. }
  250. for i := 1; i < 128; i++ {
  251. snaps.Update(common.HexToHash(fmt.Sprintf("0x%02x", i+1)), common.HexToHash(fmt.Sprintf("0x%02x", i)), makeAccounts(200), nil)
  252. }
  253. // Iterate the entire stack and ensure everything is hit only once
  254. head := snaps.Snapshot(common.HexToHash("0x80"))
  255. verifyIterator(t, 200, head.(snapshot).AccountIterator(common.Hash{}))
  256. verifyIterator(t, 200, head.(*diffLayer).newBinaryAccountIterator())
  257. it, _ := snaps.AccountIterator(common.HexToHash("0x80"), common.Hash{})
  258. defer it.Release()
  259. verifyIterator(t, 200, it)
  260. }
  261. // TestIteratorFlattening tests what happens when we
  262. // - have a live iterator on child C (parent C1 -> C2 .. CN)
  263. // - flattens C2 all the way into CN
  264. // - continues iterating
  265. func TestIteratorFlattening(t *testing.T) {
  266. // Create an empty base layer and a snapshot tree out of it
  267. base := &diskLayer{
  268. diskdb: rawdb.NewMemoryDatabase(),
  269. root: common.HexToHash("0x01"),
  270. cache: fastcache.New(1024 * 500),
  271. }
  272. snaps := &Tree{
  273. layers: map[common.Hash]snapshot{
  274. base.root: base,
  275. },
  276. }
  277. // Create a stack of diffs on top
  278. snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"),
  279. randomAccountSet("0xaa", "0xee", "0xff", "0xf0"), nil)
  280. snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"),
  281. randomAccountSet("0xbb", "0xdd", "0xf0"), nil)
  282. snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"),
  283. randomAccountSet("0xcc", "0xf0", "0xff"), nil)
  284. // Create an iterator and flatten the data from underneath it
  285. it, _ := snaps.AccountIterator(common.HexToHash("0x04"), common.Hash{})
  286. defer it.Release()
  287. if err := snaps.Cap(common.HexToHash("0x04"), 1); err != nil {
  288. t.Fatalf("failed to flatten snapshot stack: %v", err)
  289. }
  290. //verifyIterator(t, 7, it)
  291. }
  292. func TestIteratorSeek(t *testing.T) {
  293. // Create a snapshot stack with some initial data
  294. base := &diskLayer{
  295. diskdb: rawdb.NewMemoryDatabase(),
  296. root: common.HexToHash("0x01"),
  297. cache: fastcache.New(1024 * 500),
  298. }
  299. snaps := &Tree{
  300. layers: map[common.Hash]snapshot{
  301. base.root: base,
  302. },
  303. }
  304. snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"),
  305. randomAccountSet("0xaa", "0xee", "0xff", "0xf0"), nil)
  306. snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"),
  307. randomAccountSet("0xbb", "0xdd", "0xf0"), nil)
  308. snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"),
  309. randomAccountSet("0xcc", "0xf0", "0xff"), nil)
  310. // Construct various iterators and ensure their tranversal is correct
  311. it, _ := snaps.AccountIterator(common.HexToHash("0x02"), common.HexToHash("0xdd"))
  312. defer it.Release()
  313. verifyIterator(t, 3, it) // expected: ee, f0, ff
  314. it, _ = snaps.AccountIterator(common.HexToHash("0x02"), common.HexToHash("0xaa"))
  315. defer it.Release()
  316. verifyIterator(t, 3, it) // expected: ee, f0, ff
  317. it, _ = snaps.AccountIterator(common.HexToHash("0x02"), common.HexToHash("0xff"))
  318. defer it.Release()
  319. verifyIterator(t, 0, it) // expected: nothing
  320. it, _ = snaps.AccountIterator(common.HexToHash("0x04"), common.HexToHash("0xbb"))
  321. defer it.Release()
  322. verifyIterator(t, 5, it) // expected: cc, dd, ee, f0, ff
  323. it, _ = snaps.AccountIterator(common.HexToHash("0x04"), common.HexToHash("0xef"))
  324. defer it.Release()
  325. verifyIterator(t, 2, it) // expected: f0, ff
  326. it, _ = snaps.AccountIterator(common.HexToHash("0x04"), common.HexToHash("0xf0"))
  327. defer it.Release()
  328. verifyIterator(t, 1, it) // expected: ff
  329. it, _ = snaps.AccountIterator(common.HexToHash("0x04"), common.HexToHash("0xff"))
  330. defer it.Release()
  331. verifyIterator(t, 0, it) // expected: nothing
  332. }
  333. // BenchmarkIteratorTraversal is a bit a bit notorious -- all layers contain the
  334. // exact same 200 accounts. That means that we need to process 2000 items, but
  335. // only spit out 200 values eventually.
  336. //
  337. // The value-fetching benchmark is easy on the binary iterator, since it never has to reach
  338. // down at any depth for retrieving the values -- all are on the toppmost layer
  339. //
  340. // BenchmarkIteratorTraversal/binary_iterator_keys-6 2239 483674 ns/op
  341. // BenchmarkIteratorTraversal/binary_iterator_values-6 2403 501810 ns/op
  342. // BenchmarkIteratorTraversal/fast_iterator_keys-6 1923 677966 ns/op
  343. // BenchmarkIteratorTraversal/fast_iterator_values-6 1741 649967 ns/op
  344. func BenchmarkIteratorTraversal(b *testing.B) {
  345. // Create a custom account factory to recreate the same addresses
  346. makeAccounts := func(num int) map[common.Hash][]byte {
  347. accounts := make(map[common.Hash][]byte)
  348. for i := 0; i < num; i++ {
  349. h := common.Hash{}
  350. binary.BigEndian.PutUint64(h[:], uint64(i+1))
  351. accounts[h] = randomAccount()
  352. }
  353. return accounts
  354. }
  355. // Build up a large stack of snapshots
  356. base := &diskLayer{
  357. diskdb: rawdb.NewMemoryDatabase(),
  358. root: common.HexToHash("0x01"),
  359. cache: fastcache.New(1024 * 500),
  360. }
  361. snaps := &Tree{
  362. layers: map[common.Hash]snapshot{
  363. base.root: base,
  364. },
  365. }
  366. for i := 1; i <= 100; i++ {
  367. snaps.Update(common.HexToHash(fmt.Sprintf("0x%02x", i+1)), common.HexToHash(fmt.Sprintf("0x%02x", i)), makeAccounts(200), nil)
  368. }
  369. // We call this once before the benchmark, so the creation of
  370. // sorted accountlists are not included in the results.
  371. head := snaps.Snapshot(common.HexToHash("0x65"))
  372. head.(*diffLayer).newBinaryAccountIterator()
  373. b.Run("binary iterator keys", func(b *testing.B) {
  374. for i := 0; i < b.N; i++ {
  375. got := 0
  376. it := head.(*diffLayer).newBinaryAccountIterator()
  377. for it.Next() {
  378. got++
  379. }
  380. if exp := 200; got != exp {
  381. b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
  382. }
  383. }
  384. })
  385. b.Run("binary iterator values", func(b *testing.B) {
  386. for i := 0; i < b.N; i++ {
  387. got := 0
  388. it := head.(*diffLayer).newBinaryAccountIterator()
  389. for it.Next() {
  390. got++
  391. head.(*diffLayer).accountRLP(it.Hash(), 0)
  392. }
  393. if exp := 200; got != exp {
  394. b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
  395. }
  396. }
  397. })
  398. b.Run("fast iterator keys", func(b *testing.B) {
  399. for i := 0; i < b.N; i++ {
  400. it, _ := snaps.AccountIterator(common.HexToHash("0x65"), common.Hash{})
  401. defer it.Release()
  402. got := 0
  403. for it.Next() {
  404. got++
  405. }
  406. if exp := 200; got != exp {
  407. b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
  408. }
  409. }
  410. })
  411. b.Run("fast iterator values", func(b *testing.B) {
  412. for i := 0; i < b.N; i++ {
  413. it, _ := snaps.AccountIterator(common.HexToHash("0x65"), common.Hash{})
  414. defer it.Release()
  415. got := 0
  416. for it.Next() {
  417. got++
  418. it.Account()
  419. }
  420. if exp := 200; got != exp {
  421. b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
  422. }
  423. }
  424. })
  425. }
  426. // BenchmarkIteratorLargeBaselayer is a pretty realistic benchmark, where
  427. // the baselayer is a lot larger than the upper layer.
  428. //
  429. // This is heavy on the binary iterator, which in most cases will have to
  430. // call recursively 100 times for the majority of the values
  431. //
  432. // BenchmarkIteratorLargeBaselayer/binary_iterator_(keys)-6 514 1971999 ns/op
  433. // BenchmarkIteratorLargeBaselayer/binary_iterator_(values)-6 61 18997492 ns/op
  434. // BenchmarkIteratorLargeBaselayer/fast_iterator_(keys)-6 10000 114385 ns/op
  435. // BenchmarkIteratorLargeBaselayer/fast_iterator_(values)-6 4047 296823 ns/op
  436. func BenchmarkIteratorLargeBaselayer(b *testing.B) {
  437. // Create a custom account factory to recreate the same addresses
  438. makeAccounts := func(num int) map[common.Hash][]byte {
  439. accounts := make(map[common.Hash][]byte)
  440. for i := 0; i < num; i++ {
  441. h := common.Hash{}
  442. binary.BigEndian.PutUint64(h[:], uint64(i+1))
  443. accounts[h] = randomAccount()
  444. }
  445. return accounts
  446. }
  447. // Build up a large stack of snapshots
  448. base := &diskLayer{
  449. diskdb: rawdb.NewMemoryDatabase(),
  450. root: common.HexToHash("0x01"),
  451. cache: fastcache.New(1024 * 500),
  452. }
  453. snaps := &Tree{
  454. layers: map[common.Hash]snapshot{
  455. base.root: base,
  456. },
  457. }
  458. snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), makeAccounts(2000), nil)
  459. for i := 2; i <= 100; i++ {
  460. snaps.Update(common.HexToHash(fmt.Sprintf("0x%02x", i+1)), common.HexToHash(fmt.Sprintf("0x%02x", i)), makeAccounts(20), nil)
  461. }
  462. // We call this once before the benchmark, so the creation of
  463. // sorted accountlists are not included in the results.
  464. head := snaps.Snapshot(common.HexToHash("0x65"))
  465. head.(*diffLayer).newBinaryAccountIterator()
  466. b.Run("binary iterator (keys)", func(b *testing.B) {
  467. for i := 0; i < b.N; i++ {
  468. got := 0
  469. it := head.(*diffLayer).newBinaryAccountIterator()
  470. for it.Next() {
  471. got++
  472. }
  473. if exp := 2000; got != exp {
  474. b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
  475. }
  476. }
  477. })
  478. b.Run("binary iterator (values)", func(b *testing.B) {
  479. for i := 0; i < b.N; i++ {
  480. got := 0
  481. it := head.(*diffLayer).newBinaryAccountIterator()
  482. for it.Next() {
  483. got++
  484. v := it.Hash()
  485. head.(*diffLayer).accountRLP(v, 0)
  486. }
  487. if exp := 2000; got != exp {
  488. b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
  489. }
  490. }
  491. })
  492. b.Run("fast iterator (keys)", func(b *testing.B) {
  493. for i := 0; i < b.N; i++ {
  494. it, _ := snaps.AccountIterator(common.HexToHash("0x65"), common.Hash{})
  495. defer it.Release()
  496. got := 0
  497. for it.Next() {
  498. got++
  499. }
  500. if exp := 2000; got != exp {
  501. b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
  502. }
  503. }
  504. })
  505. b.Run("fast iterator (values)", func(b *testing.B) {
  506. for i := 0; i < b.N; i++ {
  507. it, _ := snaps.AccountIterator(common.HexToHash("0x65"), common.Hash{})
  508. defer it.Release()
  509. got := 0
  510. for it.Next() {
  511. it.Account()
  512. got++
  513. }
  514. if exp := 2000; got != exp {
  515. b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
  516. }
  517. }
  518. })
  519. }
  520. /*
  521. func BenchmarkBinaryAccountIteration(b *testing.B) {
  522. benchmarkAccountIteration(b, func(snap snapshot) AccountIterator {
  523. return snap.(*diffLayer).newBinaryAccountIterator()
  524. })
  525. }
  526. func BenchmarkFastAccountIteration(b *testing.B) {
  527. benchmarkAccountIteration(b, newFastAccountIterator)
  528. }
  529. func benchmarkAccountIteration(b *testing.B, iterator func(snap snapshot) AccountIterator) {
  530. // Create a diff stack and randomize the accounts across them
  531. layers := make([]map[common.Hash][]byte, 128)
  532. for i := 0; i < len(layers); i++ {
  533. layers[i] = make(map[common.Hash][]byte)
  534. }
  535. for i := 0; i < b.N; i++ {
  536. depth := rand.Intn(len(layers))
  537. layers[depth][randomHash()] = randomAccount()
  538. }
  539. stack := snapshot(emptyLayer())
  540. for _, layer := range layers {
  541. stack = stack.Update(common.Hash{}, layer, nil)
  542. }
  543. // Reset the timers and report all the stats
  544. it := iterator(stack)
  545. b.ResetTimer()
  546. b.ReportAllocs()
  547. for it.Next() {
  548. }
  549. }
  550. */