[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: RCR 304: reference/pointer concept

Eric Mahurin

5/9/2005 6:09:00 PM

Here are a couple of things this might be useful for (I just
put these in the RCR). I'm sorry I didn't provide something
more concrete sooner. Regarding #2, I've used references to
hash values many times to get better efficiency (in perl).
i.e.

$a = \$hash{$key}
# read and modify $$a

Doing this, only one lookup is needed (for making the ref) as
opposed to 2 (for the get and set). As far as I know, we can't
get this extra efficiency for hash values in Ruby. You can
create references to hash values in my Ruby implementation, but
it doesn't give you any additional efficiency. That would need
to be done at the C level. And that would be useful -
especially for large hashes.


1. pass-by-reference or pass an lvalue. From what I understand,
the way you would write/use a Ruby method that modified lvalues
right now, would be like this:

def byvalue(*args)
# read and modify args
args
end
a,b,c = *byvalue(a,b,c)

With references, you could do this:

def byref(*args)
# read and modify args[i][]
end
byref(ref{:a},ref{:b},ref{:c})

With this simple variable case, there isn't much advantage, but
when you are dealing with something deep within an object, it
can offer an advantage:

a["hi"][3],io.pos = *byvalue(a["hi"][3],io.pos)

vs.

byref(a["hi"].ref[3],io.ref.pos)

Also, with the byvalue case, you are forced into the assign,
whereas byref may choose not to do this assign. Depending on
the object, this could even prevent an exception (i.e.
IO#pos=).

2. Although not addressed in this implementation, a reference
could provide some efficiency when ther is some work that could
be saved when you get or set the same thing 2 or more times. A
primary example of this would be a reference to a hash value.
With a good implementation of a reference to a hash value, you
would only need to do the hash lookup once (while making the
reference). Compare this to what you have now - you do a hash
lookup every time you get/set a given hash value.

--- Bill Atkins <batkins57@gmail.com> wrote:
> What would this accomplish?
>
> On 5/9/05, Eric Mahurin <eric_mahurin@yahoo.com> wrote:
> > I just submitted RCR 304 with the rubyforge 'reference'
> > project/gem implementation:
> >
> > http://www.rcrchive.net/rc...
> >
> > Go vote and make comments on it!
> >
> > Discover Yahoo!
> > Use Yahoo! to plan a weekend, have fun online and more.
> Check it out!
> > http://discover....
> >
> >
>
>
> --
> Bill Atkins
>
>



__________________________________
Yahoo! Mail Mobile
Take Yahoo! Mail with you! Check email on your mobile phone.
http://mobile.yahoo.com/...


2 Answers

Austin Ziegler

5/9/2005 6:26:00 PM

0

On 5/9/05, Eric Mahurin <eric_mahurin@yahoo.com> wrote:
> Here are a couple of things this might be useful for (I just
> put these in the RCR). I'm sorry I didn't provide something
> more concrete sooner. Regarding #2, I've used references to
> hash values many times to get better efficiency (in perl).
> i.e.

Premature optimization is the root of all evil, as they say. As
such, I consider any suggestion that this abominable form would
increase its value by adding "optimization" ... questionable. At
best. Suggesting that it's necessary/useful for large hashes is
questionable as hash lookups are supposed to be O(1) in any case.

If the hash lookup in Ruby is worse than that, then perhaps we need
a new hash implementation, not an abominable "ref" in the core.

> 1. pass-by-reference or pass an lvalue. From what I understand,
> the way you would write/use a Ruby method that modified lvalues
> right now, would be like this:
> def byvalue(*args)
> # read and modify args
> args
> end
>
a, b, c = byvalue(a, b, c)

This is correct, with the modification that I've made above. This is
also the right way to do this, in Ruby.

> With references, you could do this:
> def byref(*args)
> # read and modify args[i][]
> end
> byref(ref{:a},ref{:b},ref{:c})

1. This is precisely the case that I think makes your misguided
proposal abominable. This is precisely the case that should NOT
be used, because it introduces -- as Robert Klemme pointed out --
side effects to the method call that are not at all apparent.

2. This syntax, as well, is precisely the syntax that I think makes
this proposal so disastrously ugly. Not only do I have to *call*
the method differently (e.g., with "ref { :a }" etc.), but I have
to access the provided values differently. All so that I can
change the values "in place." Not exactly efficient for the
developer, and genuinely fugly.

3. This would then encourage a poor programming style produced by
people who have experience with languages like C, C++, and Perl,
where you have to do this to do anything truly useful. Why should
we carry over bad, broken programming habits to a cleaner
language like Ruby?

> With this simple variable case, there isn't much advantage, but
> when you are dealing with something deep within an object, it
> can offer an advantage:

4. If you are dealing with something deep in an object and need to
pass it that way for possible modification, then your design
needs shifting. Sorry, but it's true. At a minimum, it should be:

val, pos = method(a["hi"][3], io.pos)
a["hi"][3], io.pos = val, pos

This is a bad RCR for a bad concept (for Ruby) and should be
withdrawn.

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca



Paul Brannan

5/10/2005 2:12:00 PM

0

On Tue, May 10, 2005 at 03:26:14AM +0900, Austin Ziegler wrote:
> Premature optimization is the root of all evil, as they say. As
> such, I consider any suggestion that this abominable form would
> increase its value by adding "optimization" ... questionable. At
> best. Suggesting that it's necessary/useful for large hashes is
> questionable as hash lookups are supposed to be O(1) in any case.

Hash lookups using a hash value are O(1). However, calculating the hash
value need not be an O(1) operation. This is why it is sometimes more
efficient to use trees instead of hashes; with a tree, there is no need
to calculate a hash value.

I'm not advocating the RCR, of course. I do not dislike the idea of
references-to-references, but adding them to Ruby is non-trivial and
unnecessary, given that we have other mechanisms (such as bindings) that
perform similar functions.

Paul