Skip to content

Rewrite TBQueue to use arrays - #70

Merged
bgamari merged 8 commits into
haskell:masterfrom
konsumlamm:tbqueue-array
Sep 20, 2023
Merged

bgamari merged 8 commits into
haskell:masterfrom
konsumlamm:tbqueue-array

Conversation

@konsumlamm

@konsumlamm konsumlamm commented Sep 9, 2023 •

Copy link
Copy Markdown
Contributor

Closes #65.
Closes #36 (is included, since it's needed for newTBQueueIO). Closes #35.

I cherry-picked the commit from #36 to keep the author credit.

The new implementation uses TArray Int (Maybe a). The read index is the next index to read from, the write index is the next index to write to. readTBQueue and writeTBQueue only interfere if the queue is full or empty (in which case one of them has to retry anyway), so most of the time they can run concurrently. This new implementation is faster than the old one in all 3 benchmarks.

Optimizations:

  • use unsafeRead and unsafeWrite for array accesses (readArray and writeArray generate 4 (!) comparisons)
  • avoid using mod by defining incMod and decMod

Since the array size can only be an Int, the capacity is also stored as an Int internally and newTBQueue/newTBQueueIO error if the capacity is bigger than maxBound :: Int. For that reason, I'd argue that using Natural in the API isn't very good (and since this was only changed in 2.5, perhaps it could be changed back in 2.6). newTBQueue/newTBQueueIO now also error if the capacity is zero, since such a queue is very useless and it would need extra special casing.

I also updated the benchmarks (to be able to benchmark my changes) and added a proper cabal benchmark (it needs a separate stm-bench.cabal file to avoid cyclic dependencies, since almost everything depends on stm).

In the testsuite, I removed Issue17.hs, which tested behaviour for negative and zero capacities (which are now invalid).

Lastly, I added an hie.yaml file to for HLS to work properly in this project.

@konsumlamm
konsumlamm marked this pull request as ready for review September 9, 2023 01:07
@simonmar

simonmar commented Sep 9, 2023

Copy link
Copy Markdown
Member

Could you show the benchmark results please?

@konsumlamm

Copy link
Copy Markdown
Contributor Author

Sure, here are my results:

All
  concurrent spsc
    Chan:          OK (2.30s)
      336  ms ±  28 ms
    TChan:         OK (1.36s)
      193  ms ± 7.2 ms
    TQueue:        OK (7.71s)
      116  ms ± 5.7 ms
    TBQueue (old): OK (9.29s)
      303  ms ± 8.0 ms
    TBQueue (new): OK (5.91s)
      193  ms ± 724 μs
  concurrent mpmc
    Chan:          OK (34.32s)
      11.387 s ± 722 ms
    TChan:         OK (2.44s)
      811  ms ± 9.7 ms
    TQueue:        OK (2.02s)
      676  ms ±  36 ms
    TBQueue (old): OK (5.58s)
      1.853 s ± 107 ms
    TBQueue (new): OK (84.81s)
      1.354 s ± 125 ms
  bulk
    Chan:          OK (3.68s)
      538  ms ±  42 ms
    TChan:         OK (3.81s)
      555  ms ±  42 ms
    TQueue:        OK (1.11s)
      369  ms ± 7.3 ms
  burst
    Chan:          OK (1.68s)
      262  ms ± 8.8 ms
    TChan:         OK (0.87s)
      297  ms ±  17 ms
    TQueue:        OK (1.53s)
      225  ms ±  11 ms
    TBQueue (old): OK (4.93s)
      334  ms ±  19 ms
    TBQueue (new): OK (0.85s)
      285  ms ±  10 ms

The concrete times vary a bit, but the new TBQueue is consistently faster.

@simonmar

Copy link
Copy Markdown
Member

Seems good to me. We should look into why CI is consistently failing though (I imagine it's not just this PR but something more fundamental)

@bgamari

bgamari commented Sep 14, 2023

Copy link
Copy Markdown
Contributor

Could you rebase on master?

@konsumlamm

konsumlamm commented Sep 15, 2023 •

Copy link
Copy Markdown
Contributor Author

I managed to fix the CI up to the point where only ghc-9.6.2 fails (see https://github.com/konsumlamm/stm/actions/runs/6196967418/job/16824613715 on branch https://github.com/konsumlamm/stm/tree/tbqueue-array-ci):

[__6] rejecting: exceptions-0.10.7/installed-0.10.7 (package is broken, missing dependency stm-2.5.1.0)

Do you have an idea why this happens and how to fix it?

@RyanGlScott

Copy link
Copy Markdown
Member

I suspect that cabal is confused because exceptions (a boot library that is bundled with GHC) depends on stm (another boot library), but the CI builds a local copy of stm that is meant to supersede the GHC-bundled version of stm. I think you could avoid this problem by putting this in your cabal.haskell-ci file and regenerating:

installed: +all -exceptions

This should permit the CI to rebuild exceptions on top of the local build of stm when necessary.

@konsumlamm

Copy link
Copy Markdown
Contributor Author

Adding installed: +all -exceptions -filepath did the trick.

@konsumlamm

Copy link
Copy Markdown
Contributor Author

Benchmarks only get built for GHC >= 8.0, since they use TypeApplications, but that shouldn't be a problem.

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.

Why isn't TBQueue backed by an array? Add MArray instance for TArray in IO

5 participants