[ANN] called_from 0.1.0 released

M

makoto kuwata

I releaded called_from 0.1.0.
http://github.com/kwatch/called_from

Kernel#caller() is very useful, but very slow.
'called_from' solves this problem.


About
-----

'called_from' provides called_from() global function
which gets filename and line number of caller.

In short:

require 'called_from'
filename, linenum, function = called_from(1)

is equivarent to:

caller(1)[0] =~ /:(\d+)( in `(.*)')?/
filename, linenum, function = $`, $1, $2

But called_from() is much faster than caller()[0].



Example
-------

require 'called_from'

##
## report not only what sql is queried but also from where query()
is called.
## (this is very convenient for development and debugging)
## in such examle, speed of called_from() is very important.
##
def query(sql)
filename, linenum, function = called_from()
log.debug("#{Time.now}: #{sql} (called from #{filename}:#
{linenum})")
return connection.query(sql)
end

##
## if cache data is older than template file's timestamp,
## refresh cache data.
## it is very important that called_from() is much faster than
caller()[0]
## because speed is necessary for caching.
##
def cache_helper(cache_key)
## check template file's timestamp
template_filename, linenum, function = called_from()
timestamp = File.mtime(template_filename)
## if cache data is newer than template's timestamp, return data
data = @cache.get(cache_key, :timestamp=>timestamp)
return data if data
## if cache data is older than template's timestamp, refresh it
data = yield()
@cache.set(cache_key, data)
return data
end



Benchmark
---------

Here is an example result of benchmark on Merb application.
This shows that called_from() is more than 30 times faster than caller
()[0].


*** n=100000
user system total
real
caller()[0] 7.920000 0.400000 8.320000
( 8.985343)
caller()[0] (*1) 8.590000 0.420000 9.010000
( 9.804065)
called_from() 0.240000 0.010000 0.250000
( 0.257151)
called_from() (*2) 0.250000 0.000000 0.250000
( 0.268603)


(*1) retrieve filename and line number using pattern match (=~ /:
(\d+)/)
(*2) retrieve filename and line number



Installation
------------

$ sudo gem install called_from

Or

$ tar xzf called_from_X.X.X.tar.gz
$ cd called_from_X.X.X/
$ ruby setup.rb config
$ ruby setup.rb setup
$ sudo ruby setup.rb install

Notice: called_from is implemented in both C-extention and pure Ruby.
C-extention is available on UNIX-like OS and Ruby 1.8.x.
 
T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

Hi, this is really interesting, but:

$ gem install called_from
Building native extensions. This could take a while...
/Users/tony/.rvm/jruby-1.3.1/lib/ruby/1.8/mkmf.rb:7: JRuby does not support
native extensions. Check wiki.jruby.org for alternatives.
(NotImplementedError)

We are in the process of moving our codebase onto JRuby. It's nice you have
a pure Ruby version available but not if I can't install the gem on JRuby.

I releaded called_from 0.1.0.
http://github.com/kwatch/called_from

Kernel#caller() is very useful, but very slow.
'called_from' solves this problem.


About
-----

'called_from' provides called_from() global function
which gets filename and line number of caller.

In short:

require 'called_from'
filename, linenum, function = called_from(1)

is equivarent to:

caller(1)[0] =~ /:(\d+)( in `(.*)')?/
filename, linenum, function = $`, $1, $2

But called_from() is much faster than caller()[0].



Example
-------

require 'called_from'

##
## report not only what sql is queried but also from where query()
is called.
## (this is very convenient for development and debugging)
## in such examle, speed of called_from() is very important.
##
def query(sql)
filename, linenum, function = called_from()
log.debug("#{Time.now}: #{sql} (called from #{filename}:#
{linenum})")
return connection.query(sql)
end

##
## if cache data is older than template file's timestamp,
## refresh cache data.
## it is very important that called_from() is much faster than
caller()[0]
## because speed is necessary for caching.
##
def cache_helper(cache_key)
## check template file's timestamp
template_filename, linenum, function = called_from()
timestamp = File.mtime(template_filename)
## if cache data is newer than template's timestamp, return data
data = @cache.get(cache_key, :timestamp=>timestamp)
return data if data
## if cache data is older than template's timestamp, refresh it
data = yield()
@cache.set(cache_key, data)
return data
end



Benchmark
---------

Here is an example result of benchmark on Merb application.
This shows that called_from() is more than 30 times faster than caller
()[0].


*** n=100000
user system total
real
caller()[0] 7.920000 0.400000 8.320000
( 8.985343)
caller()[0] (*1) 8.590000 0.420000 9.010000
( 9.804065)
called_from() 0.240000 0.010000 0.250000
( 0.257151)
called_from() (*2) 0.250000 0.000000 0.250000
( 0.268603)


(*1) retrieve filename and line number using pattern match (=~ /:
(\d+)/)
(*2) retrieve filename and line number



Installation
------------

$ sudo gem install called_from

Or

$ tar xzf called_from_X.X.X.tar.gz
$ cd called_from_X.X.X/
$ ruby setup.rb config
$ ruby setup.rb setup
$ sudo ruby setup.rb install

Notice: called_from is implemented in both C-extention and pure Ruby.
C-extention is available on UNIX-like OS and Ruby 1.8.x.
 
J

Jeremy Kemper

This is probably the most common usage of Kernel#caller. You've made
it more useful and much faster -- great extension!

jeremy

I releaded called_from 0.1.0.
http://github.com/kwatch/called_from

Kernel#caller() is very useful, but very slow.
'called_from' solves this problem.


About
-----

'called_from' provides called_from() global function
which gets filename and line number of caller.

In short:

=A0 =A0require 'called_from'
=A0 =A0filename, linenum, function =3D called_from(1)

is equivarent to:

=A0 =A0caller(1)[0] =3D~ /:(\d+)( in `(.*)')?/
=A0 =A0filename, linenum, function =3D $`, $1, $2

But called_from() is much faster than caller()[0].



Example
-------

=A0 =A0require 'called_from'

=A0 =A0##
=A0 =A0## report not only what sql is queried but also from where query()
is called.
=A0 =A0## (this is very convenient for development and debugging)
=A0 =A0## in such examle, speed of called_from() is very important.
=A0 =A0##
=A0 =A0def query(sql)
=A0 =A0 =A0filename, linenum, function =3D called_from()
=A0 =A0 =A0log.debug("#{Time.now}: #{sql} (called from #{filename}:#
{linenum})")
=A0 =A0 =A0return connection.query(sql)
=A0 =A0end

=A0 =A0##
=A0 =A0## if cache data is older than template file's timestamp,
=A0 =A0## refresh cache data.
=A0 =A0## it is very important that called_from() is much faster than
caller()[0]
=A0 =A0## because speed is necessary for caching.
=A0 =A0##
=A0 =A0def cache_helper(cache_key)
=A0 =A0 =A0## check template file's timestamp
=A0 =A0 =A0template_filename, linenum, function =3D called_from()
=A0 =A0 =A0timestamp =3D File.mtime(template_filename)
=A0 =A0 =A0## if cache data is newer than template's timestamp, return da= ta
=A0 =A0 =A0data =3D @cache.get(cache_key, :timestamp=3D>timestamp)
=A0 =A0 =A0return data if data
=A0 =A0 =A0## if cache data is older than template's timestamp, refresh i= t
=A0 =A0 =A0data =3D yield()
=A0 =A0 [email protected](cache_key, data)
=A0 =A0 =A0return data
=A0 =A0end



Benchmark
---------

Here is an example result of benchmark on Merb application.
This shows that called_from() is more than 30 times faster than caller
()[0].


=A0 =A0*** n=3D100000
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 user =A0 = =A0 system =A0 =A0 =A0total
real
=A0 =A0caller()[0] =A0 =A0 =A0 =A0 =A0 =A0 =A07.920000 =A0 0.400000 =A0 8= 320000
( =A08.985343)
=A0 =A0caller()[0] =A0 (*1) =A0 =A0 =A0 8.590000 =A0 0.420000 =A0 9.01000= 0
( =A09.804065)
=A0 =A0called_from() =A0 =A0 =A0 =A0 =A0 =A00.240000 =A0 0.010000 =A0 0.2= 50000
( =A00.257151)
=A0 =A0called_from() (*2) =A0 =A0 =A0 0.250000 =A0 0.000000 =A0 0.250000
( =A00.268603)


=A0 =A0(*1) retrieve filename and line number using pattern match (=3D~ /= :
(\d+)/)
=A0 =A0(*2) retrieve filename and line number



Installation
------------

=A0 =A0$ sudo gem install called_from

Or

=A0 =A0$ tar xzf called_from_X.X.X.tar.gz
=A0 =A0$ cd called_from_X.X.X/
=A0 =A0$ ruby setup.rb config
=A0 =A0$ ruby setup.rb setup
=A0 =A0$ sudo ruby setup.rb install

Notice: called_from is implemented in both C-extention and pure Ruby.
C-extention is available on UNIX-like OS and Ruby 1.8.x.
 
J

Judson Lester

[Note: parts of this message were removed to make it a legal post.]

I've long wished that caller returned an array of BacktraceLine... likewise
Exception#backtrace.

class BacktraceLine
attr_reader :line, :file, :method, :arguments
end

Not sure if it's a daydream or an REP...

Judson

This is probably the most common usage of Kernel#caller. You've made
it more useful and much faster -- great extension!

jeremy

I releaded called_from 0.1.0.
http://github.com/kwatch/called_from

Kernel#caller() is very useful, but very slow.
'called_from' solves this problem.


About
-----

'called_from' provides called_from() global function
which gets filename and line number of caller.

In short:

require 'called_from'
filename, linenum, function = called_from(1)

is equivarent to:

caller(1)[0] =~ /:(\d+)( in `(.*)')?/
filename, linenum, function = $`, $1, $2

But called_from() is much faster than caller()[0].



Example
-------

require 'called_from'

##
## report not only what sql is queried but also from where query()
is called.
## (this is very convenient for development and debugging)
## in such examle, speed of called_from() is very important.
##
def query(sql)
filename, linenum, function = called_from()
log.debug("#{Time.now}: #{sql} (called from #{filename}:#
{linenum})")
return connection.query(sql)
end

##
## if cache data is older than template file's timestamp,
## refresh cache data.
## it is very important that called_from() is much faster than
caller()[0]
## because speed is necessary for caching.
##
def cache_helper(cache_key)
## check template file's timestamp
template_filename, linenum, function = called_from()
timestamp = File.mtime(template_filename)
## if cache data is newer than template's timestamp, return data
data = @cache.get(cache_key, :timestamp=>timestamp)
return data if data
## if cache data is older than template's timestamp, refresh it
data = yield()
@cache.set(cache_key, data)
return data
end



Benchmark
---------

Here is an example result of benchmark on Merb application.
This shows that called_from() is more than 30 times faster than caller
()[0].


*** n=100000
user system total
real
caller()[0] 7.920000 0.400000 8.320000
( 8.985343)
caller()[0] (*1) 8.590000 0.420000 9.010000
( 9.804065)
called_from() 0.240000 0.010000 0.250000
( 0.257151)
called_from() (*2) 0.250000 0.000000 0.250000
( 0.268603)


(*1) retrieve filename and line number using pattern match (=~ /:
(\d+)/)
(*2) retrieve filename and line number



Installation
------------

$ sudo gem install called_from

Or

$ tar xzf called_from_X.X.X.tar.gz
$ cd called_from_X.X.X/
$ ruby setup.rb config
$ ruby setup.rb setup
$ sudo ruby setup.rb install

Notice: called_from is implemented in both C-extention and pure Ruby.
C-extention is available on UNIX-like OS and Ruby 1.8.x.
 
R

Roger Pack

We are in the process of moving our codebase onto JRuby. It's nice you
have
a pure Ruby version available but not if I can't install the gem on
JRuby.

Also fails on 1.9. I know there was a commit recently to allow
Thread.list.map &:backtrace (can't seem to find it right now).
so that might help.
Thanks!
-r
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,951
Messages
2,570,113
Members
46,699
Latest member
Abigail89S

Latest Threads

Top