Skip to content

Instantly share code, notes, and snippets.

@Catfish-Man
Catfish-Man / lockcachecontention.m
Last active July 5, 2017 07:00
Benchmark showing how locks sharing a cache line will contend with each other
#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++) {\
#!/usr/sbin/dtrace -q -s
/*
set(char *key, char *domain, char *user, char *host, char *container, char *value)
*/
CFPreferences$target:::set {
printf("Set request at %Y ( key: %s, domain: %s, user:%s, host: %s, container: %s, value: %s)\n", walltimestamp, copyinstr(arg0), copyinstr(arg1), copyinstr(arg2), copyinstr(arg3), copyinstr(arg4), copyinstr(arg5));
ustack();
printf("\n\n\n");
}
//Given this:
NSArray *objects = @[@1, @2, @3]
//These are equivalent in behavior
NSNumber *first = [objects objectAtIndex:0]
NSNumber *second = (NSNumber *)CFArrayGetValueAtIndex(objects, 0)
//But is the code that runs the same? Not so much… in the first one we do…
objc_msgSend(objects, <selector reference>, 0)
-> http://sealiesoftware.com/msg/x86-mavericks.html
@Catfish-Man
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++) {
func g(_ x: inout Int) {
x = x * x
}
func f() {
var computed:Int {
get { return 5 }
set { print(newValue) }
}
g(&computed)
@Catfish-Man
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:))
#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
Catfish-Man / sethack.m
Created March 11, 2016 06:21
Demonstrating the trick of using stack-allocated mimics and sets for lookup tables instead of heap allocated keys and dictionaries
// Compile with clang -framework Foundation sethack.m
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
/*
CFHashBytes from http://www.opensource.apple.com/source/CF/CF-1153.18/CFUtilities.c
*/
#define ELF_STEP(B) T1 = (H << 4) + B; T2 = T1 & 0xF0000000; if (T2) T1 ^= (T2 >> 24); T1 &= (~T2); H = T1;
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:
#import <Foundation/Foundation.h>
#import <time.h>
#define USE_CF_NONOBJC 1
#if USE_CF_NONOBJC
extern CFTypeRef _CFNonObjCRetain(CFTypeRef cf);
extern void _CFNonObjCRelease(CFTypeRef cf);
#endif