Skip to content

Commit

Permalink
badger: clean up empty results lists before running them through the …
Browse files Browse the repository at this point in the history
…merger.

this didn't make much difference but I'll leave some benchmarks here just because I forgot to do this after the big badger rewrite.

this was immediately after the badger rewrite:

goos: linux
goarch: amd64
pkg: github.com/fiatjaf/eventstore/test
cpu: AMD Ryzen 3 3200G with Radeon Vega Graphics
BenchmarkDBs/badger/filter/q-0-4         	     500	      1227 ns/op	    2447 B/op	      31 allocs/op
BenchmarkDBs/badger/filter/q-1-4         	     500	      1506 ns/op	    3371 B/op	      41 allocs/op
BenchmarkDBs/badger/filter/q-2-4         	     500	      2212 ns/op	    4323 B/op	      56 allocs/op
BenchmarkDBs/badger/filter/q-3-4         	     500	       729.1 ns/op	    1769 B/op	      20 allocs/op
BenchmarkDBs/badger/filter/q-4-4         	     500	       827.5 ns/op	    1737 B/op	      19 allocs/op
BenchmarkDBs/badger/filter/q-5-4         	     500	      1215 ns/op	    1799 B/op	      21 allocs/op
BenchmarkDBs/badger/filter/q-6-4         	     500	      1661 ns/op	    2923 B/op	      34 allocs/op
BenchmarkDBs/badger/filter/q-7-4         	     500	      1152 ns/op	    2166 B/op	      26 allocs/op
BenchmarkDBs/badger/filter/q-8-4         	     500	      4425 ns/op	    8320 B/op	     129 allocs/op
BenchmarkDBs/badger/filter/q-9-4         	     500	      5429 ns/op	    8273 B/op	     126 allocs/op
BenchmarkDBs/badger/filter/q-10-4        	     500	      5006 ns/op	    3729 B/op	      68 allocs/op
BenchmarkDBs/badger/filter/q-11-4        	     500	      4833 ns/op	    8313 B/op	     134 allocs/op
BenchmarkDBs/badger/insert-4             	     500	      3541 ns/op	     999 B/op	      20 allocs/op

and this is now after this commit (yes, it's basically the same):

goos: linux
goarch: amd64
pkg: github.com/fiatjaf/eventstore/test
cpu: AMD Ryzen 3 3200G with Radeon Vega Graphics
BenchmarkDBs/badger/filter/q-0-4         	     500	       939.8 ns/op	    1989 B/op	      26 allocs/op
BenchmarkDBs/badger/filter/q-1-4         	     500	      2108 ns/op	    4332 B/op	      53 allocs/op
BenchmarkDBs/badger/filter/q-2-4         	     500	      2472 ns/op	    4297 B/op	      56 allocs/op
BenchmarkDBs/badger/filter/q-3-4         	     500	       722.8 ns/op	    1716 B/op	      20 allocs/op
BenchmarkDBs/badger/filter/q-4-4         	     500	       786.7 ns/op	    1805 B/op	      20 allocs/op
BenchmarkDBs/badger/filter/q-5-4         	     500	      1108 ns/op	    2286 B/op	      27 allocs/op
BenchmarkDBs/badger/filter/q-6-4         	     500	      1044 ns/op	    2579 B/op	      30 allocs/op
BenchmarkDBs/badger/filter/q-7-4         	     500	       991.6 ns/op	    2296 B/op	      27 allocs/op
BenchmarkDBs/badger/filter/q-8-4         	     500	      4514 ns/op	    9067 B/op	     135 allocs/op
BenchmarkDBs/badger/filter/q-9-4         	     500	      5294 ns/op	    9364 B/op	     139 allocs/op
BenchmarkDBs/badger/filter/q-10-4        	     500	      4806 ns/op	    6819 B/op	     112 allocs/op
BenchmarkDBs/badger/filter/q-11-4        	     500	      4956 ns/op	    7828 B/op	     127 allocs/op
BenchmarkDBs/badger/insert-4             	     500	      3500 ns/op	    1160 B/op	      20 allocs/op
  • Loading branch information
fiatjaf committed Oct 1, 2024
1 parent f32393f commit 8647eb3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
33 changes: 25 additions & 8 deletions badger/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,29 @@ func getAddrTagElements(tagValue string) (kind uint16, pkb []byte, d string) {
// mergeSortMultipleBatches takes the results of multiple iterators, which are already sorted,
// and merges them into a single big sorted slice
func mergeSortMultiple(batches [][]*nostr.Event, limit int) []*nostr.Event {
// clear up empty lists here while simultaneously computing the total count.
// this helps because if there are a bunch of empty lists then this pre-clean
// step will get us in the faster 'merge' branch otherwise we would go to the other.
// we would have to do the cleaning anyway inside it.
// and even if we still go on the other we save one iteration by already computing the
// total count.
total := 0
for i := len(batches) - 1; i >= 0; i-- {
if len(batches[i]) == 0 {
batches = swapDelete(batches, i)
} else {
total += len(batches[i])
}
}

// this amazing equation will ensure that if one of the two sides goes very small (like 1 or 2)
// the other can go very high (like 500) and we're still in the 'merge' branch.
// if values go somewhere in the middle then they may match the 'merge' branch (batches=20,limit=70)
// or not (batches=25, limit=60)
if math.Log(float64(len(batches)*2))+math.Log(float64(limit)) < 8 {
// this amazing function will ensure that if one of the two sides goes very small (like 1 or 2)
// the other can go very high (like 500) and we're still here. if values go somewhere in the middle
// then they may match here (batches=20,limit=70) or not (batches=25, limit=60)
return mergesortedslices.MergeFuncLimit(batches, nostr.CompareEventPtr, limit)
return mergesortedslices.MergeFuncLimitNoEmptyLists(batches, nostr.CompareEventPtr, limit)
} else {
// use quicksort in a dumb way that will still be fast because it's cheated
total := 0
for _, one := range batches {
total += len(one)
}
merged := make([]*nostr.Event, total)

lastIndex := 0
Expand Down Expand Up @@ -201,3 +213,8 @@ func filterMatchesTags(ef *nostr.Filter, event *nostr.Event) bool {
}
return true
}

func swapDelete[A any](arr []A, i int) []A {
arr[i] = arr[len(arr)-1]
return arr[:len(arr)-1]
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/elastic/go-elasticsearch/v8 v8.10.1
github.com/fergusstrange/embedded-postgres v1.28.0
github.com/fiatjaf/cli/v3 v3.0.0-20240723181502-e7dd498b16ae
github.com/fiatjaf/merge-sorted-slices v0.0.5
github.com/fiatjaf/merge-sorted-slices v0.0.6
github.com/go-sql-driver/mysql v1.7.1
github.com/jmoiron/sqlx v1.3.5
github.com/lib/pq v1.10.9
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ github.com/fergusstrange/embedded-postgres v1.28.0 h1:Atixd24HCuBHBavnG4eiZAjRiz
github.com/fergusstrange/embedded-postgres v1.28.0/go.mod h1:t/MLs0h9ukYM6FSt99R7InCHs1nW0ordoVCcnzmpTYw=
github.com/fiatjaf/cli/v3 v3.0.0-20240723181502-e7dd498b16ae h1:0B/1dU3YECIbPoBIRTQ4c0scZCNz9TVHtQpiODGrTTo=
github.com/fiatjaf/cli/v3 v3.0.0-20240723181502-e7dd498b16ae/go.mod h1:aAWPO4bixZZxPtOnH6K3q4GbQ0jftUNDW9Oa861IRew=
github.com/fiatjaf/merge-sorted-slices v0.0.5 h1:Mmt3TdONTnhp3F4YHN/Uta8tFOm5RhBhdzuLEucStAw=
github.com/fiatjaf/merge-sorted-slices v0.0.5/go.mod h1:k7H3l+OhO2m/PA99FdBjF29Z0TAwRClgKA9PvoT6obY=
github.com/fiatjaf/merge-sorted-slices v0.0.6 h1:6jawP9Zx5RTKEHvQt6z+8zvmtCkb1JTz1498N3OE70A=
github.com/fiatjaf/merge-sorted-slices v0.0.6/go.mod h1:k7H3l+OhO2m/PA99FdBjF29Z0TAwRClgKA9PvoT6obY=
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
Expand Down

0 comments on commit 8647eb3

Please sign in to comment.