Skip to content

Commit

Permalink
Add cc to the rest of jvm impl
Browse files Browse the repository at this point in the history
  • Loading branch information
natsukagami committed May 8, 2024
1 parent 5c16bc3 commit e5d40eb
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 15 deletions.
2 changes: 2 additions & 0 deletions jvm/src/main/scala/PosixLikeIO/PIO.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package PosixLikeIO

import language.experimental.captureChecking

import gears.async.Scheduler
import gears.async.default.given
import gears.async.{Async, Future}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package PosixLikeIO.examples

import language.experimental.captureChecking

import gears.async.AsyncOperations.*
import gears.async.default.given
import gears.async.{Async, Future}
Expand All @@ -13,7 +15,7 @@ import PosixLikeIO.{PIOHelper, SocketUDP}

@main def clientAndServerUDP(): Unit =
given ExecutionContext = ExecutionContext.global
Async.blocking:
Async.blocking: spawn ?=>
val server = Future:
PIOHelper.withSocketUDP(8134): serverSocket =>
val got: DatagramPacket = serverSocket.receive().awaitResult.get
Expand All @@ -22,7 +24,7 @@ import PosixLikeIO.{PIOHelper, SocketUDP}
serverSocket.send(ByteBuffer.wrap(responseMessage), got.getAddress.toString.substring(1), got.getPort)
sleep(50)

def client(value: Int): Future[Unit] =
def client(value: Int): Future[Unit]^{spawn} =
Future:
PIOHelper.withSocketUDP(): clientSocket =>
val data: Array[Byte] = value.toString.getBytes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package PosixLikeIO.examples

import language.experimental.captureChecking

import gears.async.Async
import gears.async.default.given

Expand Down
2 changes: 2 additions & 0 deletions jvm/src/main/scala/PosixLikeIO/examples/readWholeFile.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package PosixLikeIO.examples

import language.experimental.captureChecking

import gears.async.Async
import gears.async.default.given

Expand Down
2 changes: 2 additions & 0 deletions jvm/src/main/scala/async/DefaultSupport.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package gears.async.default

import language.experimental.captureChecking

import gears.async._

given AsyncOperations = JvmAsyncOperations
Expand Down
28 changes: 16 additions & 12 deletions jvm/src/main/scala/measurements/measureTimes.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package measurements

import language.experimental.captureChecking

import gears.async.default.given
import gears.async.{Async, BufferedChannel, ChannelMultiplexer, Future, SyncChannel}

Expand Down Expand Up @@ -65,9 +67,12 @@ def measureIterations[T](action: () => T): Int =

val c1: Double = measureIterations: () =>
Async.blocking:
Async.race(Future { Thread.sleep(10) }, Future { Thread.sleep(100) }, Future { Thread.sleep(50) }).await
Async.race(Future { Thread.sleep(50) }, Future { Thread.sleep(10) }, Future { Thread.sleep(100) }).await
Async.race(Future { Thread.sleep(100) }, Future { Thread.sleep(50) }, Future { Thread.sleep(10) }).await
val r1 = Async.race(Future { Thread.sleep(10) }, Future { Thread.sleep(100) }, Future { Thread.sleep(50) })
r1.await
val r2 = Async.race(Future { Thread.sleep(50) }, Future { Thread.sleep(10) }, Future { Thread.sleep(100) })
r2.await
val r3 = Async.race(Future { Thread.sleep(100) }, Future { Thread.sleep(50) }, Future { Thread.sleep(10) })
r3.await

val c2: Double = measureIterations: () =>
Async.blocking:
Expand Down Expand Up @@ -95,11 +100,11 @@ def measureIterations[T](action: () => T): Int =
println("Non-raced futures per second: " + c2_per_second_adjusted)
println("Overhead: " + (c2_per_second_adjusted / c1_per_second_adjusted))

/* Linux
/* Linux
Raced futures awaited per second: 15.590345727332032
Non-raced futures per second: 15.597976831457009
Overhead: 1.0004894762604013
*/
*/

@main def measureRaceOverheadVsJava(): Unit =
given ExecutionContext = ExecutionContext.global
Expand Down Expand Up @@ -148,11 +153,11 @@ def measureIterations[T](action: () => T): Int =
println("Java threads awaited per second: " + c2_per_second_adjusted)
println("Overhead: " + (c2_per_second_adjusted / c1_per_second_adjusted))

/* Linux
/* Linux
Raced futures awaited per second: 15.411487529449996
Java threads awaited per second: 15.671210243700953
Overhead: 1.0168525402726147
*/
*/

@main def channelsVsJava(): Unit =
given ExecutionContext = ExecutionContext.global
Expand Down Expand Up @@ -305,13 +310,13 @@ def measureIterations[T](action: () => T): Int =
Thread.sleep(500)
println("ChannelMultiplexer over BufferedChannels sends per second: " + cmOverBufferedSendsPerSecond)

/* Linux
/* Linux
Java "channel" sends per second: 8691652
SyncChannel sends per second: 319371.0
BufferedChannel sends per second: 308286.0
ChannelMultiplexer over SyncChannels sends per second: 155737.0
ChannelMultiplexer over BufferedChannels sends per second: 151995.0
*/
*/

/** Warmup for 10 seconds and benchmark for 60 seconds.
*/
Expand Down Expand Up @@ -490,7 +495,7 @@ def measureRunTimes[T](action: () => T): TimeMeasurementResult =
dataAlmostJson.append("}")
println(dataAlmostJson.toString)

/* Linux
/* Linux
{
"File writing": {
Expand All @@ -506,5 +511,4 @@ def measureRunTimes[T](action: () => T): TimeMeasurementResult =
"Java Files.write": [18.96376850600001, 0.20493288428568684],
},
},
}
*/
} */
2 changes: 1 addition & 1 deletion shared/src/main/scala/async/Async.scala
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ object Async:
* @see
* [[Source!.awaitResult awaitResult]] for non-unwrapping await.
*/
inline def await(using Async): T = src.awaitResult.get
def await(using Async): T = src.awaitResult.get
extension [E, T](src: Source[Either[E, T]]^)
/** Waits for an item to arrive from the source, then automatically unwraps it. Suspends until an item returns.
* @see
Expand Down

0 comments on commit e5d40eb

Please sign in to comment.