IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New They're mostly about eliminating casting in collections
In fact, I'd say they're entirely about that.

The J-heads don't want to incur the cost of multiple binary versions of a so-called generic method. Personally, I think any code that makes use of a parameterized type should implement method calls via reflection - then it would work.

This has been rejected most likely because invocation by reflection is a dog performance-wise. Of course, none of the J-heads care to consider that maybe they ought to fix their toy VM so that invocation by reflection is fast.


The tree of research must from time to time be refreshed with the blood of bean counters.
     -- Alan Kay
New as a non jhead or any recent programmer
could you expound on "method calls via reflection" perhaps in the olden days we called it sommat else or more likely this is new to me and perhaps others so an explaination would be educational.
thanx,
bill
when I was young I envisioned myself as the embodiment of Trinity, Now I realize I have turned into the Bambino
questions, help? [link|mailto:pappas@catholic.org|email pappas at catholic.org]
New Sure - Ever tried BeanShell?
You can lookup and call any method on any object you like in Java - you just have to jump through hoops to do it.

Suppose you have an object - you don't know what class it is but you expect it to implement int add(int, int); You could lookup and invoke the method like this:

import java.lang.reflect.*;
...
Class cls = object.getClass(); // get the object's class
Class[] argTypes = { Integer.class, Integer.class }; // specify the types of the methods arguments
Method add = cls.getMethod("add",argTypes); // this is an objectified function pointer
Object[] args = { new Integer(5); new Integer(7); } // pack up the arguments in object wrappers in an array
int result = ((Integer) add.invoke(object,args)).intValue(); // call the thing and unwrap the result

It doesn't matter what class object is, as long as you can find a method add taking a couple of ints then you can arrange to call it. This is how BeanShell works. BeanShell is a scripting language that sits atop Java and uses reflection everywhere. Variables need not be declared or typed. The above code is the equivalent of

result = object.add(5,7);

in BeanShell. They could have made the compiler interpret invocations on parameterized types this way. They would have taken a performance hit though. In Java 1.3 I benchmarked the time required to call a method this way vs a regular method call and it was 50 times slower.

I have this nifty utility that uses reflection to get values from a path. You can do something like:

Object value = PropertyPath.valueForPath(object,"user.address.zip");

This is handy for doing GUI's. It is roughly the equivalent of user.getAddress().getZip(), but if address is null you'll get an exception using the latter case, but PropertyPath will just swallow all exceptions and give you null if it can't traverse the path for some reason. It does some neat tricks flattening collections too but I digress.

Doing more benchmarking, I found the bulk of the time being wasted was in class.getMethod(...); It turns out that doing method.invoke(object,args) is about 1.5 times the speed of object.add(int,int); This is quite decent. PropertyPath caches method lookups and builds a cache of AccessPaths indexed by class. So PropertyPath is about 1.6 times the speed of regular method calls. There's no reason the compiler couldn't use a similar technique and cache Method lookups in local static variables. The performance difference would have been minor considering the gain in flexibility. But I can just hear the justification (we wanted users of generics to incur no performance hits). Fine - doing nothing costs nothing. But there's no gain either.

And why is method lookup performance so bad? Because reflection was added as an afterthought and its use is officially "discouraged" because it circumvents the type system which runs counter to the Java philosophy.


The tree of research must from time to time be refreshed with the blood of bean counters.
     -- Alan Kay
Expand Edited by tuberculosis March 14, 2004, 01:17:09 AM EST
New That was pretty much Bruce Eckel's point of view
The other stuff that he said was interesting as well.

I get the sense that he is moving away from B&D and more towards dynamic languages...

Cheers,
Ben
"good ideas and bad code build communities, the other three combinations do not"
- [link|http://archives.real-time.com/pipermail/cocoon-devel/2000-October/003023.html|Stefano Mazzocchi]
New B&D is simpler - why complicate?
No one answered why all these complications are necessary.

FORTH code can self-modify in the simplest way by patching the address holding a pointer to the actual code. So, a word can alter its own action. Isn't this reflection? This is so simple, why are these things deliberately complicated to the point people write long essays about it?
-drl
New Why isn't Forth ideal?
Let me give you just a couple of reasons (of many) why Forth will not replace all other languages.

First of all not all algorithms can be implemented efficiently with stack manipulation. The existence of efficient algorithms that cannot be coded in Forth is an issue in some contexts.

A second problem is that it is essentially impossible with Forth to be positive that you have always counted the number of arguments correctly. (Particularly when the receiving function is dynamically computed at runtime.) Therefore you have the ever-present possibility that the wrong argument will apply to the wrong function and everything will just fall apart on you. My understanding (second-hand, but from a Forth fan) is that most non-trivial Forth programs have bugs of this form in them. If you expect your program to deal with malicious data (like, say, web programs do), security flaws like this are a problem.

A third problem is that Forth has a definite attitude about how programs will be structured. However some problems lend themselves more naturally to being structured in different ways than that. A more flexible language solves that.

I know that these won't convince you, but they convince enough people that Forth will never conquer all aspects of computing.

Cheers,
Ben
"good ideas and bad code build communities, the other three combinations do not"
- [link|http://archives.real-time.com/pipermail/cocoon-devel/2000-October/003023.html|Stefano Mazzocchi]
New Re: Why isn't Forth ideal?
Oh I wasn't advocating it as a replacement for say Perl. I was just pointing out that here is an idiom with very sophisticated behavior that is fundamentally very simple, about as simple as possible. I don't understand why these issues become so entrenched. TB said here are these people bitching about performance and so compromising the entire structure of the thing they are developing to get it, when instead they could just decide to do things more efficiently on a lower level - but that's too bitter to contemplate.

Doesn't a point ever come when people say - "OK this entire approach is a disaster."
-drl
New Yes, but never from those who designed it. ;)
New Bondage & Discipline?


The tree of research must from time to time be refreshed with the blood of bean counters.
     -- Alan Kay
New Yes, it's stupid vacuous X-speak
..but accurate - stay in the idiom and pay attention. The onus on the programmer, not the system.
-drl
New Yes - he no longer "Thinks in Java"
I think he's a Python convert.

[link|http://www.artima.com/intv/tipping.html|http://www.artima.com/intv/tipping.html]


The tree of research must from time to time be refreshed with the blood of bean counters.
     -- Alan Kay
Expand Edited by tuberculosis March 14, 2004, 01:20:52 AM EST
New The real way to do generics would have been ...
to have the VM support the idea. One of the proposals (I think Pizza) did just that. In other words at Runtime the VM would know that this List is a List<String>. It would have required VM changes and bytecode changes which are anathema in the Java community (backwards compatability is all well and good but Java developers are paying for the stupidity of Gosling and Co. since 1996 with some of their choices). With the current implementation (and using reflection as well believe) the generic type is wiped out at Runtime. In other words at runtime a List will always be just a List even if you declared it as a List<String> because what the compiler does is type erasure and inserting casts. While this may not seem so bad, it means that it will be difficult to write tools that work well with generics, because many tools use reflection to figure out the type of an Object etc.

The 1.5 autoboxing implementation is also problematic. You can get a null pointer exception when trying to get an int, the following code will throw a null pointer exception:
Map<Integer> map=new HashMap<Integer>();
int i=map.get(3); // raise a NPE

the problem is that since an int is not a reference type but a primitive type you don't expect a null pointer exception. This just exposes the fundamental problem in Java regarding primitive types. Again instead of fixing the language they added another kludge on top to try to solve the problem.
     Bruce Eckel on Java's generics - (ben_tilly) - (14)
         ObAPL plug - (deSitter)
         I enjoyed the follow-up more :) - (FuManChu)
         They're mostly about eliminating casting in collections - (tuberculosis) - (11)
             as a non jhead or any recent programmer - (boxley) - (1)
                 Sure - Ever tried BeanShell? - (tuberculosis)
             That was pretty much Bruce Eckel's point of view - (ben_tilly) - (7)
                 B&D is simpler - why complicate? - (deSitter) - (5)
                     Why isn't Forth ideal? - (ben_tilly) - (2)
                         Re: Why isn't Forth ideal? - (deSitter) - (1)
                             Yes, but never from those who designed it. ;) -NT - (FuManChu)
                     Bondage & Discipline? -NT - (tuberculosis) - (1)
                         Yes, it's stupid vacuous X-speak - (deSitter)
                 Yes - he no longer "Thinks in Java" - (tuberculosis)
             The real way to do generics would have been ... - (bluke)

HA! THAT DON'T PAY THE RENT!
93 ms