Skip to content

Instantly share code, notes, and snippets.

Avatar

David Smith Catfish-Man

View GitHub Profile
View samplecache.m
static id cachedObject;
static NSLock *lock;
id getCachedObject() {
[lock lock];
id result = [cachedObject retain];
[lock unlock];
return result;
}
View feb2017donations
We're a group of tech employees (@Catfish_Man, @jnadeau, @numist, @jauricchio, and @daagaak) interested in making sure
as many people as possible survive the current state of US politics. For each dollar we donate, our employer
will donate two. We're pooling our resources to do the same for you, so for each dollar you donate to the
orgs below, we'll also donate one dollar to those orgs, and our employer will donate two: quadruple your donation!
We have $45k set aside for this, so we'll keep matching donations until we've matched that much or 72 hours have passed.
You can send me a tweet/DM (@Catfish_Man) with a screenshot of evidence that you donated (receipt page, whatever works, we're not picky).
Southern Poverty Law Center
Council on American-Islamic Relations
View oddstrings2.m
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
const long count = 10000000;
const char *str1 = "hellohello"; //fits in a tagged pointer
const char *str2 = "hellohelQo"; //at this character count, can only fit in a tagged pointer by using <8 bits per character, which requires dropping less frequently used chars like 'Q'
const char *str3 = "Northern Sami"; //this string is encountered frequently enough that it's baked into a perfect hash table in CoreFoundation
const char *str4 = "hellohellü"; //this string can't be tagged *and* can't be stored using an ASCII backing store
NSDate *start = [NSDate date];
View rootclasskvo.m
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
NS_ROOT_CLASS
@interface KVOTest
@end
@implementation KVOTest
View Chicken thighs and brown butter pan sauce tortellini
As seen here: https://twitter.com/Catfish_Man/status/721914376664408064
Loosely based on http://inhousecook.blogspot.com/2013/09/paprika-chicken-and-ravioli-with-brown.html
Preheat oven to 375F
Chop off a hunk of onion - we had half of one left, so I took about 3/4" off the center, plus a bit more when that looked skimpy
In an oven-safe pan (I used a big cast iron one) saute the onion over medium-low heat in a few tablespoons of unsalted butter until it's brown and translucent
View sillykeys.m
#import <Foundation/Foundation.h>
static const char *alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
NSString *badKey(int idx) {
char *buffer = malloc(4096);
memset(buffer, 'A', 4096 * sizeof(char));
//NSString hashes the beginning, middle, and end of long strings, so we want our keys to differ only in other places.
//-isEqual: also compares from front to back, so to exaggerate the problem, I placed the differences in the keys about 3/4 of the way through the string
buffer[3072] = alphabet[arc4random_uniform(52)];
@Catfish-Man
Catfish-Man / sethack.m
Created Mar 11, 2016
Demonstrating the trick of using stack-allocated mimics and sets for lookup tables instead of heap allocated keys and dictionaries
View sethack.m
// 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;
View tollfree.m
//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
View stringbench.swift
import Foundation
func measure(work:Void->Void) {
let start = NSDate()
for _ in 0..<100000 {
work()
}
let elapsed = -(start.timeIntervalSinceNow)
print(elapsed)
}
@Catfish-Man
Catfish-Man / Controller.m
Created Dec 30, 2015
An old boggle project's trie
View Controller.m
#import "Controller.h"
#define ENABLE_INLINE
#ifdef ENABLE_INLINE
#define INLINE static inline
#else
#define INLINE
#endif
static inline int INDEX(char letter) {