Skip to content

Instantly share code, notes, and snippets.

View lithdew's full-sized avatar

Kenta Iwasaki lithdew

View GitHub Profile
@lithdew
lithdew / sparse.zig
Created June 26, 2022 09:26
zig: generational paged sparse set
const std = @import("std");
const sparse = @This();
/// This is an implementation of a paged sparse set that stores the payload in
/// the sparse array.
//
/// A sparse set has a dense and a sparse array. The sparse array is directly
/// indexed by a 64 bit identifier. The sparse element is linked with a dense
/// element, which allows for liveliness checking. The liveliness check itself
@lithdew
lithdew / main.zig
Created November 1, 2021 14:21
zig: x25519 handshake
const std = @import("std");
pub fn main() !void {
const server_keys = try std.crypto.sign.Ed25519.KeyPair.create(null);
const client_keys = try std.crypto.sign.Ed25519.KeyPair.create(null);
std.log.info("server secret key: {s}", .{std.fmt.fmtSliceHexLower(&server_keys.secret_key)});
std.log.info("server public key: {s}", .{std.fmt.fmtSliceHexLower(&server_keys.public_key)});
std.log.info("client secret key: {s}", .{std.fmt.fmtSliceHexLower(&client_keys.secret_key)});
@lithdew
lithdew / io_uring.go
Last active July 24, 2021 18:31
epoll (go) vs. io_uring (zig)
// GOMAXPROCS=8 go run io_uring.go
package main
import (
"bufio"
"fmt"
"net"
"runtime"
"sync/atomic"
@lithdew
lithdew / benchmark.txt
Last active June 28, 2021 22:07
go: one shot event
$ go test -bench=. -run=NONE
goos: linux
goarch: amd64
cpu: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
BenchmarkOneShotEvent-8 3904360 306.8 ns/op 8 B/op 1 allocs/op
BenchmarkChannel-8 3139602 381.7 ns/op 96 B/op 1 allocs/op
BenchmarkWaitGroup-8 2958838 408.5 ns/op 16 B/op 1 allocs/op
@lithdew
lithdew / main.zig
Last active May 23, 2021 22:24
zig: cancellation token
const std = @import("std");
const os = std.os;
const builtin = std.builtin;
const testing = std.testing;
const assert = std.debug.assert;
/// A simple cancellation framework for blocking/non-blocking tasks.
pub const Cancellation = struct {
@lithdew
lithdew / x.md
Last active May 20, 2021 09:20
zig: `std.x` wishlist
  • Include std.x.os.Reactor.

A high-level I/O event reactor interface that wraps over event notification systems like epoll, kqueue.

Plan is to get epoll done first, kqueue second, select third, then iocp/afd fourth.

  • Include std.x.os.Socket.setLinger(linger_for_num_seconds: ?u16) !void.
  • Include std.x.os.Socket.getRemoteAddress() !std.x.Socket.Address.
  • Include std.x.os.Socket.setKeepAlive(enabled bool) !void.
  • Include std.x.os.Socket.(r|R)eader(flags) and std.x.os.Socket.(w|W)riter(flags).
@lithdew
lithdew / mpsc.zig
Created March 1, 2021 11:56
zig: mpsc queue
const std = @import("std");
const os = std.os;
const mem = std.mem;
const testing = std.testing;
pub fn Channel(comptime T: type) type {
return struct {
const Self = @This();
@lithdew
lithdew / disruptor.zig
Created February 21, 2021 10:23
zig: disruptor
const std = @import("std");
const mem = std.mem;
const math = std.math;
const VACANT = math.maxInt(usize);
pub fn RingBuffer(comptime T: type, comptime num_readers: comptime_int, comptime capacity: comptime_int) type {
return struct {
const Self = @This();
@lithdew
lithdew / benchmark.zig
Last active February 6, 2021 17:03
zig: open-addressing robinhood hashmap w/ backward shift deletion
// zig run benchmark.zig -lc -O ReleaseFast
const std = @import("std");
usingnamespace @import("hashmap.zig");
pub fn main() !void {
const allocator = std.heap.c_allocator;
var map = try HashMap(
@lithdew
lithdew / trie.zig
Last active January 27, 2021 08:49
zig: trie for routing
const std = @import("std");
const mem = std.mem;
const fmt = std.fmt;
const sort = std.sort;
const testing = std.testing;
pub fn Trie(comptime S: []const u8, comptime V: type) type {
return struct {
const Self = @This();