Repository navigation
Expand file tree
/
Copy pathexplainer.html
More file actions
972 lines (938 loc) · 76.8 KB
/
Copy pathexplainer.html
File metadata and controls
972 lines (938 loc) · 76.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Campfire on Ruby 4 Ractors: where the speed comes from</title>
<style>
:root {
color-scheme: light;
--surface-0: #f6f5f2; --surface-1: #fcfcfb; --surface-2: #efeee9; --line: #dad8d0;
--text-primary: #0b0b0b; --text-secondary: #52514e; --text-muted: #7a7974;
--ruby: #2a78d6; --rust: #eb6834; --go: #1baf7a; --rails: #eda100;
--accent: #2a78d6; --bad: #e34948; --good: #008300; --wait: #c3c2b7; --zjit: #e87ba4;
--mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, monospace;
--sans: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", "Helvetica Neue", Arial, "Noto Sans", "DejaVu Sans", sans-serif;
}
@media (prefers-color-scheme: dark) {
:root {
color-scheme: dark;
--surface-0: #121211; --surface-1: #1a1a19; --surface-2: #242422; --line: #3a3a37;
--text-primary: #ffffff; --text-secondary: #c3c2b7; --text-muted: #8f8e86;
--ruby: #3987e5; --rust: #d95926; --go: #199e70; --rails: #c98500;
--accent: #3987e5; --bad: #e66767; --good: #2fa84f; --wait: #55544f; --zjit: #d55181;
}
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--surface-0); color: var(--text-primary); font: 16px/1.55 var(--sans); }
main { max-width: 1040px; margin: 0 auto; padding: 48px 24px 120px; }
h1 { font-size: clamp(30px, 5vw, 50px); line-height: 1.08; letter-spacing: -0.02em; margin: 0 0 16px; }
h2 { font-size: 28px; letter-spacing: -0.01em; margin: 88px 0 8px; }
h2 small { display: block; font-size: 13px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--text-muted); font-weight: 600; margin-bottom: 6px; }
p { color: var(--text-secondary); max-width: 760px; }
p strong, li strong { color: var(--text-primary); }
code { font-family: var(--mono); font-size: 0.88em; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }
a { color: var(--accent); }
.lede { font-size: 19px; max-width: 800px; }
.refs { display: flex; flex-wrap: wrap; gap: 8px; margin: 20px 0 0; }
.refs a { text-decoration: none; font-size: 13px; padding: 5px 10px; border: 1px solid var(--line); border-radius: 999px; color: var(--text-secondary); background: var(--surface-1); }
.refs a b { color: var(--text-primary); }
footer { margin-top: 96px; color: var(--text-muted); font-size: 13px; border-top: 1px solid var(--line); padding-top: 20px; }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
</style>
</head>
<body>
<main>
<h1>Campfire on Ruby 4 Ractors:<br>where the speed comes from</h1>
<p class="lede">
A port of <a href="https://github.com/basecamp/once-campfire">ONCE Campfire</a> that keeps Ruby but drops Rails. It serves
<strong>byte-identical HTML</strong> from one process: Falcon running in worker Ractors, plain modules over SQLite,
and message passing between Ractors instead of Redis. On the same 4 CPU threads it serves dynamic pages
<strong>15–27× faster</strong> than the Rails app it was ported from, and beats the Go port on most of them.
</p>
<div class="refs">
<a href="https://github.com/tobi/campfire-once-ruby-ractor"><b>this repo</b> tobi/campfire-once-ruby-ractor</a>
<a href="https://github.com/basecamp/once-campfire"><b>source of truth</b> basecamp/once-campfire</a>
<a href="https://github.com/basecamp/once-campfire-go"><b>architecture</b> once-campfire-go</a>
<a href="https://github.com/basecamp/once-campfire-rust"><b>parity & bench harness</b> once-campfire-rust</a>
</div>
<cf-hero></cf-hero>
<h2><small>1 · The shape</small>One process, many Ractors, no Redis</h2>
<p>
Rails Campfire runs Thruster, several Puma processes, Resque workers and Redis, which carries Action Cable
broadcasts and the job queue. The port follows the <a href="https://github.com/basecamp/once-campfire-go">Go port</a>'s
architecture instead: one module per concern over SQLite, with queues and the cable hub inside the process. In Ruby 4
the pieces that need to run in parallel are <strong>Ractors</strong>. Each one has its own lock, its own heap and its own
SQLite connection. They talk only by sending frozen messages, which cross between Ractors without being copied.
Pick a flow to watch it run.
</p>
<cf-arch></cf-arch>
<h2><small>2 · What got deleted</small>Keep the wire contracts, drop the framework</h2>
<p>
The browser and the database can't tell the difference: same cookies, same signed ids, same
Action Cable frames, same Active Storage URLs, the same SQLite file. Everything between them is new, smaller code.
A few behaviors differ on purpose, as in the Rust port: forgery protection is by <code>Sec-Fetch-Site</code>
instead of CSRF tokens, so a page renders the same until what it shows changes.
The <a href="https://github.com/basecamp/once-campfire-rust">Rust repo</a>'s parity suite checked the result before
those differences: <strong>956 of 956</strong> checks passed across five seeds (HTML, DOM, accessibility tree, assets, Cable frames and screenshots).
</p>
<cf-stack></cf-stack>
<h2><small>3 · Reads</small>One snapshot per request</h2>
<p>
A room page runs 5–30 queries. In autocommit mode, SQLite opens a read transaction for <em>each one</em>:
WAL-index locks, an <code>fstat</code>, cache validation. That costs about 4.5 µs. Inside an open transaction the
same point query takes about 0.2 µs. So every GET runs in one <code>BEGIN … COMMIT</code>, and the first write
in a request commits the snapshot first, so a stale read can never be upgraded into a write.
</p>
<cf-snapshot></cf-snapshot>
<h2><small>4 · Rendering</small>Append, cache, and splice the gzip</h2>
<p>
Templates compile to Ruby methods that append into <strong>one pre-sized String</strong> per response. There's no
interpolation, no <code>join</code> and no intermediate arrays, and ids come out of SQL as text. Message fragments are
cached per Ractor and keyed like Rails' <code>cache</code> calls. But the room page is about 460 KB of HTML, and
gzipping it took <strong>1.9 ms of a 2.4 ms request</strong>. The fix, adapted from the Rust port: deflate
each part of the page <em>once</em>: every cached fragment, using the fragments before it as the dictionary, and
the layout text between them, using the fragment before it. Each is flushed to a byte boundary and kept. Pages
carry no per-request token, so at request time the stored pieces are copied straight into the stream, and the
ETag repeats until the page changes, so revalidation is a 304.
</p>
<cf-splice></cf-splice>
<h2><small>5 · Writes</small>A single writer, handed out in arrival order</h2>
<p>
SQLite allows one writer at a time. Left to the file lock, worker Ractors raced for it: <code>BEGIN IMMEDIATE</code>,
<code>SQLITE_BUSY</code>, sleep, retry. Retries that kept losing waited 100 ms or more while others walked straight in, and the lock
sat idle between sleepers. Now a <strong>writer Ractor hands out the write permit first-come, first-served</strong>.
<code>Connection#transaction</code> takes it without callers changing anything, and only the waiting <em>fiber</em> suspends, so the
rest of that Ractor keeps serving. The SQL itself still runs on the caller's own connection, so passing the permit costs
two messages per transaction rather than a round trip per statement.
</p>
<cf-writer></cf-writer>
<h3 style="margin-top:56px">Rails callbacks become SQLite triggers</h3>
<p>
<code>touch: true</code> on boosts and messages, <code>Room#receive</code> marking memberships unread, dependent deletes and
the search-history trim now run as triggers <em>inside the write that causes them</em>. A post is one short
transaction with three statements from Ruby. One rule keeps them honest: <strong>triggers never read the clock</strong>.
SQLite's clock only has millisecond precision and could move <code>updated_at</code> backwards, so a trigger only copies times from the row being written.
</p>
<cf-triggers></cf-triggers>
<h3 style="margin-top:56px">Checkpoints off the request path</h3>
<p>
SQLite's auto-checkpoint runs <em>inside</em> whichever COMMIT crosses 1,000 WAL pages, and flushes the database to disk
right there: about every 30th post took 10–20 ms instead of 0.4. Request connections now never checkpoint. A thread in
the otherwise idle main Ractor runs a non-blocking <code>PASSIVE</code> checkpoint every 250 ms, and a short
<code>RESTART</code> (holding the writer's permit) once the WAL passes about 32 MB.
</p>
<cf-checkpoint></cf-checkpoint>
<h2><small>6 · The ledger</small>What each change bought</h2>
<p>
Measured on a local benchmark pinned to the same 4 CPUs, before and after each round of changes. These are not the
Docker bench numbers further down, which are higher on every route.
</p>
<cf-gains></cf-gains>
<h2><small>7 · The result</small>Against Rails, Go and Rust</h2>
<p>
The bench harness is <a href="https://github.com/basecamp/once-campfire-rust">once-campfire-rust</a>'s
<code>bench/run</code> with Go and Ruby added. All four are unmodified production images on the same seed, each pinned to 4 hardware
threads of a Threadripper PRO 7975WX, with the load generator on 4 others. Values are medians of three interleaved reps.
Each route has its own linear scale; hover over a bar for its latency.
</p>
<cf-bench></cf-bench>
<h2><small>8 · Ruby head</small>The same code on ruby/ruby master</h2>
<p>
Same app, same harness, same CPUs: the release image against one built from
<a href="https://github.com/ruby/ruby/commit/df0fa43f5343f5ec18c63521592c36cde1092f7a">ruby/ruby master</a> (<code>Dockerfile.head</code>),
run once with YJIT and once with ZJIT, the new method-based JIT. No application changes. Pages where Ruby does most of the work gain the most (the messages page 1.5×). The sidebar,
which spends its time in zlib, barely moves. Peak memory drops by about 40%. Posts are bound by the single writer and don't move. ZJIT works with Ractors and runs within a few percent of YJIT.
</p>
<cf-head></cf-head>
<p class="muted" style="font-size:14px">
Medians of three reps: 4.0.7 and head · YJIT interleaved (<code>bench/results/ruby-head-zjit-20261005</code>), head · ZJIT rerun on its own with each container's boot line saved, all three reading <code>JIT: ZJIT</code> (<code>bench/results/ruby-zjit-20261005</code>). Head's cold start is slower (2.9 s against 1.0 s), likely
from the Bundler 4.1 beta it ships with. Cable p99 swings between runs on every build, so read the throughput, not the tail. One trap on the way:
the first ZJIT run was really YJIT. Master's Bundler re-executed the server under the lockfile's older Bundler, which dropped the
<code>--zjit</code> flag, so the head image now sets <code>BUNDLE_VERSION=system</code>.
</p>
<h2><small>9 · Decisions</small>The trade-offs, written down</h2>
<cf-decisions></cf-decisions>
<h2><small>10 · Not there yet</small>Where it still loses</h2>
<cf-gaps></cf-gaps>
<footer>
Data: <code>bench/results/rails-rust-ruby-20261005</code>, <code>bench/results/go-rust-ruby-20261005</code>, <code>bench/results/ruby-head-zjit-20261005</code> and <code>bench/results/ruby-zjit-20261005</code>
(October 5, 2026). The local before/after numbers come from development runs on the same machine. The writer and checkpoint animations
are illustrative simulations; the latency numbers next to them are measured. Details are in <code>docs/PORTING.md</code>.
</footer>
</main>
<script type="module">
import { LitElement, html, svg, css, nothing } from "https://esm.sh/lit@3.2.1";
const DATA = {"http":{"room_show":{"1":{"ruby":{"rps":1609,"p50":0.601,"p99":1.251},"rust":{"rps":5085,"p50":0.195,"p99":0.233},"go":{"rps":763,"p50":1.226,"p99":1.832},"rails":{"rps":81,"p50":11.367,"p99":17.215}},"16":{"ruby":{"rps":5094,"p50":2.865,"p99":5.839},"rust":{"rps":20976,"p50":0.742,"p99":1.333},"go":{"rps":3152,"p50":3.959,"p99":15.711},"rails":{"rps":189,"p50":85.567,"p99":203.519}},"64":{"ruby":{"rps":4808,"p50":12.511,"p99":25.999},"rust":{"rps":20898,"p50":2.913,"p99":5.555},"go":{"rps":3069,"p50":18.591,"p99":67.007},"rails":{"rps":166,"p50":407.551,"p99":503.295}}},"messages_page":{"1":{"ruby":{"rps":2949,"p50":0.318,"p99":0.748},"rust":{"rps":5578,"p50":0.177,"p99":0.216},"go":{"rps":1045,"p50":0.937,"p99":1.304},"rails":{"rps":152,"p50":6.323,"p99":14.775}},"16":{"ruby":{"rps":8499,"p50":1.588,"p99":4.523},"rust":{"rps":23034,"p50":0.678,"p99":1.162},"go":{"rps":4096,"p50":2.929,"p99":12.287},"rails":{"rps":335,"p50":46.143,"p99":101.055}},"64":{"ruby":{"rps":7940,"p50":7.751,"p99":17.439},"rust":{"rps":23549,"p50":2.627,"p99":4.855},"go":{"rps":4103,"p50":11.727,"p99":68.607},"rails":{"rps":305,"p50":202.111,"p99":347.903}}},"sidebar":{"1":{"ruby":{"rps":2028,"p50":0.489,"p99":0.752},"rust":{"rps":4760,"p50":0.207,"p99":0.242},"go":{"rps":3385,"p50":0.291,"p99":0.474},"rails":{"rps":195,"p50":5.015,"p99":7.143}},"16":{"ruby":{"rps":7235,"p50":2.103,"p99":3.579},"rust":{"rps":20182,"p50":0.764,"p99":1.469},"go":{"rps":11927,"p50":1.138,"p99":4.223},"rails":{"rps":480,"p50":32.351,"p99":60.575}},"64":{"ruby":{"rps":7209,"p50":8.687,"p99":13.775},"rust":{"rps":21111,"p50":2.945,"p99":5.559},"go":{"rps":12283,"p50":5.035,"p99":9.831},"rails":{"rps":480,"p50":132.095,"p99":199.423}}},"search":{"1":{"ruby":{"rps":2197,"p50":0.433,"p99":0.77},"rust":{"rps":5453,"p50":0.177,"p99":0.232},"go":{"rps":1402,"p50":0.699,"p99":1.003},"rails":{"rps":146,"p50":6.719,"p99":8.815}},"16":{"ruby":{"rps":6959,"p50":1.92,"p99":6.863},"rust":{"rps":19871,"p50":0.762,"p99":1.595},"go":{"rps":5412,"p50":2.159,"p99":9.591},"rails":{"rps":334,"p50":45.503,"p99":92.927}},"64":{"ruby":{"rps":6704,"p50":9.127,"p99":16.031},"rust":{"rps":23037,"p50":2.629,"p99":5.559},"go":{"rps":5282,"p50":10.927,"p99":36.415},"rails":{"rps":324,"p50":193.407,"p99":259.327}}},"avatar":{"1":{"ruby":{"rps":19601,"p50":0.047,"p99":0.106},"rust":{"rps":30730,"p50":0.03,"p99":0.047},"go":{"rps":19714,"p50":0.047,"p99":0.075},"rails":{"rps":14743,"p50":0.062,"p99":0.143}},"16":{"ruby":{"rps":94726,"p50":0.127,"p99":1.163},"rust":{"rps":139309,"p50":0.076,"p99":0.316},"go":{"rps":114358,"p50":0.108,"p99":0.577},"rails":{"rps":69938,"p50":0.17,"p99":0.874}},"64":{"ruby":{"rps":97807,"p50":0.562,"p99":2.137},"rust":{"rps":147420,"p50":0.3,"p99":4.839},"go":{"rps":120964,"p50":0.384,"p99":2.355},"rails":{"rps":57810,"p50":0.635,"p99":6.003}}},"static_css":{"1":{"ruby":{"rps":22286,"p50":0.041,"p99":0.088},"rust":{"rps":32924,"p50":0.028,"p99":0.043},"go":{"rps":26608,"p50":0.035,"p99":0.059},"rails":{"rps":18040,"p50":0.051,"p99":0.108}},"16":{"ruby":{"rps":111343,"p50":0.111,"p99":0.609},"rust":{"rps":138216,"p50":0.076,"p99":0.348},"go":{"rps":155372,"p50":0.08,"p99":0.377},"rails":{"rps":84150,"p50":0.14,"p99":0.758}},"64":{"ruby":{"rps":108037,"p50":0.495,"p99":2.197},"rust":{"rps":146565,"p50":0.294,"p99":5.491},"go":{"rps":170861,"p50":0.282,"p99":1.54},"rails":{"rps":75924,"p50":0.549,"p99":4.223}}},"up":{"1":{"ruby":{"rps":13535,"p50":0.068,"p99":0.157},"rust":{"rps":19729,"p50":0.048,"p99":0.067},"go":{"rps":16421,"p50":0.057,"p99":0.089},"rails":{"rps":1606,"p50":0.588,"p99":1.028}},"16":{"ruby":{"rps":61370,"p50":0.253,"p99":1.587},"rust":{"rps":118724,"p50":0.134,"p99":0.233},"go":{"rps":79600,"p50":0.157,"p99":0.787},"rails":{"rps":3436,"p50":4.463,"p99":9.823}},"64":{"ruby":{"rps":59522,"p50":0.972,"p99":3.377},"rust":{"rps":114047,"p50":0.517,"p99":1.459},"go":{"rps":81024,"p50":0.587,"p99":3.183},"rails":{"rps":3410,"p50":18.511,"p99":30.223}}},"post_message":{"1":{"ruby":{"rps":1040,"p50":0.861,"p99":3.245},"rust":{"rps":1352,"p50":0.587,"p99":6.371},"go":{"rps":704,"p50":0.761,"p99":26.303},"rails":{"rps":127,"p50":7.179,"p99":28.111}},"16":{"ruby":{"rps":1810,"p50":8.967,"p99":34.591},"rust":{"rps":2713,"p50":4.607,"p99":26.799},"go":{"rps":1077,"p50":4.171,"p99":79.807},"rails":{"rps":199,"p50":69.503,"p99":213.631}},"64":{"ruby":{"rps":1554,"p50":33.791,"p99":200.959},"rust":{"rps":2688,"p50":21.583,"p99":59.519},"go":{"rps":878,"p50":51.935,"p99":408.575},"rails":{"rps":219,"p50":272.383,"p99":2207.743}}}},"cable":[{"clients":100,"ruby":{"tput":1094,"p50":3.741,"p99":10.543},"rust":{"tput":1807,"p50":1.993,"p99":3.467},"go":{"tput":665,"p50":2.363,"p99":4.391},"rails":{"tput":73,"p50":20.799,"p99":67.007}},{"clients":500,"ruby":{"tput":312,"p50":11.015,"p99":222.335},"rust":{"tput":786,"p50":4.017,"p99":5.567},"go":{"tput":272,"p50":6.511,"p99":19.199},"rails":{"tput":21,"p50":53.631,"p99":468.991}},{"clients":1000,"ruby":{"tput":117,"p50":17.055,"p99":72.511},"rust":{"tput":431,"p50":6.639,"p99":8.831},"go":{"tput":140,"p50":11.519,"p99":26.335},"rails":{"tput":11,"p50":92.159,"p99":175.103}}],"proc":{"ruby":{"cold":2912,"upload":233.8,"peak":1162,"idle":82},"rust":{"cold":537,"upload":41.9,"peak":357,"idle":29},"go":{"cold":504,"upload":36.2,"peak":413,"idle":44},"rails":{"cold":4622,"upload":79.0,"peak":1526,"idle":439}}};
const APPS = [
{ key: "ruby", name: "Ruby (this)" },
{ key: "rust", name: "Rust" },
{ key: "go", name: "Go" },
{ key: "rails", name: "Rails" },
];
const ROUTES = [
["room_show", "Room page"], ["messages_page", "Messages page"], ["sidebar", "Sidebar"],
["search", "Search"], ["post_message", "Post a message"], ["up", "/up"],
];
const fmt = (v) => v >= 100 ? Math.round(v).toLocaleString("en-US") : v >= 10 ? v.toFixed(1) : v.toFixed(2);
const reduced = matchMedia("(prefers-reduced-motion: reduce)").matches;
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const base = css`
:host { display: block; margin: 28px 0 0; }
.card { background: var(--surface-1); border: 1px solid var(--line); border-radius: 14px; padding: 22px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
button.seg { font: 600 13px var(--sans); color: var(--text-secondary); background: var(--surface-2); border: 1px solid var(--line);
padding: 6px 12px; border-radius: 999px; cursor: pointer; }
button.seg[aria-pressed="true"] { color: var(--surface-1); background: var(--text-primary); border-color: var(--text-primary); }
.muted { color: var(--text-muted); font-size: 13px; }
.mono { font-family: var(--mono); }
svg text { font-family: var(--sans); }
`;
// Runs `fn` while the element is on screen.
class Visible extends LitElement {
connectedCallback() {
super.connectedCallback();
this._io = new IntersectionObserver(([e]) => { this.onScreen = e.isIntersecting; this.visibleChanged?.(e.isIntersecting); }, { threshold: 0.25 });
this._io.observe(this);
}
disconnectedCallback() { super.disconnectedCallback(); this._io?.disconnect(); this.onScreen = false; this.visibleChanged?.(false); }
}
/* ---------------------------------------------------------------- hero */
class Hero extends LitElement {
static styles = [base, css`
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 12px; margin-top: 36px; }
.t { background: var(--surface-1); border: 1px solid var(--line); border-radius: 14px; padding: 18px 20px; }
.n { font-size: 40px; font-weight: 700; letter-spacing: -0.03em; line-height: 1; }
.l { color: var(--text-secondary); font-size: 14px; margin-top: 8px; }
.s { color: var(--text-muted); font-size: 12px; margin-top: 4px; }
`];
render() {
const h = DATA.http;
const x = (r, c) => Math.round(h[r][c].ruby.rps / h[r][c].rails.rps);
return html`<div class="tiles">
<div class="t"><div class="n">${x("room_show", 16)}×</div><div class="l">Rails' throughput on the room page</div><div class="s">${fmt(h.room_show[16].ruby.rps)} vs ${fmt(h.room_show[16].rails.rps)} req/s, 16 clients</div></div>
<div class="t"><div class="n">${(h.messages_page[16].ruby.rps / h.messages_page[16].go.rps).toFixed(1)}×</div><div class="l">Go's throughput on message history</div><div class="s">${fmt(h.messages_page[16].ruby.rps)} vs ${fmt(h.messages_page[16].go.rps)} req/s</div></div>
<div class="t"><div class="n">${fmt(h.post_message[1].ruby.p99)}<span style="font-size:20px"> ms</span></div><div class="l">p99 latency to post a message, 1 client</div><div class="s">Rust ${fmt(h.post_message[1].rust.p99)} ms · Rails ${fmt(h.post_message[1].rails.p99)} ms</div></div>
<div class="t"><div class="n">956/956</div><div class="l">Parity checks against Rails pass</div><div class="s">HTML, DOM, accessibility tree, Cable frames, screenshots</div></div>
</div>`;
}
}
customElements.define("cf-hero", Hero);
/* ---------------------------------------------------------------- architecture */
const W = [30, 126, 222, 318].map((y, i) => ({ id: `w${i}`, x: 330, y, w: 230, h: 82, cy: y + 41 }));
const N = {
browsers: { x: 14, y: 168, w: 104, h: 96, cx: 66, cy: 216 },
kernel: { x: 160, y: 184, w: 120, h: 64, cx: 220, cy: 216 },
writer: { x: 650, y: 30, w: 170, h: 82, cy: 71 },
bus: { x: 650, y: 140, w: 170, h: 70, cy: 175 },
jobs: { x: 650, y: 238, w: 170, h: 70, cy: 273 },
log: { x: 650, y: 336, w: 170, h: 64, cy: 368 },
sqlite: { x: 330, y: 446, w: 230, h: 62, cy: 477 },
ckpt: { x: 120, y: 446, w: 170, h: 62, cy: 477 },
};
const P = {
in: () => [[118, 216], [160, 216]],
toW: (i) => [[280, 216], [305, 216], [305, W[i].cy], [330, W[i].cy]],
fromW: (i) => [[330, W[i].cy], [305, W[i].cy], [305, 216], [280, 216], [118, 216]],
wWriter: (i) => [[560, W[i].cy], [605, W[i].cy], [605, 71], [650, 71]],
writerW: (i) => [[650, 71], [605, 71], [605, W[i].cy], [560, W[i].cy]],
wBus: (i) => [[560, W[i].cy], [620, W[i].cy], [620, 175], [650, 175]],
busW: (i) => [[650, 175], [620, 175], [620, W[i].cy], [560, W[i].cy]],
wJobs: (i) => [[560, W[i].cy], [628, W[i].cy], [628, 273], [650, 273]],
wDb: (i) => [[560, W[i].cy + 18], [590, W[i].cy + 18], [590, 477], [560, 477]],
ckDb: () => [[290, 477], [330, 477]],
wBrowser: (i) => [[330, W[i].cy + 20], [140, W[i].cy + 20], [140, 250], [118, 250]],
writerDb: () => [[735, 112], [735, 130], [840, 130], [840, 500], [560, 500]],
};
class Arch extends Visible {
static properties = { mode: {}, caption: {}, hot: {}, dots: {}, queue: {}, step: {} };
static styles = [base, css`
svg { width: 100%; height: auto; display: block; }
.node rect { fill: var(--surface-2); stroke: var(--line); stroke-width: 1.5; transition: fill .25s, stroke .25s; }
.node.hot rect.box { stroke: var(--accent); stroke-width: 2.5; }
.node text.t { fill: var(--text-primary); font-size: 13px; font-weight: 650; }
.node text.s { fill: var(--text-muted); font-size: 11px; }
.chip rect { fill: var(--surface-1); stroke: var(--line); }
.chip text { fill: var(--text-secondary); font-size: 10px; }
.chip.hot rect { fill: var(--accent); stroke: var(--accent); }
.chip.hot text { fill: #fff; }
path.wire { fill: none; stroke: var(--line); stroke-width: 1.5; }
.cap { min-height: 52px; margin-top: 12px; font-size: 15px; color: var(--text-primary); display: flex; gap: 10px; align-items: baseline; }
.cap .k { font: 700 12px var(--mono); color: var(--accent); white-space: nowrap; }
.q rect { fill: var(--accent); }
.q text { fill: #fff; font-size: 10px; font-weight: 700; }
`];
constructor() { super(); this.mode = "get"; this.caption = ""; this.hot = new Set(); this.dots = []; this.queue = []; this.step = ""; this.run = 0; this.k = 0; }
visibleChanged(v) { if (v && !this.playing) this.play(); if (!v) this.run++; }
pick(m) { this.mode = m; this.run++; this.dots = []; this.hot = new Set(); this.queue = []; this.playing = false; this.play(); }
async play() {
const run = ++this.run; this.playing = true;
try { while (run === this.run) { await this.scenarios[this.mode].call(this, run); await this.wait(run, 900); } }
catch (e) { if (e !== "stop") throw e; }
finally { if (run === this.run) this.playing = false; }
}
wait(run, ms) { return sleep(reduced ? Math.min(ms, 60) : ms).then(() => { if (run !== this.run) throw "stop"; }); }
say(step, text) { this.step = step; this.caption = text; }
light(...ids) { this.hot = new Set(ids); }
// Moves a dot along a polyline.
travel(run, pts, color, ms = 700, label = "") {
const segs = []; let total = 0;
for (let i = 1; i < pts.length; i++) { const d = Math.hypot(pts[i][0] - pts[i - 1][0], pts[i][1] - pts[i - 1][1]); segs.push(d); total += d; }
const dot = { x: pts[0][0], y: pts[0][1], color, label };
this.dots = [...this.dots, dot];
return new Promise((resolve, reject) => {
const t0 = performance.now(); const dur = reduced ? 1 : ms;
const tick = (t) => {
if (run !== this.run) { reject("stop"); return; }
let p = Math.min(1, (t - t0) / dur); p = p < 0.5 ? 2 * p * p : 1 - Math.pow(-2 * p + 2, 2) / 2;
let d = p * total, i = 0;
while (i < segs.length - 1 && d > segs[i]) { d -= segs[i]; i++; }
const f = segs[i] ? d / segs[i] : 1;
dot.x = pts[i][0] + (pts[i + 1][0] - pts[i][0]) * f; dot.y = pts[i][1] + (pts[i + 1][1] - pts[i][1]) * f;
this.requestUpdate();
if (p < 1) requestAnimationFrame(tick); else { this.dots = this.dots.filter((x) => x !== dot); resolve(); }
};
requestAnimationFrame(tick);
});
}
get scenarios() {
const ruby = "var(--ruby)", rust = "var(--rust)", go = "var(--go)", rails = "var(--rails)";
return {
async get(run) {
const i = this.k++ % 4;
this.light("browsers"); this.say("GET", "A browser asks for a room page.");
await this.travel(run, P.in(), ruby, 600);
this.light("kernel"); this.say("accept()", "One listening socket is shared by every worker Ractor. Whichever one calls accept() first takes the connection: the kernel does the load balancing.");
await this.travel(run, P.toW(i), ruby, 700);
this.light(`w${i}`, `w${i}:db`, "sqlite"); this.say("BEGIN", `worker-${i} opens one read snapshot. All ~20 queries on the page run inside it at about 0.2 µs each, using this Ractor's own SQLite connection.`);
await Promise.all([this.travel(run, P.wDb(i), ruby, 600), this.wait(run, 1300)]);
this.light(`w${i}`, `w${i}:cache`); this.say("render", "The cached message fragments (frozen Strings, per Ractor) are appended into one pre-sized response String.");
await this.wait(run, 1500);
this.light(`w${i}`, `w${i}:gz`); this.say("splice", "gzip: the pre-deflated parts of the page, fragments and layout alike, are copied straight into the stream.");
await this.wait(run, 1400);
this.light("browsers"); this.say("200", "The response goes back. No other Ractor was involved and no lock was taken.");
await this.travel(run, P.fromW(i), ruby, 800);
await this.wait(run, 600);
},
async post(run) {
const i = this.k++ % 4;
this.light("browsers"); this.say("POST", "Someone posts a message.");
await this.travel(run, P.in(), rust, 500);
await this.travel(run, P.toW(i), rust, 600);
this.light(`w${i}`, "writer"); this.say("acquire", `worker-${i}'s fiber asks the writer Ractor for the write permit. Only this fiber waits; the Ractor keeps serving other requests.`);
await this.travel(run, P.wWriter(i), rust, 600, "acquire");
this.say("grant", "The permit is free, so it comes straight back. If it were held, this request would queue first-come, first-served.");
await this.travel(run, P.writerW(i), rust, 600, "grant");
this.light(`w${i}`, `w${i}:db`, "sqlite"); this.say("COMMIT", "BEGIN IMMEDIATE, then INSERT message, rich text and search row. Triggers touch the room and mark members unread. COMMIT: one transaction, on this Ractor's own connection.");
await Promise.all([this.travel(run, P.wDb(i), rust, 700), this.wait(run, 2200)]);
this.light(`w${i}`, "writer"); this.say("release", "The permit goes back to the writer, which hands it to the next waiter.");
await this.travel(run, P.wWriter(i), rust, 500, "release");
this.light(`w${i}`, "bus"); this.say("publish", "The rendered Turbo Stream goes to the Bus Ractor as a frozen String, so it crosses Ractors without a copy.");
await this.travel(run, P.wBus(i), go, 600);
this.light("bus", "w0", "w1", "w2", "w3", "jobs"); this.say("fan-out", "The Bus hands it to every worker. Each worker builds the WebSocket frame once and queues it on each subscriber it holds. Push notifications go to a job Ractor.");
await Promise.all([0, 1, 2, 3].map((j) => this.travel(run, P.busW(j), go, 700)).concat([this.travel(run, P.wJobs(i), rails, 900, "push")]));
this.light("browsers", "w0", "w1", "w2", "w3"); this.say("frames", "Every connected browser receives the message.");
await Promise.all([0, 1, 2, 3].map((j) => this.travel(run, P.wBrowser(j), go, 900)));
await this.wait(run, 500);
},
async contend(run) {
this.light("w0", "w1", "w2", "w3", "writer"); this.say("race", "Three workers want to write at the same moment.");
await Promise.all([0, 1, 3].map((j, n) => this.wait(run, n * 120).then(() => this.travel(run, P.wWriter(j), rust, 700, "acquire"))));
this.queue = [0, 1, 3]; this.requestUpdate();
this.say("FIFO", "The writer grants the permit in arrival order: worker-0, then worker-1, then worker-3. Nobody sleeps and retries, and nobody loses repeatedly.");
await this.wait(run, 900);
for (const j of [0, 1, 3]) {
this.light(`w${j}`, `w${j}:db`, "writer", "sqlite");
await this.travel(run, P.writerW(j), rust, 450, "grant");
await Promise.all([this.travel(run, P.wDb(j), rust, 500), this.wait(run, 700)]);
this.queue = this.queue.slice(1); this.requestUpdate();
await this.travel(run, P.wWriter(j), rust, 400, "release");
}
this.say("done", "Three commits back to back, with no gaps and no SQLITE_BUSY. That took post p99 at 16 clients from about 104 ms to 31 ms.");
await this.wait(run, 1600);
},
async ckpt(run) {
this.light("ckpt", "sqlite"); this.say("PASSIVE", "Every 250 ms the checkpointer thread in the main Ractor copies WAL pages into the database without blocking anyone. It runs without holding the GVL.");
await this.travel(run, P.ckDb(), rails, 600); await this.wait(run, 900);
await this.travel(run, P.ckDb(), rails, 600); await this.wait(run, 900);
this.light("ckpt", "writer", "sqlite"); this.say("RESTART", "Once the WAL passes about 32 MB, the checkpointer takes the writer's permit for a brief RESTART, so the WAL starts over from the beginning. Writers just queue for a moment.");
await this.travel(run, [[205, 446], [205, 425], [735, 425], [735, 112]], rails, 900, "acquire");
await this.travel(run, P.writerDb(), rails, 900, "restart");
this.light("log"); this.say("logs", "Logging is off the hot path too: each Ractor appends to a local buffer (about 100 ns, no allocations), and a timer moves the whole buffer to the log Ractor.");
await this.wait(run, 2200);
},
};
}
node(id, n, title, sub, extra = nothing) {
return svg`<g class="node ${this.hot.has(id) ? "hot" : ""}">
<rect class="box" x=${n.x} y=${n.y} width=${n.w} height=${n.h} rx="10"></rect>
<text class="t" x=${n.x + 12} y=${n.y + 22}>${title}</text>
<text class="s" x=${n.x + 12} y=${n.y + 38}>${sub}</text>${extra}</g>`;
}
chips(i, n) {
const items = [["fibers", 44], ["db", 48, "SQLite"], ["cache", 70, "frag cache"], ["gz", 38, "gzip"]];
let x = n.x + 10;
return items.map(([k, w, label]) => { const g = svg`<g class="chip ${this.hot.has(`w${i}:${k}`) ? "hot" : ""}">
<rect x=${x} y=${n.y + 52} width=${w} height="20" rx="5"></rect><text x=${x + w / 2} y=${n.y + 66} text-anchor="middle">${label || k}</text></g>`; x += w + 6; return g; });
}
render() {
const wires = [P.in(), ...[0, 1, 2, 3].flatMap((i) => [P.toW(i), P.wWriter(i), P.wBus(i), P.wJobs(i), P.wDb(i)]), P.ckDb()];
const d = (pts) => "M" + pts.map((p) => p.join(" ")).join(" L");
return html`<div class="card">
<div class="row">${[["get", "GET a page"], ["post", "Post a message"], ["contend", "Writers contend"], ["ckpt", "Checkpoints & logs"]].map(([k, l]) =>
html`<button class="seg" aria-pressed=${this.mode === k} @click=${() => this.pick(k)}>${l}</button>`)}</div>
<svg viewBox="0 0 840 520" role="img" aria-label="Architecture: browsers, a shared listening socket, four worker Ractors, writer, bus, jobs and log Ractors, SQLite and a checkpointer thread">
${wires.map((p) => svg`<path class="wire" d=${d(p)}></path>`)}
${this.node("browsers", N.browsers, "Browsers", "HTTP + WebSocket")}
${this.node("kernel", N.kernel, "accept()", "1 socket, N Ractors")}
${W.map((n, i) => this.node(`w${i}`, n, `worker-${i} Ractor`, "Falcon · Async reactor", this.chips(i, n)))}
${this.node("writer", N.writer, "writer Ractor", "write permit, FIFO", svg`${this.queue.map((j, n) => svg`<g class="q"><rect x=${N.writer.x + 12 + n * 26} y=${N.writer.y + 50} width="22" height="20" rx="5"></rect><text x=${N.writer.x + 23 + n * 26} y=${N.writer.y + 64} text-anchor="middle">w${j}</text></g>`)}`)}
${this.node("bus", N.bus, "bus Ractor", "fan-out to workers")}
${this.node("jobs", N.jobs, "job Ractors ×2", "push, webhooks, media")}
${this.node("log", N.log, "log Ractor", "owns fd 2")}
${this.node("sqlite", N.sqlite, "SQLite · one file, WAL", "a connection per Ractor")}
${this.node("ckpt", N.ckpt, "main Ractor", "checkpointer thread")}
${this.dots.map((p) => svg`<g><circle cx=${p.x} cy=${p.y} r="7" fill=${p.color} stroke="var(--surface-1)" stroke-width="2"></circle>
${p.label ? svg`<text x=${p.x + 11} y=${p.y - 8} font-size="11" font-weight="700" fill="var(--text-primary)" stroke="var(--surface-1)" stroke-width="4" paint-order="stroke">${p.label}</text>` : nothing}</g>`)}
</svg>
<div class="cap" aria-live="polite"><span class="k">${this.step}</span><span>${this.caption}</span></div>
</div>`;
}
}
customElements.define("cf-arch", Arch);
/* ---------------------------------------------------------------- stack */
const STACK = [
["Thruster (front proxy, gzip)", "Falcon in each worker Ractor; gzip spliced from cached parts"],
["Puma: forked processes × 5 threads", "N worker Ractors in one process, fibers inside each"],
["Rack middleware + ActionDispatch", "A 90-line router and a ~400-line controller base"],
["ActiveRecord", "Struct models with frozen SQL literals (Extralite caches statements by SQL text)"],
["Model callbacks (touch, receive, dependent)", "SQLite triggers, run inside the same write"],
["Redis + Action Cable server", "Bus Ractor + an Action Cable protocol server on async-websocket"],
["Resque workers + Redis queue", "Job Ractors fed by message passing (lost on crash, like Go/Rust)"],
["ERB via ActionView", "ERB compiled to methods that append into one String"],
["Rails.logger", "A Ractor-local buffer, moved whole to a log Ractor"],
];
class Stack extends Visible {
static properties = { shown: {} };
static styles = [base, css`
.grid { display: grid; grid-template-columns: 1fr 34px 1fr; gap: 6px 10px; align-items: center; }
.h { font: 700 12px var(--sans); letter-spacing: .08em; text-transform: uppercase; color: var(--text-muted); padding-bottom: 6px; }
.a, .b { padding: 9px 12px; border-radius: 8px; font-size: 14px; transition: opacity .5s, transform .5s, text-decoration-color .5s; }
.a { background: var(--surface-2); color: var(--text-secondary); text-decoration: line-through; text-decoration-color: transparent; }
.b { background: var(--surface-2); border-left: 3px solid var(--ruby); color: var(--text-primary); opacity: 0; transform: translateX(-12px); }
.arr { color: var(--text-muted); text-align: center; opacity: 0; transition: opacity .4s; }
.on .a { text-decoration-color: var(--bad); color: var(--text-muted); }
.on .b { opacity: 1; transform: none; }
.on .arr { opacity: 1; }
.r { display: contents; }
`];
constructor() { super(); this.shown = 0; }
visibleChanged(v) {
if (!v || this.shown) return;
STACK.forEach((_, i) => setTimeout(() => { this.shown = i + 1; }, reduced ? 0 : 250 + i * 260));
}
render() {
return html`<div class="card"><div class="grid">
<div class="h">Rails Campfire</div><div></div><div class="h">This port</div>
${STACK.map(([a, b], i) => html`<div class="r ${i < this.shown ? "on" : ""}"><div class="a">${a}</div><div class="arr">→</div><div class="b">${b}</div></div>`)}
</div></div>`;
}
}
customElements.define("cf-stack", Stack);
/* ---------------------------------------------------------------- snapshot */
class Snapshot extends Visible {
static properties = { t: {} };
static styles = [base, css`
svg { width: 100%; height: auto; display: block; margin-top: 8px; }
text { font-size: 12px; fill: var(--text-secondary); }
text.b { font-weight: 700; fill: var(--text-primary); font-size: 13px; }
.setup { fill: var(--rails); } .q { fill: var(--ruby); }
`];
constructor() { super(); this.t = 0; }
visibleChanged(v) {
if (!v) { cancelAnimationFrame(this.raf); return; }
const t0 = performance.now(); const dur = reduced ? 1 : 5200;
const tick = (t) => { this.t = Math.min(1, ((t - t0) % (dur + 1800)) / dur); this.raf = requestAnimationFrame(tick); };
this.raf = requestAnimationFrame(tick);
}
lane(y, label, items, total) {
const scale = 720 / 95; // µs → px, 95 µs fits
const now = this.t * 95; let x = 0;
return svg`<text class="b" x="0" y=${y - 10}>${label}</text>
${items.map(([kind, us]) => { const x0 = x; x += us; const w = Math.max(0, Math.min(us, now - x0)) * scale;
return w > 0 ? svg`<rect class=${kind} x=${120 + x0 * scale} y=${y} width=${Math.max(w - (kind === "q" ? 0.6 : 0), 0.4)} height="26" rx="2"></rect>` : nothing; })}
<text x=${120 + Math.min(now, total) * scale + 8} y=${y + 18} class="b">${now >= total ? `${total.toFixed(1)} µs` : ""}</text>`;
}
render() {
const auto = []; for (let i = 0; i < 20; i++) auto.push(["setup", 4.5], ["q", 0.2]);
const snap = [["setup", 4.5]]; for (let i = 0; i < 20; i++) snap.push(["q", 0.2]);
return html`<div class="card">
<div class="row"><span class="muted">20 point queries for one page. <span style="color:var(--rails)">■</span> read-transaction setup (4.5 µs) <span style="color:var(--ruby)">■</span> the query itself (0.2 µs)</span></div>
<svg viewBox="0 0 920 150" role="img" aria-label="Autocommit: 20 times 4.7 microseconds is 94 microseconds; one snapshot: 4.5 plus 20 times 0.2 is 8.5 microseconds">
<text x="0" y="40">Autocommit</text>${this.lane(30, "", auto, 94)}
<text x="0" y="112">One snapshot</text>${this.lane(102, "", snap, 8.5)}
</svg>
<div class="muted">About 11× less database overhead per page, and the snapshot gives every query on the page the same consistent view. This is <code>Connection#read</code> in <code>lib/campfire/db.rb</code>.</div>
</div>`;
}
}
customElements.define("cf-snapshot", Snapshot);
/* ---------------------------------------------------------------- splice */
class Splice extends Visible {
static properties = { mode: {}, p: {} };
static styles = [base, css`
svg { width: 100%; height: auto; display: block; margin-top: 12px; }
text { font-size: 12px; fill: var(--text-secondary); }
.lay { fill: var(--rails); } .frag { fill: var(--ruby); } .z { fill: var(--go); }
.box { fill: none; stroke: var(--text-muted); stroke-dasharray: 4 3; }
text.b { fill: var(--text-primary); font-weight: 700; font-size: 13px; }
`];
constructor() { super(); this.mode = "after"; this.p = 0; }
visibleChanged(v) {
if (!v) { cancelAnimationFrame(this.raf); return; }
const t0 = performance.now();
const tick = (t) => { this.p = reduced ? 1 : ((t - t0) % 4600) / 3600; this.raf = requestAnimationFrame(tick); };
this.raf = requestAnimationFrame(tick);
}
render() {
const parts = [["lay", 70], ["frag", 120], ["frag", 90], ["frag", 140], ["frag", 110], ["frag", 100], ["lay", 50]];
const p = Math.min(1, this.p); const after = this.mode === "after";
let x = 20; const top = [];
parts.forEach(([k, w], i) => {
const live = !after;
const start = i / parts.length * 0.6, prog = Math.max(0, Math.min(1, (p - start) / 0.4));
const ty = 30 + prog * 120;
top.push(svg`<rect class=${k} x=${x} y="30" width=${w - 3} height="28" rx="3" opacity="0.35"></rect>`);
if (live) {
top.push(svg`<rect class=${k} x=${x} y=${ty} width=${(w - 3) * (1 - 0.72 * Math.max(0, (prog - 0.5) * 2))} height="28" rx="3"></rect>`);
} else {
top.push(svg`<rect class="z" x=${x} y=${ty} width=${(w - 3) * 0.28} height="28" rx="3"></rect>`);
}
x += w;
});
return html`<div class="card">
<div class="row">
<button class="seg" aria-pressed=${!after} @click=${() => (this.mode = "before")}>Before: gzip the whole page</button>
<button class="seg" aria-pressed=${after} @click=${() => (this.mode = "after")}>After: splice cached deflate</button>
</div>
<svg viewBox="0 0 760 230" role="img" aria-label="Page parts flowing through deflate">
<text class="b" x="20" y="20">Rendered page, ~460 KB</text>
${top}
<rect class="box" x="14" y="92" width="690" height="44" rx="8"></rect>
<text x="24" y="120">${after ? "every part arrives already compressed (green): the layout once per distinct text, fragments once per predecessor" : "deflate (level 6) runs over every byte, on every request"}</text>
<text class="b" x="20" y="214">${after ? "within ~1% of compressing the page whole" : "≈2.4 ms of CPU per room page, 1.9 ms of it in gzip"}</text>
<text x="20" y="196"><tspan fill="var(--rails)">■</tspan> layout <tspan fill="var(--ruby)">■</tspan> cached message fragments <tspan fill="var(--go)">■</tspan> pre-deflated piece</text>
</svg>
<div class="muted">A fragment's piece is keyed by the identity of its frozen fragment String plus the fragments before it, so it stays valid until a message changes. A layout piece is keyed by the SHA-256 of its text and of the fragment before it. The ETag hashes the parts' digests, not the body.</div>
</div>`;
}
}
customElements.define("cf-splice", Splice);
/* ---------------------------------------------------------------- writer simulation */
function rng(seed) { return () => { seed = (seed * 1664525 + 1013904223) >>> 0; return seed / 2 ** 32; }; }
// Illustrative: 12 clients in a closed loop, each holding the write lock 0.45 ms per commit.
function simulate(fifo) {
const R = rng(7), H = 0.45, END = 60, n = 12;
const segs = []; const waits = [];
let lockFree = 0; const queue = [];
const ev = []; // [time, client, kind]
for (let c = 0; c < n; c++) ev.push([R() * 2, c, "want"]);
const st = Array.from({ length: n }, () => ({ since: 0, pause: 0.2 }));
let holder = -1, busyUntil = 0;
const push = (e) => { ev.push(e); ev.sort((a, b) => a[0] - b[0]); };
ev.sort((a, b) => a[0] - b[0]);
while (ev.length) {
const [t, c, kind] = ev.shift(); if (t > END) break;
if (kind === "want") { st[c].since = t; st[c].pause = 0.2; kind2(t, c); }
else if (kind === "retry") kind2(t, c);
else if (kind === "done") {
holder = -1;
if (fifo && queue.length) { const nx = queue.shift(); grant(t + 0.02, nx); }
push([t + 0.15 + R() * 0.6, c, "want"]);
}
}
function kind2(t, c) {
if (holder === -1 && t >= busyUntil) grant(t, c);
else if (fifo) { queue.push(c); }
else { push([t + st[c].pause, c, "retry"]); if (st[c].pause < 5) st[c].pause *= 2; }
}
function grant(t, c) {
holder = c; busyUntil = t + H;
segs.push({ c, a: st[c].since, b: t, kind: "wait" }, { c, a: t, b: t + H, kind: "hold" });
waits.push(t - st[c].since);
push([t + H, c, "done"]);
}
waits.sort((a, b) => a - b);
const done = segs.filter((s) => s.kind === "hold" && s.b <= END).length;
return { segs, n, END, done, p99: waits[Math.floor(waits.length * 0.99)] || 0, max: waits[waits.length - 1] || 0 };
}
class Writer extends Visible {
static properties = { fifo: {}, p: {} };
static styles = [base, css`
svg { width: 100%; height: auto; display: block; margin-top: 12px; }
text { font-size: 11px; fill: var(--text-muted); }
.stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; margin-top: 12px; }
.stat { background: var(--surface-2); border-radius: 10px; padding: 10px 14px; }
.stat b { display: block; font-size: 22px; letter-spacing: -0.02em; }
.stat span { font-size: 12px; color: var(--text-muted); }
.measured { margin-top: 14px; padding: 12px 14px; border-left: 3px solid var(--ruby); background: var(--surface-2); border-radius: 0 10px 10px 0; font-size: 14px; }
`];
constructor() { super(); this.fifo = false; this.p = 0; this.sims = { false: simulate(false), true: simulate(true) }; }
visibleChanged(v) {
if (!v) { cancelAnimationFrame(this.raf); return; }
const t0 = performance.now();
const tick = (t) => { this.p = reduced ? 1 : Math.min(1, ((t - t0) % 7500) / 6000); this.raf = requestAnimationFrame(tick); };
this.raf = requestAnimationFrame(tick);
}
set(f) { this.fifo = f; }
render() {
const s = this.sims[this.fifo]; const sx = 800 / s.END, now = this.p * s.END; const rowH = 16;
return html`<div class="card">
<div class="row">
<button class="seg" aria-pressed=${!this.fifo} @click=${() => this.set(false)}>Before: race for SQLite's lock</button>
<button class="seg" aria-pressed=${this.fifo} @click=${() => this.set(true)}>After: writer Ractor, FIFO permit</button>
<span class="muted">Simulation: 12 writers, 0.45 ms per commit, 60 ms window</span>
</div>
<svg viewBox="0 0 850 ${s.n * rowH + 30}" role="img" aria-label="Timeline of writers waiting (gray) and holding the lock (color)">
${Array.from({ length: s.n }, (_, c) => svg`<text x="0" y=${c * rowH + 12}>w${c + 1}</text>`)}
${s.segs.filter((g) => g.a < now).map((g) => svg`<rect x=${36 + g.a * sx} y=${g.c * rowH + 2} width=${Math.max(0.8, (Math.min(g.b, now) - g.a) * sx)} height=${rowH - 5} rx="2"
fill=${g.kind === "hold" ? "var(--ruby)" : "var(--wait)"} opacity=${g.kind === "hold" ? 1 : 0.55}></rect>`)}
<line x1=${36 + now * sx} x2=${36 + now * sx} y1="0" y2=${s.n * rowH} stroke="var(--text-primary)" stroke-width="1"></line>
<text x="36" y=${s.n * rowH + 22}>0 ms</text><text x="836" y=${s.n * rowH + 22} text-anchor="end">${s.END} ms</text>
<text x="300" y=${s.n * rowH + 22}><tspan fill="var(--wait)">■</tspan> waiting <tspan fill="var(--ruby)">■</tspan> holding the write lock</text>
</svg>
<div class="stats">
<div class="stat"><b>${s.done}</b><span>commits in the window</span></div>
<div class="stat"><b>${s.p99.toFixed(1)} ms</b><span>p99 wait (simulated)</span></div>
<div class="stat"><b>${s.max.toFixed(1)} ms</b><span>longest wait (simulated)</span></div>
</div>
<div class="measured"><b>Measured</b>, posting at 16 clients: p99 <b>~104 ms → 31 ms</b>; at 4 clients <b>21 → 10 ms</b>.
The lock no longer sits idle while sleepers back off, and no retry loses over and over.</div>
</div>`;
}
}
customElements.define("cf-writer", Writer);
/* ---------------------------------------------------------------- triggers */
class Triggers extends Visible {
static properties = { after: {}, n: {} };
static styles = [base, css`
.cols { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 14px; }
@media (max-width: 700px) { .cols { grid-template-columns: 1fr; } }
.tx { border: 1.5px dashed var(--line); border-radius: 10px; padding: 10px; margin-bottom: 10px; }
.tx > .h { font: 700 11px var(--mono); color: var(--text-muted); margin-bottom: 6px; }
.st { font: 12.5px var(--mono); padding: 6px 9px; border-radius: 6px; margin: 4px 0; background: var(--surface-2); color: var(--text-primary);
transition: opacity .35s, transform .35s; }
.st.trg { margin-left: 22px; background: transparent; border: 1px solid var(--go); color: var(--text-secondary); }
.st.trg::before { content: "trigger ↳ "; color: var(--go); font-weight: 700; }
.hid { opacity: 0; transform: translateY(6px); }
.sum { display: flex; gap: 22px; margin-top: 8px; font-size: 14px; color: var(--text-secondary); }
.sum b { color: var(--text-primary); font-size: 20px; margin-right: 4px; }
h4 { margin: 0 0 8px; font-size: 14px; }
`];
constructor() { super(); this.n = 0; }
visibleChanged(v) {
clearInterval(this.iv); if (!v) return;
this.n = 0; this.iv = setInterval(() => { this.n = this.n >= 9 ? 0 : this.n + 1; }, reduced ? 50 : 650);
}
st(i, text, trg = false) { return html`<div class="st ${trg ? "trg" : ""} ${i < this.n ? "" : "hid"}">${text}</div>`; }
render() {
return html`<div class="card">
<div class="cols">
<div><h4>Rails callbacks (and the port's first version)</h4>
<div class="tx"><div class="h">transaction 1</div>
${this.st(0, "INSERT INTO messages …")}${this.st(1, "INSERT INTO action_text_rich_texts …")}
${this.st(2, "UPDATE rooms SET updated_at -- touch")}${this.st(3, "INSERT INTO message_search_index …")}</div>
<div class="tx"><div class="h">transaction 2 · Room#receive</div>
${this.st(4, "UPDATE memberships SET unread_at … WHERE …")}</div>
</div>
<div><h4>Now</h4>
<div class="tx"><div class="h">one transaction</div>
${this.st(0, "INSERT INTO messages …")}
${this.st(1, "UPDATE rooms SET updated_at = NEW.updated_at", true)}
${this.st(2, "UPDATE memberships SET unread_at = NEW.created_at …", true)}
${this.st(3, "INSERT INTO action_text_rich_texts …")}${this.st(4, "INSERT INTO message_search_index …")}</div>
</div>
</div>
<div class="sum"><span><b>2 → 1</b>commits per post</span><span><b>5 → 3</b>statements sent from Ruby</span><span><b>2 → 1</b>write permits</span></div>
</div>`;
}
}
customElements.define("cf-triggers", Triggers);
/* ---------------------------------------------------------------- checkpoint strip */
class Checkpoint extends Visible {
static properties = { bg: {}, off: {} };
static styles = [base, css`
svg { width: 100%; height: auto; display: block; margin-top: 12px; }
text { font-size: 11px; fill: var(--text-muted); }
`];
constructor() { super(); this.bg = true; this.off = 0; const R = rng(3);
this.commits = Array.from({ length: 400 }, (_, i) => ({ fg: (i % 30 === 29) ? 10 + R() * 10 : 0.3 + R() * 0.25, bg: 0.3 + R() * 0.25 })); }
visibleChanged(v) {
if (!v) { cancelAnimationFrame(this.raf); return; }
const t0 = performance.now();
const tick = (t) => { this.off = reduced ? 0 : ((t - t0) / 40) % 200; this.raf = requestAnimationFrame(tick); };
this.raf = requestAnimationFrame(tick);
}
render() {
const H = 140, ms = (v) => H - 10 - Math.min(v, 21) / 21 * (H - 24); const start = Math.floor(this.off);
const view = this.commits.slice(start, start + 120);
return html`<div class="card">
<div class="row">
<button class="seg" aria-pressed=${!this.bg} @click=${() => (this.bg = false)}>Before: auto-checkpoint in COMMIT</button>
<button class="seg" aria-pressed=${this.bg} @click=${() => (this.bg = true)}>After: background checkpointer</button>
</div>
<svg viewBox="0 0 860 ${H + 40}" role="img" aria-label="Commit latency per post; before, every 30th spikes to 10 to 20 ms; after, flat">
${[0, 10, 20].map((v) => svg`<line x1="40" x2="860" y1=${ms(v)} y2=${ms(v)} stroke="var(--line)"></line><text x="0" y=${ms(v) + 4}>${v} ms</text>`)}
${view.map((c, i) => { const v = this.bg ? c.bg : c.fg; return svg`<rect x=${44 + i * 6.8} y=${ms(v)} width="4.5" height=${Math.max(1.5, H - 10 - ms(v))} rx="1.5"
fill=${v > 5 ? "var(--bad)" : "var(--ruby)"}></rect>`; })}
${this.bg ? Array.from({ length: 6 }, (_, k) => { const x = 44 + ((k * 140 - (this.off * 6.8) % 140) + 840) % 840; return svg`<rect x=${x} y=${H + 6} width="26" height="10" rx="3" fill="var(--rails)"></rect>`; }) : nothing}
<text x="44" y=${H + 34}>${this.bg ? "checkpointer thread: PASSIVE every 250 ms, off the request path" : "each bar is one post's COMMIT"}</text>
</svg>
<div class="muted">Measured: p99 post latency at 1 client went from <b>17 ms to 3 ms</b>, and throughput at 16 clients from <b>~950 to ~1,850 req/s</b>. The WAL stays at roughly 67–79 MB under sustained load.</div>
</div>`;
}
}
customElements.define("cf-checkpoint", Checkpoint);
/* ---------------------------------------------------------------- gains */
const GAINS = [
{ what: "Room page", c: "c=1", a: 383, b: 1200, why: "" },
{ what: "Room page", c: "c=16", a: 1486, b: 2900, why: "spliced gzip + ETags; read snapshot" },
{ what: "Messages page", c: "c=1", a: 617, b: 2900, why: "" },
{ what: "Messages page", c: "c=16", a: 2291, b: 6100, why: "spliced gzip; per-Ractor fragment cache" },
{ what: "Search", c: "c=1", a: 840, b: 1860, why: "" },
{ what: "Search", c: "c=16", a: 2465, b: 3300, why: "spliced gzip over result fragments" },
];
class Gains extends Visible {
static properties = { on: {} };
static styles = [base, css`
.g { display: grid; grid-template-columns: 170px 1fr; gap: 6px 14px; align-items: center; }
.lab { font-size: 14px; color: var(--text-primary); } .lab span { color: var(--text-muted); font-size: 12px; margin-left: 6px; }
.bars { position: relative; height: 30px; }
.bar { position: absolute; left: 0; height: 13px; border-radius: 0 4px 4px 0; transition: width 1.1s cubic-bezier(.2,.8,.2,1); }
.a { top: 0; background: var(--wait); } .b { top: 15px; background: var(--ruby); }
.v { position: absolute; font-size: 11.5px; color: var(--text-secondary); white-space: nowrap; transition: left 1.1s cubic-bezier(.2,.8,.2,1); }
.why { grid-column: 2; font-size: 12px; color: var(--text-muted); margin: -2px 0 14px; }
.other { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 10px; margin-top: 18px; }
.o { background: var(--surface-2); border-radius: 10px; padding: 12px 14px; font-size: 13px; color: var(--text-secondary); }
.o b { display: block; font-size: 20px; color: var(--text-primary); letter-spacing: -0.02em; }
`];
constructor() { super(); this.on = false; }
visibleChanged(v) { if (v) setTimeout(() => (this.on = true), 100); }
render() {
const max = 6100;
return html`<div class="card">
<div class="muted" style="margin-bottom:12px"><span style="color:var(--wait)">■</span> before <span style="color:var(--ruby)">■</span> after · req/s, local pinned bench, 4 worker Ractors</div>
<div class="g">${GAINS.map((g) => html`
<div class="lab">${g.what}<span>${g.c}</span></div>
<div class="bars">
<div class="bar a" style="width:${this.on ? g.a / max * 82 : 0}%"></div><div class="v" style="top:-1px;left:calc(${this.on ? g.a / max * 82 : 0}% + 6px)">${fmt(g.a)}</div>
<div class="bar b" style="width:${this.on ? g.b / max * 82 : 0}%"></div><div class="v" style="top:14px;left:calc(${this.on ? g.b / max * 82 : 0}% + 6px)"><b>${fmt(g.b)}</b> · ${(g.b / g.a).toFixed(1)}×</div>
</div>${g.why ? html`<div class="why">${g.why}</div>` : nothing}`)}
</div>
<div class="other">
<div class="o"><b>2 → ~1,200 req/s</b>concurrent posts, after moving SQLite lock waits from its busy handler into Ruby (see decisions)</div>
<div class="o"><b>17 → 3 ms</b>post p99 at 1 client, from background checkpoints</div>
<div class="o"><b>~104 → 31 ms</b>post p99 at 16 clients, from the single writer</div>
<div class="o"><b>1,000 cable clients</b>reachable after raising the file-descriptor limit to the hard limit, as Go does; at 1,024, accept() failed and the server stopped answering</div>
</div>
</div>`;
}
}
customElements.define("cf-gains", Gains);
/* ---------------------------------------------------------------- bench */
class Bench extends Visible {
static properties = { conc: {}, table: {}, tip: {}, on: {} };
static styles = [base, css`
.multi { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 22px 28px; margin-top: 18px; }
h4 { margin: 0 0 8px; font-size: 14px; display: flex; justify-content: space-between; }
h4 span { font-weight: 500; color: var(--text-muted); font-size: 12px; }
.b { display: grid; grid-template-columns: 78px 1fr; align-items: center; gap: 6px; height: 24px; position: relative; }
.n { font-size: 12px; color: var(--text-secondary); }
.track { position: relative; height: 18px; }
.fill { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 0 4px 4px 0; transition: width .9s cubic-bezier(.2,.8,.2,1); }
.val { position: absolute; top: 1px; font-size: 11.5px; color: var(--text-primary); white-space: nowrap; transition: left .9s cubic-bezier(.2,.8,.2,1); }
.hit { position: absolute; inset: -3px 0; cursor: default; }
.tip { position: fixed; pointer-events: none; background: var(--text-primary); color: var(--surface-1); padding: 8px 10px; border-radius: 8px; font-size: 12px; z-index: 5; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 13px; color: var(--text-secondary); }
.sw { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }
table { border-collapse: collapse; width: 100%; font-size: 13px; margin-top: 14px; }
th, td { text-align: right; padding: 5px 8px; border-bottom: 1px solid var(--line); }
th:first-child, td:first-child { text-align: left; }
th { color: var(--text-muted); font-weight: 600; }
h3 { font-size: 16px; margin: 34px 0 6px; }
`];
constructor() { super(); this.conc = 16; this.table = false; this.tip = null; this.on = false; }
visibleChanged(v) { if (v) setTimeout(() => (this.on = true), 80); }
bar(app, v, max, label, tip) {
const w = this.on ? Math.max(0.6, v / max * 78) : 0;
return html`<div class="b"><div class="n">${app.name}</div><div class="track">
<div class="fill" style="width:${w}%;background:var(--${app.key})"></div>
<div class="val" style="left:calc(${w}% + 6px)">${label}</div>
<div class="hit" @mousemove=${(e) => (this.tip = { x: e.clientX, y: e.clientY, t: tip })} @mouseleave=${() => (this.tip = null)}></div>
</div></div>`;
}
render() {
const h = DATA.http, c = this.conc;
const legend = html`<div class="legend">${APPS.map((a) => html`<span><i class="sw" style="background:var(--${a.key})"></i>${a.name}</span>`)}</div>`;
return html`<div class="card">
<div class="row" style="justify-content:space-between">
<div class="row">${[1, 16, 64].map((k) => html`<button class="seg" aria-pressed=${c === k} @click=${() => (this.conc = k)}>${k} client${k > 1 ? "s" : ""}</button>`)}</div>
<div class="row">${legend}<button class="seg" aria-pressed=${this.table} @click=${() => (this.table = !this.table)}>Table</button></div>
</div>
${this.table ? html`<table><tr><th>Route, ${c} clients</th>${APPS.map((a) => html`<th>${a.name} req/s</th><th>p99 ms</th>`)}</tr>
${ROUTES.map(([r, label]) => html`<tr><td>${label}</td>${APPS.map((a) => html`<td>${fmt(h[r][c][a.key].rps)}</td><td>${fmt(h[r][c][a.key].p99)}</td>`)}</tr>`)}</table>`
: html`<div class="multi">${ROUTES.map(([r, label]) => {
const max = Math.max(...APPS.map((a) => h[r][c][a.key].rps));
return html`<div><h4>${label}<span>Ruby = ${(h[r][c].ruby.rps / h[r][c].rails.rps).toFixed(1)}× Rails · ${(h[r][c].ruby.rps / h[r][c].rust.rps).toFixed(2)}× Rust</span></h4>
${APPS.map((a) => { const m = h[r][c][a.key]; return this.bar(a, m.rps, max, `${fmt(m.rps)} req/s`, `${a.name} · ${label} · ${c} clients\n${fmt(m.rps)} req/s · p50 ${fmt(m.p50)} ms · p99 ${fmt(m.p99)} ms`); })}</div>`;
})}</div>`}
<h3>Action Cable fan-out</h3>
<div class="muted">Messages delivered per second with N subscribers in one room. Every client received every message in every run.</div>
<div class="multi">${DATA.cable.map((row) => { const max = Math.max(...APPS.map((a) => row[a.key].tput));
return html`<div><h4>${row.clients.toLocaleString()} clients<span>Ruby p99 ${fmt(row.ruby.p99)} ms · Rust ${fmt(row.rust.p99)} ms</span></h4>
${APPS.map((a) => { const m = row[a.key]; return this.bar(a, m.tput, max, `${fmt(m.tput)} msg/s`, `${a.name} · ${row.clients} clients\n${fmt(m.tput)} msg/s · p50 ${fmt(m.p50)} ms · p99 ${fmt(m.p99)} ms`); })}</div>`; })}</div>
<h3>Footprint</h3>
<table><tr><th></th>${APPS.map((a) => html`<th>${a.name}</th>`)}</tr>
<tr><td>Cold start until /up answers</td>${APPS.map((a) => html`<td>${(DATA.proc[a.key].cold / 1000).toFixed(1)} s</td>`)}</tr>
<tr><td>Idle container memory</td>${APPS.map((a) => html`<td>${fmt(DATA.proc[a.key].idle)} MB</td>`)}</tr>
<tr><td>Peak container memory</td>${APPS.map((a) => html`<td>${fmt(DATA.proc[a.key].peak)} MB</td>`)}</tr>
<tr><td>Upload until thumbnail is served</td>${APPS.map((a) => html`<td>${fmt(DATA.proc[a.key].upload)} ms</td>`)}</tr>
</table>
${this.tip ? html`<div class="tip" style="left:${this.tip.x + 14}px;top:${this.tip.y + 14}px;white-space:pre">${this.tip.t}</div>` : nothing}
</div>`;
}
}
customElements.define("cf-bench", Bench);
/* ---------------------------------------------------------------- ruby head */
const HEAD = {"http":{"room_show":{"1":{"ruby":{"rps":1654,"p50":0.585,"p99":1.253},"ruby-head":{"rps":1831,"p50":0.541,"p99":0.721},"ruby-zjit":{"rps":1742,"p50":0.572,"p99":0.723}},"16":{"ruby":{"rps":5168,"p50":2.891,"p99":5.867},"ruby-head":{"rps":6214,"p50":2.531,"p99":2.923},"ruby-zjit":{"rps":5995,"p50":2.619,"p99":3.007}},"64":{"ruby":{"rps":4943,"p50":12.407,"p99":23.535},"ruby-head":{"rps":5942,"p50":10.519,"p99":21.199},"ruby-zjit":{"rps":5682,"p50":10.983,"p99":21.343}}},"messages_page":{"1":{"ruby":{"rps":3015,"p50":0.312,"p99":0.746},"ruby-head":{"rps":3492,"p50":0.277,"p99":0.407},"ruby-zjit":{"rps":3384,"p50":0.285,"p99":0.403}},"16":{"ruby":{"rps":8492,"p50":1.618,"p99":4.819},"ruby-head":{"rps":12749,"p50":1.131,"p99":2.009},"ruby-zjit":{"rps":12064,"p50":1.343,"p99":1.875}},"64":{"ruby":{"rps":8065,"p50":8.463,"p99":18.303},"ruby-head":{"rps":12068,"p50":5.075,"p99":6.839},"ruby-zjit":{"rps":11405,"p50":5.431,"p99":7.731}}},"sidebar":{"1":{"ruby":{"rps":2057,"p50":0.478,"p99":0.732},"ruby-head":{"rps":2047,"p50":0.486,"p99":0.612},"ruby-zjit":{"rps":2037,"p50":0.488,"p99":0.611}},"16":{"ruby":{"rps":7184,"p50":2.133,"p99":3.803},"ruby-head":{"rps":7414,"p50":2.073,"p99":3.629},"ruby-zjit":{"rps":7472,"p50":2.105,"p99":2.947}},"64":{"ruby":{"rps":7133,"p50":8.823,"p99":12.375},"ruby-head":{"rps":7237,"p50":8.439,"p99":11.271},"ruby-zjit":{"rps":7215,"p50":8.495,"p99":14.007}}},"search":{"1":{"ruby":{"rps":2178,"p50":0.444,"p99":0.749},"ruby-head":{"rps":2330,"p50":0.422,"p99":0.546},"ruby-zjit":{"rps":2229,"p50":0.439,"p99":0.582}},"16":{"ruby":{"rps":6947,"p50":2.461,"p99":7.323},"ruby-head":{"rps":8169,"p50":1.88,"p99":2.673},"ruby-zjit":{"rps":8108,"p50":1.918,"p99":3.131}},"64":{"ruby":{"rps":6638,"p50":9.351,"p99":17.391},"ruby-head":{"rps":7956,"p50":7.447,"p99":18.815},"ruby-zjit":{"rps":7788,"p50":7.655,"p99":18.591}}},"avatar":{"1":{"ruby":{"rps":19469,"p50":0.049,"p99":0.099},"ruby-head":{"rps":19334,"p50":0.048,"p99":0.102},"ruby-zjit":{"rps":18854,"p50":0.05,"p99":0.116}},"16":{"ruby":{"rps":92457,"p50":0.129,"p99":1.148},"ruby-head":{"rps":109642,"p50":0.147,"p99":0.253},"ruby-zjit":{"rps":114366,"p50":0.129,"p99":0.247}},"64":{"ruby":{"rps":98093,"p50":0.559,"p99":2.197},"ruby-head":{"rps":116078,"p50":0.538,"p99":0.738},"ruby-zjit":{"rps":111233,"p50":0.569,"p99":0.865}}},"static_css":{"1":{"ruby":{"rps":21632,"p50":0.044,"p99":0.088},"ruby-head":{"rps":22193,"p50":0.043,"p99":0.103},"ruby-zjit":{"rps":21407,"p50":0.044,"p99":0.108}},"16":{"ruby":{"rps":111897,"p50":0.111,"p99":0.567},"ruby-head":{"rps":131632,"p50":0.107,"p99":0.219},"ruby-zjit":{"rps":116754,"p50":0.128,"p99":0.276}},"64":{"ruby":{"rps":110441,"p50":0.495,"p99":2.145},"ruby-head":{"rps":134312,"p50":0.468,"p99":0.785},"ruby-zjit":{"rps":128590,"p50":0.488,"p99":0.764}}},"up":{"1":{"ruby":{"rps":13469,"p50":0.072,"p99":0.157},"ruby-head":{"rps":13143,"p50":0.072,"p99":0.136},"ruby-zjit":{"rps":12671,"p50":0.076,"p99":0.155}},"16":{"ruby":{"rps":61591,"p50":0.217,"p99":1.554},"ruby-head":{"rps":70998,"p50":0.213,"p99":0.406},"ruby-zjit":{"rps":66440,"p50":0.212,"p99":0.645}},"64":{"ruby":{"rps":60555,"p50":0.964,"p99":3.077},"ruby-head":{"rps":67898,"p50":0.914,"p99":1.325},"ruby-zjit":{"rps":63892,"p50":1.002,"p99":1.752}}},"post_message":{"1":{"ruby":{"rps":1018,"p50":0.866,"p99":3.221},"ruby-head":{"rps":1052,"p50":0.842,"p99":1.937},"ruby-zjit":{"rps":1005,"p50":0.871,"p99":2.293}},"16":{"ruby":{"rps":1773,"p50":8.043,"p99":34.943},"ruby-head":{"rps":1852,"p50":7.439,"p99":35.807},"ruby-zjit":{"rps":1829,"p50":7.699,"p99":36.255}},"64":{"ruby":{"rps":1727,"p50":31.407,"p99":104.383},"ruby-head":{"rps":1811,"p50":32.159,"p99":88.511},"ruby-zjit":{"rps":1634,"p50":34.207,"p99":102.655}}}},"cable":[{"clients":100,"ruby":{"tput":1089,"p50":5.483,"p99":13.551},"ruby-head":{"tput":1245,"p50":5.803,"p99":118.911},"ruby-zjit":{"tput":1233,"p50":5.339,"p99":30.319}},{"clients":500,"ruby":{"tput":285,"p50":11.191,"p99":23.727},"ruby-head":{"tput":456,"p50":8.935,"p99":14.991},"ruby-zjit":{"tput":461,"p50":8.391,"p99":27.679}},{"clients":1000,"ruby":{"tput":115,"p50":16.591,"p99":56.223},"ruby-head":{"tput":258,"p50":14.391,"p99":67.263},"ruby-zjit":{"tput":230,"p50":14.303,"p99":21.583}}],"proc":{"ruby":{"cold":1042,"upload":239.0,"peak":1247,"idle":64},"ruby-head":{"cold":2899,"upload":244.0,"peak":723,"idle":65},"ruby-zjit":{"cold":916,"upload":236.8,"peak":724,"idle":70}}};
const VARIANTS = [
{ key: "ruby", name: "4.0.7 · YJIT", color: "var(--wait)" },
{ key: "ruby-head", name: "head · YJIT", color: "var(--ruby)" },
{ key: "ruby-zjit", name: "head · ZJIT", color: "var(--zjit)" },
];
class Head extends Visible {
static properties = { conc: {}, table: {}, tip: {}, on: {} };
static styles = [base, css`
.multi { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 22px 28px; margin-top: 18px; }
h4 { margin: 0 0 8px; font-size: 14px; display: flex; justify-content: space-between; }
h4 span { font-weight: 500; color: var(--text-muted); font-size: 12px; }
.b { display: grid; grid-template-columns: 88px 1fr; align-items: center; gap: 6px; height: 24px; position: relative; }
.n { font-size: 12px; color: var(--text-secondary); white-space: nowrap; }
.track { position: relative; height: 18px; }
.fill { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 0 4px 4px 0; transition: width .9s cubic-bezier(.2,.8,.2,1); }
.val { position: absolute; top: 1px; font-size: 11.5px; color: var(--text-primary); white-space: nowrap; transition: left .9s cubic-bezier(.2,.8,.2,1); }
.val i { font-style: normal; color: var(--text-muted); }
.hit { position: absolute; inset: -3px 0; }
.tip { position: fixed; pointer-events: none; background: var(--text-primary); color: var(--surface-1); padding: 8px 10px; border-radius: 8px; font-size: 12px; z-index: 5; white-space: pre; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 13px; color: var(--text-secondary); }
.sw { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }
table { border-collapse: collapse; width: 100%; font-size: 13px; margin-top: 14px; }
th, td { text-align: right; padding: 5px 8px; border-bottom: 1px solid var(--line); }
th:first-child, td:first-child { text-align: left; }
th { color: var(--text-muted); font-weight: 600; }
h3 { font-size: 16px; margin: 34px 0 6px; }
`];
constructor() { super(); this.conc = 16; this.table = false; this.tip = null; this.on = false; }
visibleChanged(v) { if (v) setTimeout(() => (this.on = true), 80); }
bar(v, val, max, base, label, tip, lowerBetter = false) {
const w = this.on ? Math.max(0.6, val / max * 64) : 0;
const r = val / base;
const ratio = v.key === "ruby" ? "" : lowerBetter ? ` · ${r <= 1 ? (1 / r).toFixed(1) + "× lower" : r.toFixed(1) + "× higher"}` : ` · ${r.toFixed(2)}×`;
return html`<div class="b"><div class="n">${v.name}</div><div class="track">
<div class="fill" style="width:${w}%;background:${v.color}"></div>
<div class="val" style="left:calc(${w}% + 6px)">${label}<i>${ratio}</i></div>
<div class="hit" @mousemove=${(e) => (this.tip = { x: e.clientX, y: e.clientY, t: tip })} @mouseleave=${() => (this.tip = null)}></div>
</div></div>`;
}
group(title, note, get, unit, lowerBetter = false, tipOf = () => "") {
const vals = VARIANTS.map((v) => get(v.key)), max = Math.max(...vals);
return html`<div><h4>${title}<span>${note}</span></h4>${VARIANTS.map((v, i) =>
this.bar(v, vals[i], max, vals[0], `${fmt(vals[i])} ${unit}`, `${v.name} · ${title}\n${fmt(vals[i])} ${unit}${tipOf(v.key)}`, lowerBetter))}</div>`;
}
render() {
const h = HEAD.http, c = this.conc;
const legend = html`<div class="legend">${VARIANTS.map((v) => html`<span><i class="sw" style="background:${v.color}"></i>${v.name}</span>`)}</div>`;
return html`<div class="card">
<div class="row" style="justify-content:space-between">
<div class="row">${[1, 16, 64].map((k) => html`<button class="seg" aria-pressed=${c === k} @click=${() => (this.conc = k)}>${k} client${k > 1 ? "s" : ""}</button>`)}</div>
<div class="row">${legend}<button class="seg" aria-pressed=${this.table} @click=${() => (this.table = !this.table)}>Table</button></div>
</div>
${this.table ? html`<table><tr><th>Route, ${c} clients</th>${VARIANTS.map((v) => html`<th>${v.name} req/s</th><th>p99 ms</th>`)}</tr>
${ROUTES.map(([r, label]) => html`<tr><td>${label}</td>${VARIANTS.map((v) => html`<td>${fmt(h[r][c][v.key].rps)}</td><td>${fmt(h[r][c][v.key].p99)}</td>`)}</tr>`)}
${HEAD.cable.map((row) => html`<tr><td>Cable, ${row.clients} clients (msg/s, p99)</td>${VARIANTS.map((v) => html`<td>${fmt(row[v.key].tput)}</td><td>${fmt(row[v.key].p99)}</td>`)}</tr>`)}
<tr><td>Peak memory (MB)</td>${VARIANTS.map((v) => html`<td>${fmt(HEAD.proc[v.key].peak)}</td><td></td>`)}</tr></table>`
: html`<div class="multi">${ROUTES.map(([r, label]) => this.group(label, `${c} clients`, (k) => h[r][c][k].rps, "req/s", false,
(k) => ` · p50 ${fmt(h[r][c][k].p50)} ms · p99 ${fmt(h[r][c][k].p99)} ms`))}</div>
<h3>Action Cable and memory</h3>
<div class="muted">Messages delivered per second to N subscribers in one room (p99 is in the tooltip and the table), and the container's peak memory across the run, where shorter is better.</div>
<div class="multi">
${HEAD.cable.map((row) => this.group(`Cable, ${row.clients.toLocaleString()} clients`, "msg/s", (k) => row[k].tput, "msg/s", false, (k) => ` · p50 ${fmt(row[k].p50)} ms · p99 ${fmt(row[k].p99)} ms`))}
${this.group("Peak container memory", "MB", (k) => HEAD.proc[k].peak, "MB", true)}
</div>`}
${this.tip ? html`<div class="tip" style="left:${this.tip.x + 14}px;top:${this.tip.y + 14}px">${this.tip.t}</div>` : nothing}
</div>`;
}
}
customElements.define("cf-head", Head);
/* ---------------------------------------------------------------- decisions */
const DECISIONS = [
["Ractors in one process, not forked workers",
"Parallel Ruby without separate processes: shared-nothing heaps, message passing, one deploy unit, and in-process queues like the Go and Rust ports.",
"Every Ractor keeps its own fragment cache and heap (1.2 GB peak). All constants must be deep-frozen. The Ractor API is still marked experimental."],
["Falcon + fibers, but SQL holds the GVL",
"Fibers overlap socket I/O within a Ractor. Queries take microseconds, so releasing the GVL for each one (~150 ns) would cost more than it saves.",
"A GET handler must never yield in the middle of a request, because it shares the Ractor's single read snapshot. Jobs.call ends the snapshot before it waits."],
["Lock waits in Ruby, not in SQLite's busy handler",
"SQLite's handler sleeps inside C while holding the Ractor's GVL, so that Ractor never reaches a GC safepoint. A writer in another Ractor stopped for GC still held the lock, and both sat out the whole 5 s timeout. Kernel#sleep yields instead: 2 → ~1,200 concurrent posts/s.",
"The port has its own retry loop (busy_retry), which other processes touching the same file still rely on."],
["Hand out the permit, not the SQL",
"A transaction mixes SQL with Ruby: last_insert_rowid, reads, branches. Shipping it to a writer Ractor would cost a round trip per statement. Passing the permit costs two messages per transaction.",
"Writes are serialized across the whole process, which is what SQLite does anyway, but now in arrival order."],
["Triggers for cascading callbacks",
"Touches, unread marking and dependent deletes run inside the write that causes them, with no extra round trips from Ruby. Triggers are recreated at every boot, so databases Rails created get them too.",
"Triggers never read SQLite's millisecond clock; touches on delete stay in Ruby. Code that writes messages or boosts must not touch the room itself."],
["Sec-Fetch-Site instead of CSRF tokens",
"As in the Rust port (and Rails main's header_only forgery protection): pages carry no per-request token, so a page renders the same until it changes. Every part can be compressed once, ETags repeat (304s), and fragment-free pages like the sidebar keep their gzip by body digest.",
"A deliberate divergence from Rails, like the /rooms/directs/:id redirect: parity checks drop the Rails token elements, and file_uploader.js is overridden so it doesn't send X-CSRF-Token. HTTPS writes need a browser that sends the header."],
["Extralite, patched",
"Faster than the sqlite3 gem, and it caches prepared statements by SQL text, which suits frozen SQL literals.",
"Two crash fixes are vendored (FTS5 statements on close; a statement freed by a failed execute stayed in the cache), to send upstream later."],
["Logging off the hot path",
"Log lines append to a Ractor-local buffer (about 100 ns, no allocations). A timer moves the whole buffer to the log Ractor with send(move: true).",
"Never use puts or warn: fd 2 belongs to the log Ractor."],
];
class Decisions extends LitElement {
static styles = [base, css`
.g { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 12px; }
.d { background: var(--surface-1); border: 1px solid var(--line); border-radius: 14px; padding: 16px 18px; }
h4 { margin: 0 0 8px; font-size: 15px; }
p { margin: 0 0 8px; font-size: 13.5px; color: var(--text-secondary); line-height: 1.5; }
.k { font: 700 10.5px var(--sans); letter-spacing: .08em; text-transform: uppercase; display: block; margin-bottom: 2px; }
.why .k { color: var(--good); } .cost .k { color: var(--bad); }
`];
render() {
return html`<div class="g">${DECISIONS.map(([t, why, cost]) => html`<div class="d"><h4>${t}</h4>
<p class="why"><span class="k">Why</span>${why}</p><p class="cost"><span class="k">Cost</span>${cost}</p></div>`)}</div>`;
}
}
customElements.define("cf-decisions", Decisions);
/* ---------------------------------------------------------------- gaps */
class Gaps extends LitElement {
static styles = [base, css`
ul { margin: 0; padding-left: 18px; color: var(--text-secondary); } li { margin: 8px 0; }
`];
render() {
const p = DATA.proc, h = DATA.http, cb = DATA.cable;
return html`<div class="card"><ul>
<li><strong>Rust is still ${(h.room_show[16].rust.rps / h.room_show[16].ruby.rps).toFixed(1)}× faster on the room page</strong>, and ${(h.messages_page[16].rust.rps / h.messages_page[16].ruby.rps).toFixed(1)}–${(h.search[16].rust.rps / h.search[16].ruby.rps).toFixed(1)}× on the other pages. What's left is mostly Ruby's own CPU time for rendering and request handling. Ruby head closes some of it: ${(h.room_show[16].rust.rps / HEAD.http.room_show[16]["ruby-head"].rps).toFixed(1)}× on the room page.</li>
<li><strong>Uploads: ${fmt(p.ruby.upload)} ms vs Rails ${fmt(p.rails.upload)} ms.</strong> This is the one place it loses to Rails, and it hasn't been profiled yet.</li>
<li><strong>Cable p99 at ${cb[1].clients}+ clients</strong> ranges from tens to hundreds of milliseconds between runs (median ${fmt(cb[1].ruby.p99)} ms vs Rust ${fmt(cb[1].rust.p99)} ms). That looks like stalls (GC marking about 1,000 fiber stacks, socket writes), not lack of throughput.</li>
<li><strong>Memory: ${fmt(p.ruby.peak)} MB peak</strong> on 4.0.7, versus Rust's ${fmt(p.rust.peak)} MB: per-Ractor heaps and caches. Ruby head brings it down to ${fmt(HEAD.proc["ruby-head"].peak)} MB.</li>
<li><strong>Not production-hardened:</strong> no TLS front server, no backup hooks, and jobs are lost on a crash.</li>
</ul></div>`;
}
}
customElements.define("cf-gaps", Gaps);
</script>
</body>
</html>