Skip to content

Instantly share code, notes, and snippets.

View matklad's full-sized avatar

Alex Kladov matklad

View GitHub Profile
const Self = @This();
const mem = std.mem;
const eof = error.EndOfStream;
const std = @import("../std.zig");
const Reader = struct {
context: *const anyopaque,
readFn: *const fn (context: *const anyopaque, buffer: []u8) anyerror!usize,
pub const Error = anyerror;
#!/bin/sh
echo `# <#`
mkdir -p ./zig
wget https://ziglang.org/download/0.10.1/zig-linux-x86_64-0.10.1.tar.xz -O ./zig/zig-linux-x86_64-0.10.1.tar.xz
tar -xf ./zig/zig-linux-x86_64-0.10.1.tar.xz -C ./zig --strip-components=1
rm ./zig/zig-linux-x86_64-0.10.1.tar.xz
echo "Zig installed."
pub trait Handler: Sized {
fn address(&mut self, state: &mut Machine, opcode: Opcode, position: usize) -> Control;
}
type OpFn<H> = fn(this: &mut H, state: &mut Machine, opcode: Opcode, position: usize) -> Control;
// We would like to write this, but Rust's const-eval is not good enough for that yet :(
// const fn mk_table<H: Handler>() -> [OpFn<H>; 256] { }
// So, we have to express this as type-level function (trait)
def print_all_sets():
n = 3
g = Gen()
while not g.done():
print([g.gen(1) == 1 for _ in range(3)])
class Gen:
def __init__(self):
self.v = []
pub struct WasmRefCell<T>(core::cell::RefCell<T>);
impl<T> WasmRefCell<T> {
pub const fn new(initial_value: T) -> WasmRefCell<T> {
WasmRefCell(core::cell::RefCell::new(initial_value))
}
pub fn with<U, F: FnOnce(&mut T) -> U>(&self, f: F) -> U {
f(&mut *self.0.borrow_mut())
}
pub struct TestResource {
path: &'static str,
cell: OnceCell<Vec<u8>>,
}
impl TestResource {
pub const fn new(path: &'static str) -> TestResource {
TestResource { path, cell: OnceCell::new() }
}
pub fn bytes(&self) -> &[u8] {
@matklad
matklad / a.nt
Last active October 4, 2020 11:29
TOML vs nestedtext.org
workspace:
members:
- crates/*
- xtask/
profile:
dev:
# Disabling debug info speeds up builds a bunch,
# and we don't rely on it for debugging that much.
debug: 0
@matklad
matklad / data.csv
Last active February 15, 2023 23:21
Measuring latencies of text editors in 2020
VS Code (compositor disabled) Sublime3 (compositor disabled) Kitty (compositor disabled) Emacs (compositor disabled) IntelliJ Idea(compositor disabled) VS Code (compositor enabled) Sublime3 (compositor enabled) Kitty (compositor enabled) Emacs (compositor enabled) IntelliJ Idea (compositor enabled)
33.11 6.51 28.80 3.57 5.58 43.03 23.69 32.88 26.51 24.32
11.41 6.86 16.23 3.72 5.44 18.95 21.41 34.74 25.58 27.36
23.91 7.46 12.08 3.23 6.52 29.17 22.13 42.68 21.77 25.10
15.02 7.21 15.29 3.76 7.14 37.95 23.81 32.55 26.18 25.69
14.32 6.93 14.38 3.71 5.40 30.00 27.72 23.69 22.69 27.63
14.09 7.53 28.89 3.84 6.03 31.61 24.66 39.54 25.33 26.98
14.72 7.51 14.65 3.65 6.25 48.57 18.52 21.66 20.52 26.54
13.87 6.77 28.89 3.17 5.50 47.22 23.33 40.54 24.05 25.85
14.36 6.61 14.36 3.71 5.61 42.68 23.04 32.98 25.21 26.66
12:10:54|~/tmp
λ bat -p main.cpp
#include <iostream>
int square(int x) {
return x * x;
}
int main(void) {
int x = 1;
@matklad
matklad / concurrent-interner.rs
Last active April 3, 2020 22:58
Concurrent Interner for Rust
use std::{
cell::RefCell,
hash::{BuildHasher, Hash, Hasher},
sync::Arc,
};
// hashbrown = "0.7"
use hashbrown::{hash_map::DefaultHashBuilder, HashMap};
// parking_lot = "0.10"
use parking_lot::{Mutex, RwLock};