Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ replace github.com/gogo/protobuf => github.com/gogo/protobuf v1.3.2
// allows us to specify that as an option.
replace google.golang.org/protobuf => github.com/lightninglabs/protobuf-go-hex-display v1.33.0-hex-display

replace github.com/lightningnetwork/lnd/sqldb => ./sqldb

// If you change this please also update docs/INSTALL.md and GO_VERSION in
// Makefile (then run `make lint` to see where else it needs to be updated as
// well).
Expand Down
25 changes: 25 additions & 0 deletions graph/db/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1640,6 +1640,8 @@ func TestGraphCacheTraversal(t *testing.T) {
require.Equal(t, numChannels*2*(numNodes-1), numNodeChans)
}

// fillTestGraph fills the graph with a given number of nodes and create a given
// number of channels between each node.
func fillTestGraph(t testing.TB, graph *ChannelGraph, numNodes,
numChannels int) (map[uint64]struct{}, []*models.Node) {

Expand Down Expand Up @@ -4042,6 +4044,29 @@ func TestNodeIsPublic(t *testing.T) {
)
}

// BenchmarkIsPublicNode measures the performance of IsPublicNode when checking
// a large number of nodes.
func BenchmarkIsPublicNode(b *testing.B) {
graph := MakeTestGraph(b)

// Create a graph with a reasonable number of nodes and channels.
numNodes := 8000
Copy link
Contributor

@MPins MPins Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After changing the number of nodes to 16000 I got:

*** SELECT WITH UNION ***

go test -tags=test_db_sqlite -bench=BenchmarkIsPublicNode

goos: linux
goarch: amd64
pkg: github.com/lightningnetwork/lnd/graph/db
cpu: AMD Ryzen 7 7840HS w/ Radeon 780M Graphics     
BenchmarkIsPublicNode-16    	      93	  11939604 ns/op
--- BENCH: BenchmarkIsPublicNode-16
    test_sqlite.go:49: Creating new SQLite DB for testing
PASS
ok  	github.com/lightningnetwork/lnd/graph/db	212.488s

Then changed the go.mod line 216:

from replace github.com/lightningnetwork/lnd/sqldb => ./sqldb
to //replace github.com/lightningnetwork/lnd/sqldb => ./sqldb

*** SELECT USING OR ***
And ran it again :

goos: linux
goarch: amd64
pkg: github.com/lightningnetwork/lnd/graph/db
cpu: AMD Ryzen 7 7840HS w/ Radeon 780M Graphics     
BenchmarkIsPublicNode-16    	      48	  24982592 ns/op
--- BENCH: BenchmarkIsPublicNode-16
    test_sqlite.go:49: Creating new SQLite DB for testing
PASS
ok  	github.com/lightningnetwork/lnd/graph/db	228.831s

The BenchmarkIsPublicNode using SELECT WITH UNION took 11.9ms and using SELECT WITH OR 24.9ms.

numChans := 4
_, nodes := fillTestGraph(b, graph, numNodes, numChans)

// Pick any node to test with.
nodePub := nodes[len(nodes)/2].PubKeyBytes

// Reset the timer to exclude setup time especially since
// `fillTestGraph` can take a while.
b.ResetTimer()

for b.Loop() {
_, err := graph.IsPublicNode(nodePub)
require.NoError(b, err)
}
}

// TestDisabledChannelIDs ensures that the disabled channels within the
// disabledEdgePolicyBucket are managed properly and the list returned from
// DisabledChannelIDs is correct.
Expand Down
9 changes: 8 additions & 1 deletion sqldb/sqlc/graph.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion sqldb/sqlc/queries/graph.sql
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ LIMIT $3;
SELECT EXISTS (
SELECT 1
FROM graph_channels c
JOIN graph_nodes n ON n.id = c.node_id_1 OR n.id = c.node_id_2
JOIN graph_nodes n ON n.id = c.node_id_1
-- NOTE: we hard-code the version here since the clauses
-- here that determine if a node is public is specific
-- to the V1 gossip protocol. In V1, a node is public
Expand All @@ -89,6 +89,13 @@ SELECT EXISTS (
WHERE c.version = 1
AND c.bitcoin_1_signature IS NOT NULL
AND n.pub_key = $1
UNION ALL
SELECT 1
FROM graph_channels c
JOIN graph_nodes n ON n.id = c.node_id_2
WHERE c.version = 1
AND c.bitcoin_1_signature IS NOT NULL
AND n.pub_key = $1
);

-- name: DeleteUnconnectedNodes :many
Expand Down
Loading