Skip to content

Instantly share code, notes, and snippets.

View josiahcarlson's full-sized avatar

Josiah Carlson josiahcarlson

View GitHub Profile
@josiahcarlson
josiahcarlson / one_bit_difference.py
Created October 3, 2023 03:22
Find all 1-bit differences among provided integers.
"""
one_bit_difference.py
Written September / October 2023 by Josiah Carlson - josiah.carlson@gmail.com
Released into the public domain. Please include this authorship note / reference
to this GIST if you use / derive this software. Thank you.
Been thinking about this algorithm for about 17 years, haven't seen it around.
I might not know the proper literature search to find relevant references, or if
this was already done.
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 1 column, instead of 4 in line 1.
$ time docker-compose run redis-server numactl -N 1 -l /usr/src/redis/src/redis-benchmark --crc 512000000 --csv --combine
algorithm,buffer,performance,crc64_matches
crc_1byte,512000000,400,1
crcspeed,512000000,1354,1
crcdual,512000000,2129,1
crctri,512000000,2243,1
crc_1byte,312000000,401,1
crcspeed,312000000,1352,1
crcdual,312000000,2090,1
crctri,312000000,2213,1
@josiahcarlson
josiahcarlson / rom_example_bruno.py
Last active August 8, 2019 04:47
Example from a livecoding stream, updated with fixes
'''
This this was retyped from https://www.twitch.tv/videos/459202303 for notes and
then updated by the author of rom, me, Josiah Carlson
Notes:
* For created_at, or any default, if it is "callable", it will be called at
entity creation/instantiation. In the demo in the video, we see:
created_at = rom.DateTime(default=datetime.datetime.now())
@josiahcarlson
josiahcarlson / reply_to_blog.txt
Created February 19, 2016 04:29
Reply to a comment
This is my reply to Alex Mills: http://www.binpress.com/tutorial/introduction-to-rate-limiting-with-redis/155
Yes, with threads, processes, or an async reactor. That said...
$ redis-benchmark -n 2000000 -c 20 -q
[snip]
SET: 115141.05 requests per second
GET: 134048.27 requests per second
INCR: 180505.41 requests per second
LPUSH: 176056.33 requests per second
-- keys: key dest
-- argv: score member [score member ...]
-- e.g. keys: "data:test:1:2" "data-sum:test:1:2"
-- argv: 10 a 15 b
local key = KEYS[1]
local dest = KEYS[2]
local sum = tonumber(redis.call('get', dest)) or 0
for i=1, #ARGV, 2 do
@josiahcarlson
josiahcarlson / redis_slots.py
Created November 19, 2014 22:31
Some useful crc utility functions for Redis cluster
'''
Released into the public domain.
This code will append 2 bytes to the end of a key to force the key to a specific Redis slot.
'''
from collections import defaultdict
from itertools import imap
import struct
@josiahcarlson
josiahcarlson / rate_limit2.py
Last active April 17, 2022 09:22
Regular and sliding window rate limiting to accompany two blog posts.
'''
rate_limit2.py
Copyright 2014, Josiah Carlson - josiah.carlson@gmail.com
Released under the MIT license
This module intends to show how to perform standard and sliding-window rate
limits as a companion to the two articles posted on Binpress entitled
"Introduction to rate limiting with Redis", parts 1 and 2:
@josiahcarlson
josiahcarlson / result_multiplex.py
Created October 12, 2014 21:20
Reliably multiplex receiving results via Redis lists
'''
result_multiplex.py
Copyright 2014 Josiah Carlson
Released as LGPL 2 or 3, your choice.
Problem:
You have a task processor that writes the results of tasks to a specified key
@josiahcarlson
josiahcarlson / rate_limit.py
Last active February 25, 2022 14:33
Rate limiting with Redis primarily using Lua scripting
'''
rate_limit.py
Written May 7-8, 2014 by Josiah Carlson
Released under the MIT license.
Offers a simple interface for offering rate limiting on a per second, minute,
hour, and day basis. Useful for offering varying rate limits depending on user
behavior. Sliding window based limits offer per minute, hour, and day limits
@josiahcarlson
josiahcarlson / follow_file.py
Created April 13, 2014 18:03
Python function to offer "tail -F"-like functionality in Python
'''
follow_file.py
Written April 13, 2014 by Josiah Carlson
Released under the MIT license.
This could likely be made more efficient with the use of the pyinotify library,
and some of the module-level constants could be tuned for better performance,
depending on your platform.