Skip to content

Instantly share code, notes, and snippets.

@xaviershay
xaviershay / full_load_path_benchmark.rb
Created May 22, 2011 06:26
Benchmark for ruby require performance with LOAD_PATH
$LOAD_PATH.unshift(File.expand_path('f'))
x = 0
y = 0
require 'benchmark'
Benchmark.bm do |b|
step = 500
(0..4).each do |n|
@xaviershay
xaviershay / application_controller.rb
Created May 19, 2011 21:43
Access helper methods outside controllers and views
class ApplicationController < ActionController::Base
# Provide access to helper methods from outside controllers and views,
# such as in Presenter objects. Rails provides ActionController::Base.helpers,
# but this does not include any of our application helpers.
def self.all_helpers
@all_helpers_proxy ||= begin
# Start with just the rails helpers. This is the same method used
# by ActionController::Base.helpers
proxy = ActionView::Base.new.extend(_helpers)
@xaviershay
xaviershay / loader.rb
Created May 17, 2011 07:43
DataMapper hacks to generate raw SQL
# Yields the given models wrapped in Loader::Table objects that can be
# used to write SQL. The block should return an array with the SQL as
# the first element, and variable substitutions as the rest.
#
# Example
# using_sql(Article) {|a|
# ["SELECT #{a.*} FROM #{a} WHERE #{a.id} = ?", 1]
# }
def using_sql(*models)
opts = models.extract_options!
@xaviershay
xaviershay / ruby_require_speed.patch
Created May 1, 2011 01:00
This drastically speeds up require time on ruby 1.9 (17s down to 10s in our production rails app) , at the expense of disabling some edge case handling around autoloading. YMMV but it works for our rails app.
From 2fb8432982b7e8ffc4c05f912f47cd9ca7261427 Mon Sep 17 00:00:00 2001
From: Xavier Shay <[email protected]>
Date: Sun, 1 May 2011 10:25:18 +1000
Subject: [PATCH] Disable some edge cases in requiring to speed it up.
---
load.c | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
diff --git a/load.c b/load.c
{-# LANGUAGE TypeFamilies, QuasiQuotes, MultiParamTypeClasses, TemplateHaskell #-}
import Yesod
import Text.Hamlet
import Database.Persist
import Database.Persist.MongoDB
import Database.MongoDB
import Data.ByteString.Lazy.UTF8
import Control.Monad
@xaviershay
xaviershay / gist:865099
Created March 10, 2011 22:43
searchable.rb
# Mix this module into a DataMapper::Resource to get fast, indexed full
# text searching.
#
# class Post
# include DataMapper::Resource
# include Searchable
#
# property :title, String
# property :body, Text
#
# Trying to isolate some performance problems with collection rendering in a rails app.
#
# FAIL
#
# Need to move to a bare rails app next.
#
# Run this RUBY_PROF or BENCHMARK = 1 in your env.
require 'rubygems'
require 'dm-core'
require 'dm-migrations'
@xaviershay
xaviershay / gist:787378
Created January 20, 2011 04:12
Bad idea of the week: ARel + DataMapper
# Because I can't figure out how the fuck to get SQL out of datamapper itself
class PGEngine < Arel::Visitors::PostgreSQL
attr_accessor :engine
def initialize(engine)
super
self.engine = engine
end
def connection_pool
require 'rubygems'
require 'dm-core'
require 'dm-migrations'
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, 'postgres://localhost/test') # createdb test
class Group
include DataMapper::Resource
@xaviershay
xaviershay / gist:746997
Created December 19, 2010 00:10
IO code to interpret JS style hashes
# Code to interpret javascript style hashes, without having to source them from
# a different file (as happens when you use the operator table. The key
# does not have to be surrounded in quotes.
#
# {key: "my value}
# Resolver is a proxy object needed to figure out the key of a pair.
Resolver := Object clone
Resolver resolve := method(
# Jiggery-pokery to extract name and value from the message without eval-ing it