Skip to content

Instantly share code, notes, and snippets.

View ysbaddaden's full-sized avatar

Julien Portalier ysbaddaden

View GitHub Profile
@ysbaddaden
ysbaddaden / rwlock_ring.md
Created April 4, 2025 14:33
Protect io_uring SQ ring with rwlock

Use a rwlock to have MP (multiple producers) access to a Ring?

The use-case is multiple threads trying to submit operations to an io_uring SQ ring, using a rwlock, a local sq_tail to be synchronized with the tail shared with the kernel (sq_ktail).

Maybe we can submit to the SQ ring from multiple threads with reduced contentions using a rwlock instead of a mutex. The read lock would protect incrementing the local sq_tail (updated with a CAS) and preparing the SQE, while the write lock would protect updates to the sq_ktail shared with the kernel (updated with a

@ysbaddaden
ysbaddaden / README.md
Created March 2, 2025 16:33
Crystal gc/none as a crystal library

Prerequisite

Get the current master branch of Crystal. Compile a new crystal compiler:

$ make .build/crystal

Setup

require "crystal/spin_lock"
require "crystal/pointer_linked_list"
# Synchronize execution of concurrently running fibers.
#
# This can be used to replace polling with a waiting list that can be resumed
# when resources are available, while still behaving inside a mutually exclusive
# context: when a waiting fiber is resumed, the mutex will be relocked.
#
# Can also be used as a notification system without a mutex.
diff --git a/src/compiler/crystal/codegen/ast.cr b/src/compiler/crystal/codegen/ast.cr
index ab4fa76d4..ed3465be1 100644
--- a/src/compiler/crystal/codegen/ast.cr
+++ b/src/compiler/crystal/codegen/ast.cr
@@ -72,6 +72,10 @@ module Crystal
nil
end
+ def linkage
+ nil
@ysbaddaden
ysbaddaden / nsync.cr
Last active December 20, 2024 11:56
Crystal port of nsync mu (missing conditions) + Mutex & RWLock objects
# Crystal port of the mutex from the "nsync" library.
#
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
@ysbaddaden
ysbaddaden / RFC_TIMERS_BENCH_RESULTS.md
Last active November 22, 2024 18:29
RFC TIMERS: BENCH RESULTS

HEAP benchmarks

  • CPU: Intel i7 14700K
  • RAM: 5600 MT/s
  • Ubuntu 22.04 / Linux 6.9

Each test creates N items (not measured) and shuffles them (unless said otherwise) then measures how long the operation(s) take to complete. The test repeats itself 10 times. The average time per operation of all runs is eventually printed.

@ysbaddaden
ysbaddaden / debug_codegen_failures.patch
Created August 29, 2024 09:55
Crystal: debug codegen failures
diff --git a/src/compiler/crystal/compiler.cr b/src/compiler/crystal/compiler.cr
index 38880ee9e..966e2f954 100644
--- a/src/compiler/crystal/compiler.cr
+++ b/src/compiler/crystal/compiler.cr
@@ -523,13 +523,17 @@ module Crystal
private def fork_codegen(units, n_threads)
workers = fork_workers(n_threads) do |input, output|
while i = input.gets(chomp: true).presence
+ Crystal::System.print_error "CODEGEN pid=#{LibC.getpid} STDIN => #{i}\n"
unit = units[i.to_i]
@ysbaddaden
ysbaddaden / stw.cr
Last active June 19, 2024 11:17
stw.cr
class STW
def initialize
@running = true
@started = Atomic(Int32).new(0)
@count = Atomic(Int32).new(0)
@threads = [] of Thread
end
def start_thread
@threads << Thread.new do
@ysbaddaden
ysbaddaden / gen_search_json.cr
Created June 10, 2024 09:06
crystal docs: gen search.json from index.json
require "json"
def cleanup_constant(type : JSON::Any)
type.as_h.select! do |key, _|
{
"id",
"name",
"value",
"doc",
"summary",
require "c/sys/time"
lib LibC
KQUEUE_CLOEXEC = O_CLOEXEC
# todo: constants
# EV_ADD = ...
# EV_ENABLE = ...
# ...