Skip to content

Instantly share code, notes, and snippets.

David Smith Catfish-Man

Block or report user

Report or block Catfish-Man

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View malloccliff.c
#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 Apr 6, 2019
Shell and Swift versions of a little script to compare Swift benchmark output files
View benchmarkcompare.swift
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:))
View localcomputedinout.swift
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
A horrendous implementation of fizzbuzz that didn’t pan out
View failbuzz.m
//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++) {
View inline.m
#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;
View Class Clusters
As of iOS 11/macOS High Sierra, and only including ones in Foundation and CoreFoundation
_NSCFString - a CFStringRef or CFMutableStringRef. This is the most common type of string object currently.
- May have 8 bit (ASCII) or 16 bit (UTF-16) backing store
_NSCFConstantString - a compile time constant CFStringRef, like you'd get with @"foo"
- May also be generated by dynamic string creation if matches a string in a pre-baked table of common strings called the StringROM
NSBigMutableString - an NSString backed by a CFStorage ( for faster handling of very large strings
NSCheapMutableString - a very limited NSMutableString that allows for zero-copy initialization. Used in NSFileManager for temporarily wrapping stack buffers.
Catfish-Man / lockcachecontention.m
Last active Jul 5, 2017
Benchmark showing how locks sharing a cache line will contend with each other
View lockcachecontention.m
#import <Foundation/Foundation.h>
#import <time.h>
#import <os/lock.h>
#define ITERS 2000
#define NSEC_PER_ITER(time) (((double)time * (double)NSEC_PER_SEC) / (double)ITERS)
#define TEST(body, name) do {\
start = [NSDate date];\
for (int i = 0; i < ITERS; i++) {\
View tweetstorm.swift
// Created by David Smith on 5/29/17.
// Copyright © 2017 Unseen University. All rights reserved.
// Localization- and encoding-safe solution to Coraline Ada's challenge here:
// Very lightly tested. Probably contains bugs.
import Foundation
func tweetStorm(input uncanonicalizedInput:String, handle:String?) -> [String] {
let input = uncanonicalizedInput.precomposedStringWithCanonicalMapping //twitter requires NFC
View Caches
Let's Reinvent Modern CPU Caches!
In The Beginning, programs were hard-coded, entered directly with switches. Values would be input, and then results would output,
but couldn't really be stored. We'll draw this like so:
Input -> Fixed Calculations -> Output
An early improvement in generality was the addition of storage (ENIAC eventually gained 100 words of magnetic core memory),
leaving us with something along these lines:
View samplecache.m
static id cachedObject;
static NSLock *lock;
id getCachedObject() {
[lock lock];
id result = [cachedObject retain];
[lock unlock];
return result;
You can’t perform that action at this time.