Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PLT-380 Replace TChan with bounded queue #502

Merged
merged 4 commits into from
Jun 13, 2022

Conversation

andreabedini
Copy link
Contributor

@andreabedini andreabedini commented Jun 8, 2022

Someone made me notice that I was using an unbounded tchan where I could get away with a mvar. Basically I want to have the two threads (the one running chain-sync and the stream consumer) in lock-step.

In the current version, blocks are pushed in the channel at full speed independently from the speed at which they are consumed, this can cause memory issues if the consumer is slower than the network (which is likely).

Copy link
Contributor

@sjoerdvisscher sjoerdvisscher left a comment

Choose a reason for hiding this comment

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

I'm completely inexperienced when it comes to concurrency programming (in Haskell or any other language), but this all sounds very reasonable. And it is simpler, and simple is good 👍

@ghost
Copy link

ghost commented Jun 8, 2022

You may use Control.Concurrent.STM.TBQueue to accumulate the items with a limited size queue to control the memory usage. That might have a bigger throughput than using a single MVar.

UPD. For blocks with transactions we have Control.Concurrent.STM.TBMQueue which allows to use a measure function to count the number of transactions in blocks instead of the number blocks. The example of usage might be found in chain index.

@koslambrou
Copy link
Contributor

I second @ak3n. The TBMQueue will solve the memory issues.

@andreabedini
Copy link
Contributor Author

You may use Control.Concurrent.STM.TBQueue to accumulate the items with a limited size queue to control the memory usage. That might have a bigger throughput than using a single MVar.

You are right that throughput is important (which reminds me I should implement pipelining as well).

UPD. For blocks with transactions we have Control.Concurrent.STM.TBMQueue which allows to use a measure function to count the number of transactions in blocks instead of the number blocks. The example of usage might be found in chain index.

Control.Concurrent.STM.TBMQueue looks nice and the ability of bounding the channel size in terms of transactions rather than blocks is interesting but it's in plutus-chain-index and plutus-streaming cannot depend on that. Maybe worth extracting it out at some point?

I'll change this PR to use Control.Concurrent.STM.TBQueue

@andreabedini andreabedini force-pushed the andrea/streaming/replace-tchan-to-mvar branch from 9069692 to d132917 Compare June 9, 2022 06:03
@andreabedini andreabedini changed the title Replace TChan with MVar Replace TChan with ~MVar~ bounded queue Jun 9, 2022
@andreabedini andreabedini changed the title Replace TChan with ~MVar~ bounded queue Replace TChan with bounded queue Jun 9, 2022
@ghost
Copy link

ghost commented Jun 9, 2022

Control.Concurrent.STM.TBQueue is unreliable in terms of memory usage in this case because the number of transactions in the blocks could take much more memory with a fixed number of blocks queue. We experienced that with chain index.

Maybe worth extracting it out at some point?

I have extracted it to a separate personal repo https://git.sr.ht/~ak3n/tbmqueue some time ago where I wanted to play with it and maybe tune but had no time and haven't uploaded to hackage (also it seems I messed up with the LICENSE file).

We can start using it which will make me a maintainer and maybe I will extend it. Or we can extract the module to a new package within the plutus-apps repo. Or just copy the module until the dependencies picture will be clearer.

@andreabedini
Copy link
Contributor Author

Control.Concurrent.STM.TBQueue is unreliable in terms of memory usage in this case because the number of transactions in the blocks could take much more memory with a fixed number of blocks queue. We experienced that with chain index.

Do you happen to remember how big was the variation? I am a bit conflicted. I don't even have evicence of lower throughput with an MVar. I will run some tests tomorrow to decide what to do.

@andreabedini andreabedini force-pushed the andrea/streaming/replace-tchan-to-mvar branch from affc1af to 794c6a7 Compare June 10, 2022 04:52
I had moved the place where the exception is thrown and forgot to remove
the previous code.
@andreabedini andreabedini force-pushed the andrea/streaming/replace-tchan-to-mvar branch from 84a2d62 to 31194d4 Compare June 10, 2022 05:07
@andreabedini
Copy link
Contributor Author

@ak3n @koslambrou I appreciate your feedback but I have decided to stick with a dead simple MVar. I added a comment explaining the thread off. I am concerned about spending time optimising for the wrong thing. We can revise this later if it becomes a problem.

@andreabedini andreabedini changed the title Replace TChan with bounded queue PLT-380 Replace TChan with bounded queue Jun 10, 2022
@koslambrou
Copy link
Contributor

@ak3n @koslambrou I appreciate your feedback but I have decided to stick with a dead simple MVar. I added a comment explaining the thread off. I am concerned about spending time optimising for the wrong thing. We can revise this later if it becomes a problem.

No problem, we can add a story for this. I may be wrong, but feels like using something like TBMQueue will indeed improve performance (it did improve the performance of plutus-chain-index, and the current use case is very similar. I might be wrong.

@andreabedini
Copy link
Contributor Author

I may be wrong, but feels like using something like TBMQueue will indeed improve performance (it did improve the performance of plutus-chain-index, and the current use case is very similar. I might be wrong.

I don't mean to say you are wrong! I saw the issue on JIRA 👍

@andreabedini andreabedini merged commit a61ba8f into main Jun 13, 2022
@andreabedini andreabedini deleted the andrea/streaming/replace-tchan-to-mvar branch June 13, 2022 04:03
koslambrou pushed a commit that referenced this pull request Jun 22, 2022
* Remove unreachable code.

I had moved the place where the exception is thrown and forgot to remove
the previous code.

* Replace TChan with MVar

* Add comment about MVar choice

* Incorporate more feedback
koslambrou pushed a commit that referenced this pull request Apr 6, 2023
* Remove unreachable code.

I had moved the place where the exception is thrown and forgot to remove
the previous code.

* Replace TChan with MVar

* Add comment about MVar choice

* Incorporate more feedback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants