| From | Sent On | Attachments |
|---|---|---|
| Randall R Schulz | Dec 3, 2007 3:41 pm | |
| Stand Trooper | Dec 3, 2007 4:03 pm | |
| Randall R Schulz | Dec 3, 2007 4:07 pm | |
| Stand Trooper | Dec 3, 2007 4:34 pm | |
| Russel Winder | Dec 3, 2007 9:46 pm | |
| Russel Winder | Dec 3, 2007 9:49 pm | |
| Charles Oliver Nutter | Dec 3, 2007 9:52 pm | |
| Randall R Schulz | Dec 3, 2007 10:10 pm | |
| Jochen Theodorou | Dec 4, 2007 5:59 am | |
| Charles Oliver Nutter | Dec 4, 2007 10:59 am | |
| Jochen Theodorou | Dec 4, 2007 11:51 am | |
| Smith, Jason, CTR, OASD(HA)/TMA | Dec 4, 2007 1:10 pm | |
| Charles Oliver Nutter | Dec 4, 2007 1:24 pm | |
| Charles Oliver Nutter | Dec 4, 2007 1:29 pm | |
| Jochen Theodorou | Dec 4, 2007 1:54 pm | |
| Smith, Jason, CTR, OASD(HA)/TMA | Dec 4, 2007 2:33 pm | |
| Jochen Theodorou | Dec 4, 2007 2:44 pm | |
| Charles Oliver Nutter | Dec 4, 2007 2:56 pm | |
| Smith, Jason, CTR, OASD(HA)/TMA | Dec 5, 2007 6:35 am | |
| Charles Oliver Nutter | Dec 5, 2007 10:16 am | |
| Smith, Jason, CTR, OASD(HA)/TMA | Dec 5, 2007 10:29 am | |
| Jochen Theodorou | Dec 5, 2007 5:45 pm | |
| Jochen Theodorou | Dec 7, 2007 8:31 am | |
| Randall R Schulz | Dec 7, 2007 8:37 am | |
| Jochen Theodorou | Dec 7, 2007 8:49 am | |
| Guillaume Laforge | Dec 7, 2007 9:01 am | |
| Jochen Theodorou | Dec 7, 2007 9:14 am | |
| Guillaume Laforge | Dec 7, 2007 9:16 am | |
| Randall R Schulz | Dec 7, 2007 9:22 am | |
| Guillaume Laforge | Dec 7, 2007 9:28 am | |
| Charles Oliver Nutter | Dec 7, 2007 10:09 am | |
| Charles Oliver Nutter | Dec 7, 2007 10:11 am | |
| Randall R Schulz | Dec 7, 2007 10:18 am | |
| Jochen Theodorou | Dec 7, 2007 10:31 am | |
| Charles Oliver Nutter | Dec 7, 2007 12:23 pm | |
| Randall R Schulz | Dec 7, 2007 12:37 pm | |
| Charles Oliver Nutter | Dec 7, 2007 1:35 pm | |
| Randall R Schulz | Dec 7, 2007 1:40 pm | |
| Gavin Grover | Dec 7, 2007 11:49 pm | |
| Jochen Theodorou | Dec 8, 2007 3:40 am |
| Subject: | Re: [groovy-user] Acceptable Placement of default: Within Switch? | |
|---|---|---|
| From: | Charles Oliver Nutter (char...@sun.com) | |
| Date: | Dec 4, 2007 2:56:39 pm | |
| List: | org.codehaus.groovy.user | |
Jochen Theodorou wrote:
Charles Oliver Nutter schrieb:
why can't you do some optimizations when they're valid and not when not?
what means valid? If I add so much optimizations that the compiler will run ten times slower then it is still valid, but not desired. For example I could add dynamic branch prediction to my if-else-cascade, that is possibly much faster if the values do not change too much. Hotspot could do something like that for example, not sure if it does.
I doubt it would add much overhead to the compiler; you'd just be checking if all cases are a literal type and whether there's enough of them for it to be a valid optimization. If not, you don't do it. Unless there are hundreds of switches being used in the system, you'd probably never see any impact. Don't most people precompile Groovy anyway? It would be much more of a concern in JRuby, where nobody precompiles anything and we compile at runtime on-demand for all applications. And even there I'm not worried about it at all.
For what it's worth, compilation time in JRuby pales in comparison to class loading and verification time.
The best way to get performance to improve is going to be mapping things as often as possible directly to their Java equivalents. The more magic you have to use the slower things will run, and optimizing a switch to actually be a switch is a simple optimization to add for cases where it's valid.
Of course it shouldn't take priority over other things that are more generally useful; I never said that, and we've always tried to address general performance issues before specific ones. But this thread is about switches :)
I don't understand why you'd only compile it one way--the slowest possible way--when there's a valid alternative for many constant cases. Even Java compiles switches two different ways depending on case density.
I know, why do you think I was talking about tableswitch and lookupswitch? But just because there is bytecode for this, does not mean it was good to do so. Let me just name the JSR instruction as an example. It just looks good on the paper, that's all.
And when you say the slowest way... That is not valid fact unless you provide a testcase showing this. and the only thing I can imagine this could be important (and microbenchamrks do not count here) is a lexer as I already mentioned. But you would write a lexer in Groovy different from Java... for example you would have this in java:
You're thinking about this like a language implementer; there are many cases where you'd want to be able to do a fast switch on literal values beyond writing a lexer. Hell, just for general flow control in an application I've moved a series of ifs into a single switch in Java code many times, because it was worth doing so. I'm just saying you could easily make the compiler do this for cases where it's valid. And again, this doesn't have to take priority over other optimizations...but it's a perfectly valid optimization. Don't discount it out of hand.
that makes 27 switch constructions with an average of 11 cases. Which means we are far away form hundreds of cases for a switch. And if I replace some of these with ranges, then I get an average of 4 cases for my switch. the really nasty cases with 87 and 64 cases can be replaced, so the maximum number is reduced to 30.
The fact that most switches are around 11 in the lexer is meaningless if the one you need to perform well runs slowly. I know I've seen some large switches in both Ruby and Java codebases, and I know many of those could or would benefit from a jump table.
I really think making our dynamic method calls faster is of a much higher priority than this. This doesn't mean we won't optimize this some time in the future, but atm there is no point in doing so.
There's no point in doing any optimizations if nobody's complaining about performance. But if you're going to start adding optimizations, this would be a valid optimization to add. Again, just because you don't think it would be useful doesn't mean it's not useful. And I honestly don't think it's that hard to do.
- Charlie





