Skip to content

Instantly share code, notes, and snippets.

View andrewrk's full-sized avatar

Andrew Kelley andrewrk

View GitHub Profile
@andrewrk
andrewrk / bench.zig
Last active January 5, 2023 03:24
benchmarking different versions of finding \r\n\r\n in a stream
const std = @import("std");
const KiB = 1024;
const MiB = 1024 * KiB;
pub fn main() !void {
var buffer: [(10000 / example.len) * example.len]u8 = undefined;
const total_bytes = 500000;
{
var i: usize = 0;
@andrewrk
andrewrk / foo.c
Created October 25, 2022 20:20
C backend output vs Zig code
static zig_E_Zir_Inst_Ref Sema_zirTypeName(zig_S_Sema * const a0, zig_S_Sema_Block * const a1, zig_u32 const a2) {
/* file:2:5 */
zig_S_Sema * t0;
t0 = a0;
zig_S_Sema * const * const t1 = (zig_S_Sema * const * )&t0;
zig_S_Sema * const t2 = (*t1);
zig_S_Zir * const t3 = (zig_S_Zir * )&t2->code;
zig_S_multi_array_list_MultiArrayList_28Zir_Inst_29_Slice * const t4 = (zig_S_multi_array_list_MultiArrayList_28Zir_Inst_29_Slice * )&t3->instructions;
zig_S_multi_array_list_MultiArrayList_28Zir_Inst_29_Slice const t5 = (*t4);
/* file:2:51 */
@andrewrk
andrewrk / README.md
Created September 1, 2022 02:31
rebase failing pull requests - useful when your contributors unfortunately open PRs against a broken master branch

Rebase Failing Pull Requests

Dependencies

  • gh with a logged in user
  • git clone zig repo in zig
  • jq, date, and git in PATH.

Instructions

@andrewrk
andrewrk / 0trace.zig
Last active June 7, 2022 22:04
a new std.debug.Trace API
pub fn Trace(comptime size: usize, comptime stack_frame_count: usize) type {
return struct {
addrs: [size][stack_frame_count]usize = undefined,
notes: [size][]const u8 = undefined,
index: usize = 0,
const frames_init = [1]usize{0} ** stack_frame_count;
pub noinline fn add(t: *@This(), note: []const u8) void {
return addAddr(t, @returnAddress(), note);
package main
import "fmt"
type Update struct{}
type Handler func(*Update)
type Dispatcher struct {
handlers []Handler
}
@andrewrk
andrewrk / global_allocator_example.zig
Created June 3, 2022 00:09
a pattern using comptime fields that might be nice for applications that want to use a global allocator
const std = @import("std");
var gpa_instance: std.heap.GeneralPurposeAllocator(.{}) = .{};
const global_allocator = gpa_instance.allocator();
const S = struct {
comptime gpa: std.mem.Allocator = global_allocator,
x: i32,
};
@andrewrk
andrewrk / stage2.txt
Last active June 1, 2022 08:26
buildOutputType compiled with -OReleaseFast, llvm backend, stage2 vs stage3 x86_64 disassembly
This file has been truncated, but you can view the full file.
0000000000328360 <buildOutputType>:
328360: 55 push rbp
328361: 48 89 e5 mov rbp,rsp
328364: 41 57 push r15
328366: 41 56 push r14
328368: 41 55 push r13
32836a: 41 54 push r12
32836c: 53 push rbx
32836d: 48 83 e4 e0 and rsp,0xffffffffffffffe0
@andrewrk
andrewrk / patch.diff
Created May 27, 2022 22:03
possible change to ZIR for functions
--- a/src/Zir.zig
+++ b/src/Zir.zig
/// Trailing:
/// 0. lib_name: u32, // null terminated string index, if has_lib_name is set
- /// 1. cc: Ref, // if has_cc is set
- /// 2. align: Ref, // if has_align is set
- /// 3. return_type: Index // for each ret_body_len
- /// 4. body: Index // for each body_len
- /// 5. src_locs: Func.SrcLocs // if body_len != 0
- pub const ExtendedFunc = struct {
@andrewrk
andrewrk / zig-perf.md
Last active July 10, 2022 09:14
some measurements of zig self-hosted compiler perf

Building a debug build of Zig:

zig build -Dskip-install-lib-files

Measurements taken on:

  • Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
  • 0.10.0-dev.2381+39ebfedd2

stage1

@andrewrk
andrewrk / 0results.txt
Created April 27, 2022 22:51
most common ZIR tags in the zig codebase
[nix-shell:~/Downloads/zig/build]$ ./zig fmt --ast-check ../src/ ../lib/std/
as_node: 571082
dbg_stmt: 325704
int: 266542
extended: 212003
param_type: 185337
int_big: 177048
call: 138602
field_call_bind: 105334
decl_val: 99774