Skip to content

Instantly share code, notes, and snippets.

View andrewrk's full-sized avatar

Andrew Kelley andrewrk

View GitHub Profile
@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
328371: 48 81 ec 60 5f 00 00 sub rsp,0x5f60
@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
@andrewrk
andrewrk / InternArena.zig
Created January 31, 2022 02:04
use case for ArrayHashMap(void, void)
map: std.AutoHashMapUnmanaged(void, void),
items: std.MultiArrayList(Item),
extra: std.ArrayListUnmanaged(u32),
strings: std.ArrayListUnmanaged(u8),
const InternArena = @This();
const std = @import("std");
const Allocator = std.mem.Allocator;
const KeyAdapter = struct {
@andrewrk
andrewrk / example.zig
Created January 20, 2022 22:05
showing one example of improved LLVM IR for zig self hosted compiler
export fn entry() bool {
doTheTest() catch return false;
return true;
}
fn doTheTest() !void {
var x1: u8 = 42;
const t1 = .{ x1, 56, 54 };
var arr1: [3]u8 = t1;
_ = arr1[0];
@andrewrk
andrewrk / 0 log.txt
Last active December 15, 2021 21:09
Haiku dev talking about RAII in C++ in relation to Zig
[14:41:12] <waddlesplash> Zig seems cool but you don't seem to be about the RAII that we love so much :-p
[14:47:25] <andrewrk> anyway I won't come in here trying to convince anyone to switch languages or coding styles :)
[14:47:44] <waddlesplash> in Haiku ... C++ virtual inheritance and RAII objects mean that the code can much more clearly communicate what it is trying to do at the same time it is actually doing it
[14:48:02] <andrewrk> I do think that there is a misunderstanding that we don't care about RAII principles in zig though. I mean yeah you have to manually `defer cleanup();` but the coding model is the same
[14:48:15] <waddlesplash> but what happens if you decide you don't want to clean something up?
[14:48:21] <waddlesplash> for instance we have a very common paradigm in Haiku:
[14:49:35] <waddlesplash> thing* whatever = allocate_memory_or_something();
[14:49:35] <waddlesplash> MemoryDeleter<thing> whateverDeleter(whatever);
[14:49:36] <waddlesplash> if (error) return error; /* whatever is delete
@andrewrk
andrewrk / mystery.md
Last active October 6, 2021 19:52
trying to figure out what's going wrong with this linking

I have an object file created by LLVM:

$ readelf -s test.o

Symbol table '.symtab' contains 8 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS test
     2: 0000000000000010    22 FUNC    LOCAL  DEFAULT    2 start.callMain2
@andrewrk
andrewrk / 0test.zig
Created August 4, 2021 05:57
stage2 generics demo
const std = @import("std");
const expect = std.testing.expect;
test "example" {
var x: usize = 0;
x += checkSize(i32);
x += checkSize(bool);
try expect(x == 5);
}