Skip to content

Instantly share code, notes, and snippets.

@tclementdev
tclementdev / libdispatch-efficiency-tips.md
Last active May 10, 2024 15:05
Making efficient use of the libdispatch (GCD)

libdispatch efficiency tips

The libdispatch is one of the most misused API due to the way it was presented to us when it was introduced and for many years after that, and due to the confusing documentation and API. This page is a compilation of important things to know if you're going to use this library. Many references are available at the end of this document pointing to comments from Apple's very own libdispatch maintainer (Pierre Habouzit).

My take-aways are:

  • You should create very few, long-lived, well-defined queues. These queues should be seen as execution contexts in your program (gui, background work, ...) that benefit from executing in parallel. An important thing to note is that if these queues are all active at once, you will get as many threads running. In most apps, you probably do not need to create more than 3 or 4 queues.

  • Go serial first, and as you find performance bottle necks, measure why, and if concurrency helps, apply with care, always validating under system pressure. Reuse

@ErikAugust
ErikAugust / spectre.c
Last active April 15, 2024 13:55
Spectre example code
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#ifdef _MSC_VER
#include <intrin.h> /* for rdtscp and clflush */
#pragma optimize("gt",on)
#else
#include <x86intrin.h> /* for rdtscp and clflush */
#endif
@jpsim
jpsim / swift_dev_linux.md
Created December 14, 2017 18:18
Build & run a subset of Swift tests on Linux

Build & run a subset of Swift tests on Linux

# Spin up a beefy Digital Ocean droplet
doctl compute droplet create swift-dev --size m-224gb --image ubuntu-16-10-x64 --region sfo2 --ssh-keys XXXXXXX
doctl compute ssh swift-dev
# Install dependencies
apt-get update
apt-get install git cmake ninja-build clang python uuid-dev libicu-dev icu-devtools libbsd-dev libedit-dev libxml2-dev libsqlite3-dev swig libpython-dev libncurses5-dev pkg-config libblocksruntime-dev libcurl4-openssl-dev autoconf libtool systemtap-sdt-dev tzdata
# Clone &amp; build Swift
@michaelochs
michaelochs / DynamicNSStringEvaluation.m
Last active February 6, 2022 14:47
As a follow up for https://gist.github.com/michaelochs/f106b5c42aafe6bed74ac5dab82281c4 this is what's going on under the hood!
@interface MyStringProxy : NSProxy
@property (nonatomic) NSString *target;
@end
@implementation MyStringProxy
- (BOOL)respondsToSelector:(SEL)aSelector {
@michaelochs
michaelochs / NSFormattingContextDynamic.m
Created December 13, 2016 15:37
`NSFormattingContextDynamic` makes a formatter return string proxies that change based on where you but them inside a format string.
NSDate *date = [NSDate new];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"nl_NL"];
dateFormatter.dateStyle = NSDateFormatterFullStyle;
dateFormatter.formattingContext = NSFormattingContextDynamic; // this is the important setting
NSString *dateString = [dateFormatter stringFromDate:date];
NSString *s1 = [NSString stringWithFormat:@"Foo %@", dateString]; // "Foo dinsdag 13 december 2016"
// MARK: Equatable
func ==<T>(lhs: TerminatingNode<T>, rhs: TerminatingNode<T>) -> Bool {
return lhs.value == rhs.value
}
func ==<T, U>(lhs: ListNode<T, U>, rhs: ListNode<T, U>) -> Bool {
return lhs.value == rhs.value && lhs.next == rhs.next
}
@andymatuschak
andymatuschak / States-v3.md
Last active May 1, 2024 12:32
A composable pattern for pure state machines with effects (draft v3)

A composable pattern for pure state machines with effects

State machines are everywhere in interactive systems, but they're rarely defined clearly and explicitly. Given some big blob of code including implicit state machines, which transitions are possible and under what conditions? What effects take place on what transitions?

There are existing design patterns for state machines, but all the patterns I've seen complect side effects with the structure of the state machine itself. Instances of these patterns are difficult to test without mocking, and they end up with more dependencies. Worse, the classic patterns compose poorly: hierarchical state machines are typically not straightforward extensions. The functional programming world has solutions, but they don't transpose neatly enough to be broadly usable in mainstream languages.

Here I present a composable pattern for pure state machiness with effects,

@austinzheng
austinzheng / generics_manifesto.md
Last active October 23, 2023 20:38
Douglas Gregor's Swift Generics Manifesto, with added markdown formatting

(Source: https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160229/011666.html)

Introduction

The “Complete Generics” goal for Swift 3 has been fairly ill-defined thus fair, with just this short blurb in the list of goals:

Complete generics: Generics are used pervasively in a number of Swift libraries, especially the standard library. However, there are a number of generics features the standard library requires to fully realize its vision, including recursive protocol constraints, the ability to make a constrained extension conform to a new protocol (i.e., an array of Equatable elements is Equatable), and so on. Swift 3.0 should provide those generics features needed by the standard library, because they affect the standard library's ABI.

This message expands upon the notion of “completing generics”. It is not a plan for Swift 3, nor an official core team communication, but it collects the results of numerous discussions among the core team and Swift developers, both of the compiler an

@cb372
cb372 / jargon.md
Last active May 8, 2024 09:21
Category theory jargon cheat sheet

Category theory jargon cheat sheet

A primer/refresher on the category theory concepts that most commonly crop up in conversations about Scala or FP. (Because it's embarassing when I forget this stuff!)

I'll be assuming Scalaz imports in code samples, and some of the code may be pseudo-Scala.

Functor

A functor is something that supports map.

@JadenGeller
JadenGeller / Type-Level Assertions.swift
Last active March 9, 2018 03:06
Type-Level Assertions (or, almost-dependent types)
/*
* We've defined "traits" by which we can type an integer that are characteristic of its value.
* These traits can even be subtraits of other traits (like both positive and negative being nonzero).
*
* We can use these traits in the type signatures of functions to indicate what trait will be returned
* as a function of the passed-in traits.
*
* Even cooler, we can specify properties of the traits such that we can runtime-verify the correctness
* of these labels (in case a function was improperly annotated, for example).
*/