Kurt Stephens

Nerd Up!

ruby

  • This discussion is closed: you can't post new comments.
  • This discussion is closed: you can't post new comments.
  • This discussion is closed: you can't post new comments.
  • This discussion is closed: you can't post new comments.
  • This discussion is closed: you can't post new comments.
  • This discussion is closed: you can't post new comments.
  • This discussion is closed: you can't post new comments.
  • This discussion is closed: you can't post new comments.
  • This discussion is closed: you can't post new comments.

Ruby: Performance of Symbol Construction

Kurt on Sun, 2008-11-23 03:54.

Measurements of Symbol Constructor Expressions.

n=10_000_000

ruby 1.8.6 (2008-08-08 patchlevel 286) [i686-linux]:

> /cnu/bin/ruby symbol_benchmark.rb
                               user     system      total        real
Null Test                  0.740000   0.000000   0.740000 (  0.742914)
'foo_bar'                  1.670000   0.000000   1.670000 (  1.661374)
"foo_bar"                  1.620000   0.000000   1.620000 (  1.625221)
:foo_bar                   0.890000   0.000000   0.890000 (  0.886903)
:'foo_bar'                 0.880000   0.000000   0.880000 (  0.878555)
<br class="clear" />

How to duplicate DataMapper objects

Kurt on Sun, 2008-11-16 21:04.

How to create a deep clone of a DataMapper::Resource object so it can be stored in a different repository.

This does not work:

obj = Foo.first
obj = obj.dup
repository(:other) { obj.save }

<br class="clear" />

Ruby DataMapper : dm-more Gem cannot be installed

Kurt on Sun, 2008-07-20 06:13.

dm-more depends on merb_datamapper (!?!) version 0.9.3 which does not appear to exist yet:

++ gem install dm-more
ERROR:  Error installing dm-more:
	dm-more requires merb_datamapper (= 0.9.3, runtime)

Not sure what to do about it.


Postgres and Ruby: Blocking LISTENs for NOTIFY

Kurt on Fri, 2008-05-23 15:56.

http://devblog.famundo.com/articles/2006/12/07/improving-postgres-listen...

Unfortunately this hack blocks all threads. It should use rb_thread_select(), not select().


Ruby 1.9 : postgres gem patch

Kurt on Thu, 2008-05-22 13:56.

I did this little hackage during a boring PGcon 2008 talk…

This patch allows the postgres gem 0.7.9.2008.01.28 to be compiled under Ruby 1.9 against PostgreSQL 8.3.0 client libraries.

It appears to work against a PostgreSQL 8.1 server, but I have not run any detailed testing, yet.

--- ext/extconf.rb.orig	2008-05-22 09:47:06.000000000 -0500
+++ ext/extconf.rb	2008-05-22 09:52:28.000000000 -0500
@@ -1,6 +1,9 @@

+# Ruby 1.9
+PLATFORM = RUBY_PLATFORM unless defined? PLATFORM
+

 # windows compatibility, need different library name
 if(PLATFORM =~ /mingw|mswin/) then
<br class="clear" />

Ruby Internals: Why RUBY_FIXNUM_FLAG should be 0x00

Kurt on Tue, 2008-01-01 04:06.

Type tags in MRI Ruby VALUE

Internally, values in MRI Ruby are 32-bit (at least for 32-bit processors). Some of the least-significant bits are used to store type information. See the VALUE definition in include/ruby/ruby.h. Using type tag bits avoids allocating additional memory for commonly-used immutable values, like integers.

Ruby uses a single-bit tag of 0x01 as the Fixnum type tag. The remaining bits, are used to store the Fixnum’s signed value. This is an immediate value; it doesn’t require storage for the Fixnum value to be allocated, unlike the heap space that would be required for a String. Other types will use different tag bits.


Ruby : Regexp#to_proc

Kurt on Sat, 2007-12-15 03:02.

Helpful in IRB:


class Regexp
  def to_proc
    @proc ||= lambda { | x | self.match(x) }
  end
end

As in:


irb(main):001:0> pp Object.methods.sort.select(&/meth/)
["instance_method",
 "instance_methods",
 "method",
 "method_defined?",
 "methods",
 "private_class_method",
 "private_instance_methods",
 "private_method_defined?",
 "private_methods",
 "protected_instance_methods",
 "protected_method_defined?",
 "protected_methods",
 "public_class_method",
 "public_instance_methods",
 "public_method_defined?",
 "public_methods",
 "singleton_methods"]
<br class="clear" />

RubyGems : Gem::SourceIndex does not honor GEM_PATH ordering

Kurt on Wed, 2007-10-17 14:37.

Gem::SourceIndex does not honor GEM_PATH ordering.

See: http://rubyforge.org/tracker/index.php?func=detail&aid=14816&group_id=12...

Gem::SourceIndex#load_gems_in calls #add_spec for all gems found in #spec_dirs, in the order of Gem.path, however #add_spec
overwrites previous gem_specs in the @gems Hash:


Ruby : Tight Code, Floppy Performance

Kurt on Tue, 2007-10-09 20:04.

So you’re coding some ruby, and you do the obligatory caching of a computation:


def foo(x)
  666
end

$cache = nil
def cryptic_cached_foo
  ($cache ||= [ foo("bar") ]).first
end

def nicey_cached_foo
  unless $cache
    $cache = [ foo("bar") ]
  end
  $cache.first
end

A discussion came up at work: is nicey_cached_foo “better” than cryptic_cached_foo? Obviously, nicey_cached_foo is more readable, but when I find myself doing the same pattern over and over, I prefer the terse cryptic_cached_foo. I expected cryptic_cached_foo to be faster. Oh boy, was I wrong….

Syndicate content

Primary links

Syndicate

Syndicate content

Browse archives

« September 2010  
Mo Tu We Th Fr Sa Su
    1 2 3 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30