Skip to content

Instantly share code, notes, and snippets.

@benolee
benolee / 00_NES_DOOM.mkd
Last active April 13, 2023 23:47
NES DOOM

NES DOOM

https://www.youtube.com/watch?v=gCWhWBtu0LA

21_various_games

The NES is connected to the graphics memory on the cartridge via groups of signals called buses. There's the Address Bus and the Data Bus. When the NES wants to access graphics memory on the cartridge, it puts the memory address it wants in the Address Bus. The memory chip then grabs the data at this address and transfers it to the NES via the Data Bus.

10_address_bus_data_bus

@benolee
benolee / 00 README.md
Last active April 13, 2023 23:48
arg_scanner gem

terminal 1

$ brew install glib
$ C_INCLUDE_PATH=/usr/local/opt/glib/include/glib-2.0/ gem install arg_scanner
$ mkfifo /tmp/ruby-type-inference-pipe-42
$ arg-scanner --type-tracker --pipe-file-path=/tmp/ruby-type-inference-pipe-42 bundle exec rails console
[1] pry(main)> app.get('/')

terminal 2. sample output. this is prettified and truncated, btw.

What is this?

This is a ruby C extension that defines a global method named current_iseq. It returns the instruction sequence of the context it was called from.

I couldn't find any other way to get a reference to the method being called from within the method itself. The closest you can get is probably __callee__ which just gives you the name of the method. There's no guarantee that getting a method object using something like method(__callee__) will actually give you the method it's being called from.

# frozen_string_literal: true
class SafeHash < Hash
def initialize(namespace)
@namespace = namespace
@lock = Concurrent::ReadWriteLock.new
super
merge!({ @namespace => Concurrent::Map.new })
end
class TBQueue
def initialize size
tvar = Concurrent::TVar
@read = tvar.new []
@write = tvar.new []
@rsize = tvar.new 0
@wsize = tvar.new size
end

Scaling your API with rate limiters

The following are examples of the four types rate limiters discussed in the accompanying blog post. In the examples below I've used pseudocode-like Ruby, so if you're unfamiliar with Ruby you should be able to easily translate this approach to other languages. Complete examples in Ruby are also provided later in this gist.

In most cases you'll want all these examples to be classes, but I've used simple functions here to keep the code samples brief.

Request rate limiter

This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.

@benolee
benolee / fiber_sleep.rb
Created February 4, 2019 21:54 — forked from wycleffsean/fiber_sleep.rb
Fiber Sleep
require 'fiber'
require 'concurrent' # gem install concurrent-ruby
Thread.abort_on_exception = true
class Async
def self.perform(&block)
instance = new
instance.instance_eval(&block)
until instance.dispatched.value.zero? do
unless instance.yields.empty?
@benolee
benolee / gist:dfbedcd2793b4a013f10611526d4847f
Created January 13, 2018 04:30
Ruby's performance tuning way
@benolee
benolee / Dynflow bottlenecks.md
Created January 13, 2018 04:27 — forked from ShimShtein/Dynflow bottlenecks.md
Dynflow performance analysis.

break /home/shim/Documents/foreman/dynflow/lib/dynflow/utils.rb:60

Why do we need indifferent_hash while saving?

 "/home/shim/Documents/foreman/dynflow/lib/dynflow/utils.rb:60:in `indifferent_hash'",
 "/home/shim/Documents/foreman/dynflow/lib/dynflow/persistence_adapters/sequel.rb:268:in `extract_metadata'",
 "/home/shim/Documents/foreman/dynflow/lib/dynflow/persistence_adapters/sequel.rb:226:in `prepare_record'",
 "/home/shim/Documents/foreman/dynflow/lib/dynflow/persistence_adapters/sequel.rb:236:in `save'",
 "/home/shim/Documents/foreman/dynflow/lib/dynflow/persistence_adapters/sequel.rb:125:in `save_action'",
@benolee
benolee / socat.md
Created November 17, 2017 17:28 — forked from robbwagoner/socat.md
nginx socket socat