Skip to content

Instantly share code, notes, and snippets.

View amomchilov's full-sized avatar

Alexander Momchilov amomchilov

View GitHub Profile
@amomchilov
amomchilov / case_vs_Hash_benchmark.rb
Last active April 23, 2024 14:37
`case` statement vs `Hash` lookup
#!/usr/bin/ruby
# ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23]
# Comparison:
# Memo+Freeze+Ident H : 15333385.5 i/s
# Memo+Freeze Hash : 11604676.7 i/s - 1.32x slower
# case : 11026464.0 i/s - 1.39x slower
# Memo'ed Hash : 4516328.0 i/s - 3.40x slower
# Hash full of Ints : 4387124.4 i/s - 3.50x slower
@amomchilov
amomchilov / proc_pidlistfds.swift
Created April 17, 2024 19:42
List open file descriptors for a process using LibProc's `proc_pidinfo` and `PROC_PIDLISTFDS`.
import Darwin
struct ProcFileDescriptorInfo {
let fd: Int32
let path: String
}
func getFileDescriptors(pid: pid_t) -> [ProcFileDescriptorInfo] {
let bufferSize = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, nil, 0)
@amomchilov
amomchilov / benchmark.rb
Created December 13, 2023 15:07
Object#equal? is faster than comparing object_ids
#!/usr/bin/ruby
# Warming up --------------------------------------
# a.equal?(b) 670.728k i/100ms
# a.id == b.id 367.019k i/100ms
# Calculating -------------------------------------
# a.equal?(b) 6.588M (± 2.5%) i/s - 33.536M in 5.093834s
# a.id == b.id 3.646M (± 3.0%) i/s - 18.351M in 5.038107s
#
# Comparison:
@amomchilov
amomchilov / visible_apps.py
Last active January 19, 2023 12:57 — forked from pudquick/visible_apps.py
Getting the list of visible apps (think: Force Quit) in macOS via Python and PyObjC
#!/usr/bin/env python3
#ref: https://gist.github.com/pudquick/eebc4d569100c8e3039bf3eae56bee4c
from Foundation import NSBundle
import objc # pip3 install objc
CoreServices = NSBundle.bundleWithIdentifier_('com.apple.CoreServices')
functions = [
('_LSCopyRunningApplicationArray', b'@I'),
('_LSCopyApplicationInformation', b'@I@@'),
import Foundation
func getDriveBSDNames() -> [String] {
var iterator: io_iterator_t = 0
let matching: CFDictionary = IOServiceMatching(kIOServicePlane)
// Use 'kIOMasterPortDefault' for macOS older than 12.0 Monterey
IOServiceGetMatchingServices(kIOMainPortDefault, matching, &iterator)
def letterPattern(size):
# TODO: your code goes here
result = ""
for i in range(size):
p = 65
for j in range(i, size):
result += chr(p)
p += 1
import Foundation
struct S<T> {
let elements: [T]
}
extension S: Decodable where T: Decodable {
init(from decoder: Decoder) throws {
let container = try decoder.unkeyedContainer()
let s = sequence(state: container, next: {
import Dispatch
import Foundation
// The Weak struct is the weak wrapper
struct Weak<T: AnyObject> {
weak var object: T?
}
// Stand-in for AUGraphAddRenderNotify
func call(
@amomchilov
amomchilov / PrivacyViolator.rb
Last active March 4, 2020 22:09
Temporarily by-pass access modifiers on an object.
#!/usr/bin/ruby
def disrespect_privacy(symbol, &block) # access private methods in a block
raise ArgumentError, 'Block must be specified' unless block_given?
block_binding = block.binding
target_object = block_binding.local_variable_get(symbol)
block_binding.local_variable_set(symbol, PrivacyViolator.new(target_object))
yield