Skip to content

Instantly share code, notes, and snippets.

@gimbling-away
gimbling-away / mew.rs
Created February 21, 2024 14:39
meow
fn new(obj: T) -> Result<Self, AllocError> {
unsafe {
match ARENA {
Some(arena) => match Arena::allocate::<T>(arena) {
ArenaResult::Okay(pointer) => Ok(Self { pointer, arena }),
ArenaResult::RecoverAndSpawn => {
match Arena::find_arena(arena, size_of::<T>()) {
Some(arena) => match Arena::allocate::<T>(arena) {
ArenaResult::Okay(pointer) => {
Arena::increment(arena);
#[repr(C)]
pub struct Arena<A: Allocator> {
count: u32,
free: bool,
size: usize,
end: NonNull<u8>,
pointer: NonNull<u8>,
next: Option<NonNull<Self>>,
allocator: A,
arena: NonNull<[u8]>,
use mimalloc::MiMalloc;
#[cfg(not(miri))]
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;
use k_gc::Rc;
#[allow(dead_code)]
pub struct Node {
#![feature(ptr_metadata, let_chains)]
use std::{
alloc::{alloc, dealloc, Layout},
ops::Deref,
ptr,
};
use libc::{mmap, munmap, MAP_ANON, MAP_PRIVATE, PROT_READ, PROT_WRITE};
use std::arch::asm;
use std::hint::unreachable_unchecked;
use std::clone::Clone;
use std::ops::Drop;
#[repr(C)]
struct Rc<T>(*mut T);
impl<T> Clone for Rc<T> {
#[inline(always)]
@gimbling-away
gimbling-away / test.rs
Created September 18, 2023 12:10
Bench
#[allow(unused_imports)] use std::hint::black_box;
#[inline(never)]
extern "C" fn fib_impl(x: u64, prev: u64, cur: u64) -> u64 {
match x {
0 => prev,
1 => cur,
_ => unsafe { be(fib_impl, x - 1, cur, prev + cur) }
}
}
@gimbling-away
gimbling-away / Program.fs
Created September 1, 2023 18:06
Stack-efficient DUs in F#
open System
open System.Runtime.InteropServices
module Program =
[<Struct>]
[<RequireQualifiedAccess>]
type NormalDU =
| ManagedObject of array: int[]
| String of str: string
| Int of int: int
#![feature(cell_update)]
use crossbeam::channel::{unbounded, Receiver, Sender};
use once_cell::sync::Lazy;
use std::cell::Cell;
use std::mem::MaybeUninit;
use std::ops::Deref;
use std::ptr::{NonNull, null_mut};
use std::sync::atomic::Ordering::{Acquire, Relaxed, Release};
use std::sync::atomic::{AtomicU16, AtomicUsize};
use mimalloc::MiMalloc;
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;
mod rcute {
use std::alloc::Layout;
use std::fmt::{Debug, Formatter};
use std::ops::Deref;
use std::ptr::NonNull;
let rec private lex_ (source: char ReadOnlySpan) (index: int) (collector: Token array) =
if index = source.Length then
collector
else
let character = source[index]
match character with
| ' ' -> lex_ source (index + 1) collector
| '+' -> lex_ source (index + 1) (Array.append collector [| RParen; RParen; Add; LParen; LParen |])
| '-' -> lex_ source (index + 1) (Array.append collector [| RParen; RParen; Sub; LParen; LParen |])