Skip to content

Instantly share code, notes, and snippets.

@NikolaiRuhe
NikolaiRuhe / JSONValue.swift
Created October 15, 2018 20:44
JSON DOM Swift implementation
import Foundation
/// A JSONValue is a generic DOM representation of decoded JSON.
enum JSONValue {
case null
case boolean(Bool)
case number(Double)
case string(String)
case array(JSONArray)
@tclementdev
tclementdev / libdispatch-efficiency-tips.md
Last active November 15, 2024 01:56
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

As of iOS 11/macOS High Sierra, and only including ones in Foundation and CoreFoundation
Strings:
_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 (https://github.com/opensource-apple/CF/blob/master/CFStorage.h) 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.
# When I want to attach lldb to swiftc to see how it works when compiling simple Swift programs:
utils/build-script \
--release --debug-swift \
--build-swift-dynamic-stdlib=0 --build-swift-dynamic-sdk-overlay=0 \
--skip-build-osx --skip-build-ios --skip-build-tvos --skip-build-watchos
# When I want to run the Swift test suite, to make sure my pull request would pass CI:
utils/build-script \
--release --debug-swift \
--test