go-libp2p-pubsub icon indicating copy to clipboard operation
go-libp2p-pubsub copied to clipboard

Publish call high latency

Open aratz-lasa opened this issue 3 years ago • 7 comments

Hello,

The Publish call, in my application, is taking in average 5ms to complete. Moreover, it makes use of an internal lock. Therefore, if you execute Publish N times in parallel, it takes 5ms+N to complete all of them.

Taking this into account, I have a question: What is the lock used for, apart for accessing the t.closed bool? Is there a way to get rid of it? Right now the Publish call it is a huge bottleneck.

aratz-lasa avatar Jul 19 '22 20:07 aratz-lasa

That's unfortunate.

Are you using the topic publish method or the pubsub level Publish method?

Can you profile this? Also, can you check with 0.6/0.7? Lets see if there is a regression.

vyzo avatar Jul 20 '22 06:07 vyzo

First of all, thank you for your fast response!

I have profiled it and the most time takes it at signing messages. If I disable signing it reduces from 5ms to 0.3ms. So I guess it's not bad. However, I do want to sign the messages. Moreover, there is no really a regression from 0.6 to 0.7.

However, I would like to know, what is the lock needed for?

aratz-lasa avatar Jul 20 '22 17:07 aratz-lasa

Are you using RSA keys for some reason?

Can you try with ed25529 keys? It should be considerably faster.

vyzo avatar Jul 21 '22 05:07 vyzo

The lock is used to prevent data races, maybe we can get rid of it indeed.

vyzo avatar Jul 21 '22 05:07 vyzo

I checked and we do use RSA, because when you create a new Host in libp2p, if you don't specify the Identity option it fallbacks into RSA. So I will change that, thanks!

On the other hand, what is the data race that we prevent using the lock? I checked and I could not find any, apart from the closed boolean, which can be replaced by a uber atomic.bool.

aratz-lasa avatar Jul 21 '22 06:07 aratz-lasa

Or a CAS with sync/atomic; care for a patch?

vyzo avatar Jul 21 '22 07:07 vyzo

Okay, patch done. I used the uber package since internally makes use of uint32 and the sync.atomic operations

aratz-lasa avatar Jul 22 '22 06:07 aratz-lasa