Skip to content

Instantly share code, notes, and snippets.

View mattsta's full-sized avatar
🐢
Moving slowly and fixing things

Matt Stancliff mattsta

🐢
Moving slowly and fixing things
View GitHub Profile
@mattsta
mattsta / gist:3a95f38977ee025ce82f5d6f84585682
Created March 21, 2025 18:21
git aliases to remove end of line whitespace before committing
# git cleancommit <files>
# removes trailing end-of-line whitespace in staged or committed files
cleancommit = "!bash -c 'commit_args=\"\"; files_to_clean=\"\"; for i in \"$@\"; do if [[ \"$i\" == -* ]]; then commit_args=\"$commit_args $i\"; else if [[ \"$i\" = /* ]]; then files_to_clean=\"$files_to_clean $i\"; else files_to_clean=\"$files_to_clean ${GIT_PREFIX}$i\"; fi; fi; done; if [ -n \"$files_to_clean\" ]; then git diff --name-only -- $files_to_clean | while IFS= read -r file; do if [ -f \"$file\" ]; then temp_file=$(mktemp); git diff -U0 \"$file\" | grep \"^@@\" | awk \"{split(\\$3,a,\\\",\\\"); line=substr(a[1],2); count=(length(a)>1)?a[2]:1; for(i=0;i<count;i++) if(line+i>0) print line+i}\" > \"$temp_file\"; while read -r line_num; do if [ -n \"$line_num\" ] && [[ \"$line_num\" =~ ^[0-9]+$ ]]; then sed -i \"\" \"$line_num s/[[:space:]]*$//\" \"$file\"; fi; done < \"$temp_file\"; rm -f \"$temp_file\"; git add \"$file\"; fi; done; else git diff --cached --name-only --diff-filter=ACM | while IFS= read -r
@mattsta
mattsta / setup.sh
Last active June 5, 2023 06:22
ubuntu 20 / lambdalabs pyenv+poetry+cuda12 bring-up
#!/usr/bin/env bash
## PURPOSE: Fix problems with the currently outdated lambdalabs default image.
# Does:
# - Installs latest Python 3.10 using pyenv
# - Installs the current poetry environment (assuming this is running in a poetry project dir)
# - Installs latest pytorch CUDA 12 nightly into current poetry environment
# Script for "normal-maxing" the default lambdalabs image as of June 2023.
#!/usr/bin/env bash
# mac vm_stat output is unreadable by default (it reports values as multiple of 16384 byte pages),
# so convert the results to actual GB used automatically.
# this python one liner is a list comprehension because it has to hack around
# python not allowing single-line for loops directly.
vm_stat | python -c 'import sys; [print(f"{l:<35}{int(sz) * 16384 / 2**30:>15,.2f} GB") for idx, (l, sz) in enumerate(x.strip().strip(".").split(":") for x in sys.stdin) if idx > 0]'
@mattsta
mattsta / speedups.c
Created December 11, 2021 19:24
a more optimized version of speedups.c from python websockets project with more efficient cascading XOR calculation for large data using Intel SIMD from 32 byte blocks then 16 byte blocks then 8 byte blocks then 1 byte blocks.
/* C implementation of performance sensitive functions. */
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <assert.h>
#include <stdint.h> /* uint32_t, uint64_t */
#include <stdio.h>
#if __AVX2__
@mattsta
mattsta / hsetup.sh
Created December 10, 2020 19:27
quick install steps for haproxy-2.3.2
#!/usr/bin/env bash
set -e
set -x
sudo apt install libssl-dev libsystemd-dev libpcre2-dev -y
wget http://www.haproxy.org/download/2.3/src/haproxy-2.3.2.tar.gz
tar xfvzp hap*
@mattsta
mattsta / hundreds.erl
Last active August 29, 2015 14:20
This is an erlang version of https://gist.github.com/gigamonkey/6249d85021bc8bf54eb4 (with a minor change to 'combos'/'combineAdjacent' for easier reading)
#!/usr/bin/env escript
% Problem statement:
% Write a program that outputs all possibilities to put + or - or nothing between the
% numbers 1, 2, ..., 9 (in this order) such that the result is always 100.
% For example: 1 + 2 + 34 – 5 + 67 – 8 + 9 = 100.
% Generate all combos of digits 1-9 with +, -, or nothing in between.
combos([N]) -> [[N]];
combos([N|Ns]) -> [[N, X] ++ Rest || X <- [plus, minus, empty], Rest <- combos(Ns)].
@mattsta
mattsta / reformatted.hs
Created February 23, 2015 17:50
commandstats reformatted
{'ver': {'status': 'UNKNOWN', 'version': ''}}
{'cmds': {'sorted by calls': [{'get': '158,696,585 times'},
{'set': '71,614,064 times'},
{'ping': '23,045,904 times'},
{'exists': '1,425,952 times'},
{'info': '1,403,483 times'},
{'replconf': '1,370,802 times'},
{'mget': '700,439 times'},
{'del': '163,092 times'},
{'setex': '139 times'},
@mattsta
mattsta / mem_size_bench.c
Created December 20, 2014 19:54
benchmark memory operations from small 32 bytes to 4+ MB. results reported as factional nanoseconds.
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <string.h>
static long long ustime(void) {
struct timeval tv;
long long ust;
gettimeofday(&tv, NULL);
@mattsta
mattsta / gist:8810a0cdf18f45875ebf
Created December 20, 2014 16:06
dont sue me sue
* THIS CONVERSATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS CONVERSATION, EVEN IF ADVISED OF THE
@mattsta
mattsta / thing.hs
Created December 3, 2014 21:51
Script/Non-Script Speeds
127.0.0.1:6379> script load "return redis.call('georadius', '{global}:z:locations:'..ARGV[1], ARGV[2], ARGV[3], 10, 'km', 'withdistance', 'ascending')"
"1c4c03da2a57cf99d50d57aba9a1d431526b2b9e"
127.0.0.1:6379> script load "return redis.call('get', 'abc')"
"b86fdf4c29439ed5d9a52c206d90e2b2d8cfc42c"
Geo tests (key doesn't exist, so just testing getting an empty reply):
matt@ununoctium:~/repos/redis/src% redis-benchmark -n 10000 georadius {global}:z:locations:u1 51.0500000 3.7166700 10 km withdistance ascending
====== georadius {global}:z:locations:u1 51.0500000 3.7166700 10 km withdistance ascending ======
10000 requests completed in 0.08 seconds