Skip to content

Instantly share code, notes, and snippets.

Catfish-Man / coroutines.txt
Created January 25, 2023 06:35
An overview of how and why Swift Concurrency uses coroutines
Ok, so.
A processor has things called "registers", which are essentially its local variables. Anything it wants to operate on, it loads from memory (RAM) into registers, does the operations, and then stores them back to memory. One of those registers is the "program counter", which holds the location of the next instruction to run. Now, there's a fixed quantity of registers, while you're allowed to have unlimited local variables, so there's also a dedicated region of memory for storing local variables that aren't currently in registers, which is called "the stack" (it's a stack because each time you call a function its variables get added to the top, and each time you return from a function they get taken off the top).
A thread is:
* The state of all the registers
* The stack
* A pointer for the operating system (the kernel scheduler specifically) to keep track of it
* A few odds and ends of metadata and extra state like priority and such
Catfish-Man / manystrings.swift
Created July 12, 2022 03:00
How many different kinds of NSString can we generate with the same code?
import Foundation
func examine(_ str: String) {
print("bridges as: \(type(of: str as NSString))")
str.withCString {
print("inits an NSString as: \(type(of: NSString(cString: $0, encoding: String.Encoding.ascii.rawValue)!))")
This code is completely untested but should be close enough to correct to demonstrate the concepts
struct RegularMonkey {
var bananaCount: Int
struct KlingonMonkey {
var bananaCount: Int
import Foundation
func test() {
let object = NSObject()
let cocoa = NSMutableArray()
var swift = Array<AnyObject>()
var start = Date()
for _ in 1 ... 200_000 {
cocoa.insert(object, at: 0)
#import <Foundation/Foundation.h>
#import <time.h>
#define USE_CF_NONOBJC 1
extern CFTypeRef _CFNonObjCRetain(CFTypeRef cf);
extern void _CFNonObjCRelease(CFTypeRef cf);
#import <stdlib.h>
#import <malloc/malloc.h>
#define SLOW 1
int main(int argc, const char * argv[]) {
#if SLOW
int sizes[] = { 256032, 512032, 1024032, 1792032 };
for (int ii = 0; ii < 10000; ii++) {
Catfish-Man / benchmarkcompare.swift
Created April 6, 2019 07:31
Shell and Swift versions of a little script to compare Swift benchmark output files
Shell version:
paste -d , oldoutput.txt output.txt | awk -F "," '{printf "%s %s %s %f\n", $2, $8, $16, $8 / $16}' | sort -k4 | egrep -v "(1\.0|0\.9[1-9])"
import Foundation
// "paste -d , oldfile.txt newfile.txt"
let paths = CommandLine.arguments[1...2].map(URL.init(fileURLWithPath:))
func g(_ x: inout Int) {
x = x * x
func f() {
var computed:Int {
get { return 5 }
set { print(newValue) }
Catfish-Man / failbuzz.m
Last active May 31, 2018 07:25
A horrendous implementation of fizzbuzz that didn’t pan out
//compile with "clang -framework Foundation -arch i386 failbuzz.m -o FizzBuzz"
//Yes, it only works in 32 bit
//No, I can't fix it to work correctly past 12 without coming up with a new implementation strategy,
//or compiling a new CoreFoundation that caches more CFNumbers (…I did do that to be sure it worked though)
#import <Foundation/Foundation.h>
#import <libgen.h>
static void setup(const char * argv[]) {
for (int i = 1; i <= 100; i++) {
#import <Foundation/Foundation.h>
#import <assert.h>
//Compile with `clang -Os -framework Foundation -fno-objc-arc inlinestorage.m -o inline, run with `inline clever` or `inline naive`
NaiveArray implements a simple immutable NSArray-like interface in probably the most obvious way: store a pointer to a C array of objects
@interface NaiveArray : NSObject {
NSUInteger count;