Slide 1

Slide 1 text

Releasing Kyo: When Performance Meets Elegance In Scala Flavio Brasil Functional Scala 2023

Slide 2

Slide 2 text

How can I build solutions that are

Slide 3

Slide 3 text

●Simple ●Safe ●Fast How can I build solutions that are ?

Slide 4

Slide 4 text

Activate My first open source project
 https://github.com/fwbrasil/activate

Slide 5

Slide 5 text

Bond Type-level validation https://github.com/fwbrasil/bond

Slide 6

Slide 6 text

Clump: Batching
 https://github.com/getclump/clump 
 Future: High-perf futures https://github.com/traneio/future Arrows: Monadic staging
 https://github.com/traneio/arrows NDBC: Async DB drivers https://github.com/traneio/ndbc

Slide 7

Slide 7 text

Quill Compile-time Language Integrated Queries https://github.com/zio/zio-quill

Slide 8

Slide 8 text

Monadless Syntactic sugar for monads https://github.com/monadless/monadless

Slide 9

Slide 9 text

When your hobby becomes your worry … It’s time for a break!

Slide 10

Slide 10 text

A decade working on large-scale systems ● SoundCloud, Twitter, Nubank ● Delivered major performance optimizations for Finagle, Twitter Future, Stitch, Graal, Clojure ● Saw hundreds of crashes Scala — Graal
 ScalaMatsuri 2019 https://youtu.be/a6dK3eXhO7k?si=T775S1sKFu5B4wSf

Slide 11

Slide 11 text

Functional Programming is great

Slide 12

Slide 12 text

Functional Programming is great (but terrible)

Slide 13

Slide 13 text

Interpretation overhead User-level interpreted language Highly dynamic execution High allocation rate

Slide 14

Slide 14 text

Code complexity

Slide 15

Slide 15 text

Innovation begins with broken rules

Slide 16

Slide 16 text

ZIO[R, E, A]

Slide 17

Slide 17 text

ZIO[R, E, A] type IO[+E, +A] = ZIO[Any, E, A] type Task[+A] = ZIO[Any, Throwable, A] type RIO[-R, +A] = ZIO[R, Throwable, A] type UIO[+A] = ZIO[Any, Nothing, A] type URIO[-R, +A] = ZIO[R, Nothing, A]

Slide 18

Slide 18 text

ZIO[R, E, A] Do we really need a fixed arity? Why assume the computation uses IO and Async? Why only dependency injection and short-circuiting? What if we could express an unbounded number of effects?

Slide 19

Slide 19 text

Let’s break the rules again :)

Slide 20

Slide 20 text

ZIO[R, E, A]

Slide 21

Slide 21 text

A > (R & E)

Slide 22

Slide 22 text

A > (Envs[R] & Aborts[E])

Slide 23

Slide 23 text

A > Aborts[E]

Slide 24

Slide 24 text

A > Any

Slide 25

Slide 25 text

A > Any A pure value 😱

Slide 26

Slide 26 text

A > (Envs[Database] & Aborts[DBException])

Slide 27

Slide 27 text

A > (Envs[Database] & Aborts[DBException] & IOs)

Slide 28

Slide 28 text

type Databases = Envs[Database] & Aborts[DBException] & IOs A > Databases

Slide 29

Slide 29 text

type Caches = Envs[Cache] & IOs type Databases = Envs[Database] & Aborts[DBException] & IOs A > (Databases & Caches)

Slide 30

Slide 30 text

type Caches = Envs[Cache] & IOs type Databases = Envs[Database] & Aborts[DBException] & Caches A > Databases

Slide 31

Slide 31 text

Leveraging Scala’s advanced type system

Slide 32

Slide 32 text

A > (Envs[Database] & Aborts[DBException] & IOs)

Slide 33

Slide 33 text

A > (Aborts[DBException] & Envs[Database] & IOs)

Slide 34

Slide 34 text

A > (Aborts[DBException] & IOs & Envs[Database]) Unordered Pending Effects

Slide 35

Slide 35 text

opaque type >[+T, -S] Original Design

Slide 36

Slide 36 text

opaque type >[+T, -S] = T | Kyo[_, _, _, T, S] Original Design

Slide 37

Slide 37 text

opaque type >[+T, -S] = T | Kyo[_, _, _, T, S] Original Design Implicit conversions from/to T > Any and T

Slide 38

Slide 38 text

opaque type >[+T, -S] = T | Kyo[_, _, _, T, S] After Scala 2 support

Slide 39

Slide 39 text

type >[+T, -S] >: T After Scala 2 support

Slide 40

Slide 40 text

type >[+T, -S] >: T After Scala 2 support Any type T is a subclass of T > Any

Slide 41

Slide 41 text

type >[+T, -S] >: T After Scala 2 support Any type T is a subclass of T > Any val a: Int > Any = 1 val b: Int = a.pure

Slide 42

Slide 42 text

Effect Widening val a: Int > Any = 1

Slide 43

Slide 43 text

Effect Widening val a: Int > Any = 1 
 val b: Int > Options = a 


Slide 44

Slide 44 text

Effect Widening val a: Int > Any = 1 
 val b: Int > Options = a 
 
 val c: Int > (Options & Tries) = b 
 


Slide 45

Slide 45 text

Effect Widening val a: Int > Any = 1 
 val b: Int > Options = a 
 
 val c: Int > (Options & Tries) = b 
 
 val d: Int > (Options & Tries) = 42

Slide 46

Slide 46 text

def example1(v: Int > (Options & Tries)) = v.map(_ + 1) Fluent APIs

Slide 47

Slide 47 text

def example1(v: Int > (Options & Tries)) = v.map(_ + 1) def example2(v: Int > Tries) = example1(v) 
 Fluent APIs

Slide 48

Slide 48 text

Fluent APIs def example1(v: Int > (Options & Tries)) = v.map(_ + 1) def example2(v: Int > Tries) = example1(v) 
 
 def example3 = example1(42)

Slide 49

Slide 49 text

There is no flatMap 
 (or map depending on how you see it)

Slide 50

Slide 50 text

def example( a: Int > Options, b: Int > Tries ): Int > (Options & Tries) = a.flatMap(v => b.map(_ + v))

Slide 51

Slide 51 text

def example( a: Int > Options, b: Int > Tries ): Int > (Options & Tries) = a.map(v => b.map(_ + v))

Slide 52

Slide 52 text

Direct syntax support

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

Quick effects rundown

Slide 57

Slide 57 text

Envs: Dependency Injection

Slide 58

Slide 58 text

Envs: Dependency Injection

Slide 59

Slide 59 text

Aborts: Short Circuiting

Slide 60

Slide 60 text

Aborts: Short Circuiting

Slide 61

Slide 61 text

Resources: Resource Safety

Slide 62

Slide 62 text

Lists: Exploratory Branching

Slide 63

Slide 63 text

Lists: Exploratory Branching

Slide 64

Slide 64 text

Fibers: Green Threads

Slide 65

Slide 65 text

Fibers: Green Threads

Slide 66

Slide 66 text

Requests: HTTP Client via Sttp

Slide 67

Slide 67 text

Requests: HTTP Client via Sttp

Slide 68

Slide 68 text

Locals Scoped Values Aspects Aspect-Oriented Programming Options Optional Values Tries Exception Handling Consoles Console Interaction Clocks Time Management Randoms Random Values Loggers Logging Stats Observability Queues Concurrent Queuing Channels Backpressured Communication Hubs Broadcasting with Backpressure Meters Computational Limits Timers Scheduled Execution Latches Fiber Coordination Atomics Concurrent State Adders Concurrent Accumulation Caches Memoized Functions via Caffeine

Slide 69

Slide 69 text

Unparalleled performance

Slide 70

Slide 70 text

https://tinyurl.com/kyo-benchs

Slide 71

Slide 71 text

Targets Kyo, Cats-effect, ZIO, Ox Data Throughput, Allocation, Profiling Environment Github Large Runner 8 cores, 32 GB Ubuntu 22.04 Source code 
 https://github.com/getkyo/kyo/tree/main/kyo-bench/src/main/scala/kyo/bench https://tinyurl.com/kyo-benchs

Slide 72

Slide 72 text

Kyo’s throughput is 249% higher than the best contender (average across benchmarks)

Slide 73

Slide 73 text

Overhead-free abstraction

Slide 74

Slide 74 text

Overhead-free abstraction

Slide 75

Slide 75 text

Overhead-free abstraction

Slide 76

Slide 76 text

High-performance asynchronous primitives

Slide 77

Slide 77 text

High-performance asynchronous primitives

Slide 78

Slide 78 text

High-performance asynchronous primitives

Slide 79

Slide 79 text

Low-overhead Fibers (only 32B each!)

Slide 80

Slide 80 text

Low-overhead Fibers (only 32B each!)

Slide 81

Slide 81 text

Low-overhead Fibers (only 32B each!)

Slide 82

Slide 82 text

Blazing-fast channels

Slide 83

Slide 83 text

Blazing-fast channels

Slide 84

Slide 84 text

Blazing-fast channels

Slide 85

Slide 85 text

How about Caprese?

Slide 86

Slide 86 text

Caprese Kyo delivers Caprese’s key promises

Slide 87

Slide 87 text

Caprese Kyo delivers Caprese’s key promises without language changes

Slide 88

Slide 88 text

Caprese Kyo delivers Caprese’s key promises without language changes, with a complete set of effects

Slide 89

Slide 89 text

Caprese Kyo delivers Caprese’s key promises without language changes, with a complete set of effects and next-level performance

Slide 90

Slide 90 text

Caprese Kyo delivers Caprese’s key promises without language changes, with a complete set of effects and next-level performance in Scala 2, 3, and JS

Slide 91

Slide 91 text

Caprese Kyo delivers Caprese’s key promises without language changes, with a complete set of effects and next-level performance in Scala 2, 3, and JS today

Slide 92

Slide 92 text

CREDITS: This presentation template was created by Slidesgo, and includes icons by Flaticon and infographics & images by Freepik Thanks! Any questions? https://getkyo.io https://dev.to/fwbrasil @fbrasisil