Archive for the 'Ruby' Category

Calling on the GC after Rubygems

Calling GC.start immediately after requiring rubygems cut the raw string size down to 45%. heh.

Please note (update): Eric has said (see comments) that this is better in the latest build of rubygems, and that is now out (0.9.5). Re-running the test (I have 98 gems installed at present), I still see a 75% drop in raw in-memory string size GC’ing after rubygems has loaded. I suspect this has little to do with rubygems itself, but more the manner in which ruby caches file data as it is loaded. That and the fact that the ruby GC has no reason to run automatically at this stage (It is programmed not to, which is another discussion already covered by _why). Whether or not we should care is a whole other kettle of fish. I may have a look into this in the future, as there are actually good points on several sides, from pre-optimisation, to platform optimisation, to actual speed (sometimes it may not matter), to memory fragmentation. I don’t have an answer, and likely no one does right now, and certainly not for all cases. This is information only, it might help someone.

[source:ruby]
RAW = true

def write_strings_to fn
open(fn, ‘w+’) do |f|
ObjectSpace.each_object do |o|
f.send((RAW ? :print : :puts), o) if o.class == String
end
end
end

file = %w[strings_no_gems.log strings_gems_no_gc.log strings_gems.log]

GC.start
write_strings_to file[ 0 ]

require ‘rubygems’
write_strings_to file[ 1 ]

GC.start
write_strings_to file[ 2 ]

last = File.size file[ 0 ]
file.each do |fn|
cur = File.size(fn).to_f
pcge = cur / last * 100
puts “#%26s: %15d bytes, change: %8d%” % [fn, cur, pcge]
last = cur
end
[/source]

The raw byte sizes fluctuated a small amount, but in general, the percentage values stayed the same.

# out_strs.rb
#       strings_no_gems.log:            1707 bytes, change:      100%
#    strings_gems_no_gc.log:           99578 bytes, change:     5833%
#          strings_gems.log:           45533 bytes, change:       45%

I have around 150 gems installed, and requiring rubygems grows the ruby process by around 5mb on my machine.

Clear out your old gems… It could save you time and ram…

So I was just looking through some of the ObjectSpace of one of my apps, and I decided to have a scan over the String list by hand. I’m kinda glad I did, because after my discovery, my copy of ruby is loading quite a bit faster, regularly. No really.

I had facets installed. I’ve never actually used facets in one of my projects, but I have had a look through it a few times in the past mostly out of curiosity. Since I’ve done a gem update before, to update all the gems on my machine, I actually had two copies installed.

Well, comparatively, I went from 3.2mb of strings, down to 2.6mb in my application load, by uninstalling facets.

The observation here is though, that rubygems loads up quite a bit of information about the documentation and files installed on your system as gems. If you want your ruby load times to remain snappy, start removing old gems that you never use. Always remove facets (if you can)… If you use it, start splitting out what you need, and forget the gem. Yes it’s full of useful stuffs and examples, but it’s also huge.

It’s also worth noting I think, that I was on facets versions:  facets-1.4.5 and facets-1.8.54. I installed facets-2.0 just to have a look, and it’s become a little smaller. Anyway, I’m still having it off my machine for now.

Moved back to Vi

In my ongoing search for an editor that will make me happy, I recently moved back to Vim.

I have been using the “e-text editor” recently, when working on Windows. The editor went version 1.0 about a month ago, and for the months I have used it, it has been quite the pleasant experience. Mostly for the native feel within the actual text input area. Something which I was previously too lazy to build into a .vimrc. Sadly my trial for “e” ran out, and I don’t feel that it’s fast enough or well integrated enough for my needs yet. The dependency on Cygwin saw me doing a lot of configuration so that I could test various cross platform apps in a sane way, which is a shame.

I also suffered from the load times. I know, I know, this probably means I’m not using the editor correctly, but honestly, an editor shouldn’t take a significant time to load. Gvim 7.1 loads instantly currently, and that’s lovely to come back to. The ability to very quickly and easily issue commands to anywhere, from anywhere, is also very useful - all that’s really required for this is a native command launch at the press of a button, something which too many editors simply don’t have.

Vim and Emacs have been around so damned long that they’re just two of the most mature text editors out there. They’re also written in native C for most platforms. This is something that it’s hard to get away from. Some people say they have “featuritis”, and to a certain extent that may be true. That also equates to actually *having* features though.

I love syntax highlighting. Not because it’s pretty, but because I read code in blocks, and a reasonable colour set can really make reading code faster. Simply put, the syntax highlighting in Vi is just better. I should imagine similar capabilities are present in Emacs also, and that for both these editors, this once again comes from maturity.

Highlighting Failures for Nested Heredoc in Vim

Of course, anyone can break the syntax highlighting, as above, but still…

A little work on my dusty old vimfiles, and vim is feeling quite nice to work in again. Shame that some serious configuration is needed in order to really make vim feel intuitive. Of course, my opinion on intuitive controls for the editor actually changes, depending on whether I’m in a gui environment or a terminal - and that too makes life yet more complicated. Vim delivers though.

Is it just me or has the UI speed of Vim increased in recent versions? At least on win32…

I’ll never be able to completely leave Vi of course, being that it’s about the only thing you can regularly rely on finding on a *nix system. Even then that’s not always the case.

RMagick and RubyGems 0.9.4+

[source:ruby]require ‘RMagick.rb’[/source]

Got it?

Hint: Notice the “.rb”

Right.

Okay, some people didn’t get it. This is the fix for getting RMagick to work on win32 when using updated versions of rubygems.

You’d otherwise commonly see:

(no primitives defined):Magick::Draw (NoMethodError)

Profiling learns you…

This is sitting at the top of a pile of tests I wrote today in search of speeding up some areas of a program generating relatively short strings frequently.
[source:ruby]
##
# Already learned:
# * memoization is no good for short strings, even when using symbols to lookup data.
# * String#% is very slow
# * Array#to_s is faster than Array#join, for strings of this nature
# * Interpolation is faster than either of these methods
# * Frozen strings are no aid over interpolation
# * Symbol#to_s is slower than strings, for making strings
# * ? … : … is actually faster than if … else … end, (tested on 1.8.5 win32)
# (warning, reason for this was not the operation itself, and
# using if … then … else … end on one line changed that)!
# * return … is slower than not using it
#
# From #ruby-lang:
#
# 02:38 flgr: interestingly, Array#empty? can be implemented as ary.inject(true) { false }
# 02:39 flgr: what this means is that you can replace ary.empty?() ? a : b with
# ary.inject(a){ b } all the time
# 02:50 flgr: raggi: you can probably make it faster by using { break b } :)
#
# 02:56 LoganCapaldo: well you can write a.empty? ? b : a[0] as a.fetch(0, b) too
# 02:56 LoganCapaldo: for the case that the thingy you want is an element in the array
# 02:56 LoganCapaldo: or maybe that ony works with hashes
# 02:57 LoganCapaldo: nope it works with arrays too
##
[/source]

Ebonics Pluralizations…

I was thinking, someone should make an ebonics pluralization plugin for rails.

flexibility

mmmm…. parser….

[source:ruby]
def foo( a = ( def foo; puts “BAR”; end; method(:foo) ) )
puts “FOO”
a.call
end
[/source]

can’t say it’s all that safe for “production use” though.

lol, cocksucker.

http://dev.rubyonrails.org/ticket/8681

What was that old thinking? Oh yeah…

grep the source code to your operating system for swearwords and then tell me it’s worth a pinch of salt…

ruby -e “p(!?<?!!?>:?\2+?()”

The answer to…

p.s. I had to write it: (!?&lt;?!!?&gt;:?\2+?()
p.p.s. and that just got worse.

[source:ruby]p ?%./(!!?*??&/?^+0.0:!!?()[/source]
[source:ruby]p !!:’just another ruby hacker?’?!!$::!!?”[/source]

[source:ruby]
p(%|#{<<-”}|[?\0...-0x0_2]<<% == <<%< ><<%&#{?../ 0.0_0 }&)
A Big Number Divided By Zero

ruby &= { :spirit => :creative }
[/source]

obfuscated ruby.

SourceCraft » Colored autotest with notifications on Windows

SourceCraft » Colored autotest with notifications on Windows

« Previous PageNext Page »