Skip to content

Instantly share code, notes, and snippets.

View andrewrk's full-sized avatar

Andrew Kelley andrewrk

View GitHub Profile
@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);
}
@andrewrk
andrewrk / adapted_hash_maps.zig
Last active July 23, 2021 07:13
how to use hash map contexts to save memory when doing a string table
const std = @import("std");
const mem = std.mem;
const Foo = struct {
string_bytes: std.ArrayListUnmanaged(u8),
/// Key is string_bytes index.
string_table: std.HashMapUnmanaged(u32, void, IndexContext, std.hash_map.default_max_load_percentage),
};
const IndexContext = struct {
@andrewrk
andrewrk / output.txt
Created July 3, 2021 23:49
benchmarking the zig tokenizer
[nix-shell:~/Downloads/zig/build-release]$ ./zig build-exe test.zig -OReleaseFast
[nix-shell:~/Downloads/zig/build-release]$ ls -hl ../lib/std/special/compiler_rt/udivmod{t,d}i4_test.zig test.zig
-rw-r--r-- 1 andy users 1.9M May 10 21:29 ../lib/std/special/compiler_rt/udivmoddi4_test.zig
-rw-r--r-- 1 andy users 9.8M May 10 21:29 ../lib/std/special/compiler_rt/udivmodti4_test.zig
-rw-r--r-- 1 andy users 1.2K Jul 3 16:46 test.zig
[nix-shell:~/Downloads/zig/build-release]$ ./test test.zig
tokenizer: 25 MiB/s
@andrewrk
andrewrk / codegen.cpp
Created May 11, 2021 18:46
reset error return trace index
if (instruction->operand != nullptr &&
instruction->operand->value->type->id != ZigTypeIdErrorSet &&
instruction->operand->value->type->id != ZigTypeIdErrorUnion)
{
// Reset error return trace index.
bool is_llvm_alloca = false;
LLVMValueRef err_ret_trace_ptr = get_cur_err_ret_trace_val(g, instruction->base.base.scope, &is_llvm_alloca);
if (err_ret_trace_ptr != nullptr) {
size_t index_field_index = g->stack_trace_type->data.structure.fields[0]->gen_index;
LLVMValueRef index_field_ptr = LLVMBuildStructGEP(g->builder, err_ret_trace_ptr, (unsigned)index_field_index, "");
diff --git a/lib/std/array_hash_map.zig b/lib/std/array_hash_map.zig
index 83a061dfe..fc1f18771 100644
--- a/lib/std/array_hash_map.zig
+++ b/lib/std/array_hash_map.zig
@@ -571,21 +571,30 @@ pub fn ArrayHashMapUnmanaged(
}
pub fn getIndex(self: Self, key: K) ?usize {
+ const Matcher = struct {
+ const Key = K;