Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > case strings

Reply
Thread Tools

case strings

 
 
Roedy Green
Guest
Posts: n/a
 
      06-03-2012
has anyone benchmarked or decompiled to see how the new case string
labels are compiled. Are they faster than setting up a HashMap to
classify the strings? In theory they could be since the literal
values are known at compile time, where HashMap does not have that
advantage.

--
Roedy Green Canadian Mind Products
http://mindprod.com
Controlling complexity is the essence of computer programming.
~ Brian W. Kernighan 1942-01-01
..
 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      06-03-2012
On 6/3/12 11:40 AM, Roedy Green wrote:
> has anyone benchmarked or decompiled to see how the new case string
> labels are compiled. Are they faster than setting up a HashMap to
> classify the strings? In theory they could be since the literal
> values are known at compile time, where HashMap does not have that
> advantage.
>

It took me a second to realize you meant switch/case with Strings. I
thought you were talking about letter case and case sensitivity, and the
rest became a non sequitur.

I haven't looked into the mechanisms specifically, but I would imaging
there could be significant optimizations that could be done at compile
time (java->bytecode or bytecode->native), depending on size of the
switch statement.

On the other hand, a Map has other advantages (like being able to be
dynamically defined). Use what ever is clearest for your use-case, then
if, and only if, it isn't fast enough (as indicated by profiling tools),
optimize it.
 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      06-04-2012
On 6/3/2012 1:37 PM, Daniel Pitts wrote:
> It took me a second to realize you meant switch/case with Strings. I
> thought you were talking about letter case and case sensitivity, and the
> rest became a non sequitur.
>
> I haven't looked into the mechanisms specifically, but I would imaging



Thanks for translating from Roedyese into English. I was having no luck
at all with it.

Iirc, the strings in a case statement are hashed. I seem to recall the
implementer posting about how it worked, and he used a perfect hash for
the strings, so it should be faster (slightly) than a regular hash map.

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-04-2012
On Sun, 03 Jun 2012 13:37:13 -0700, Daniel Pitts
<(E-Mail Removed)> wrote, quoted or indirectly
quoted someone who said :

>
>On the other hand, a Map has other advantages (like being able to be
>dynamically defined). Use what ever is clearest for your use-case, then
>if, and only if, it isn't fast enough (as indicated by profiling tools),
>optimize it.


The primary motive for such questions is curiosity. Secondarily I want
to know which to use by default when there is no pressing high level
need for either.

I got immense joy looking as some of the assembler code that Jet
generated, realising is many cases it was faster than I would ever
have the patience to create, taking pipelines into consideration.

If I were to learn string labels were handled it some clever way it
would give me great pleasure.

It would be sort of like everyone being issued a Ferrari powered
electrically.
--
Roedy Green Canadian Mind Products
http://mindprod.com
Controlling complexity is the essence of computer programming.
~ Brian W. Kernighan 1942-01-01
..
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-04-2012
On Sun, 03 Jun 2012 11:40:03 -0700, Roedy Green
<(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :

>has anyone benchmarked or decompiled to see how the new case string
>labels are compiled.


I gather this was the confusing sentence. What should I have called
the feature?

What the desperation of trying to deduce how it worked from benchmark
timings what threw you off?
--
Roedy Green Canadian Mind Products
http://mindprod.com
Controlling complexity is the essence of computer programming.
~ Brian W. Kernighan 1942-01-01
..
 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      06-04-2012

"Roedy Green" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> On Sun, 03 Jun 2012 11:40:03 -0700, Roedy Green
> <(E-Mail Removed)> wrote, quoted or indirectly quoted
> someone who said :
>
>>has anyone benchmarked or decompiled to see how the new case string
>>labels are compiled.

>
> I gather this was the confusing sentence. What should I have called
> the feature?


Say, string-valued case labels.


 
Reply With Quote
 
Joshua Cranmer
Guest
Posts: n/a
 
      06-04-2012
On 6/4/2012 10:57 AM, Roedy Green wrote:
> On Sun, 03 Jun 2012 11:40:03 -0700, Roedy Green
> <(E-Mail Removed)> wrote, quoted or indirectly quoted
> someone who said :
>
>> has anyone benchmarked or decompiled to see how the new case string
>> labels are compiled.

>
> I gather this was the confusing sentence. What should I have called
> the feature?


Using switch with Strings?


--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      06-06-2012
Roedy Green wrote:
> If I were to learn string labels were handled it some clever way it
> would give me great pleasure.



Joseph D. Darcy's Oracle Weblog
<https://blogs.oracle.com/darcy/entry/project_coin_string_switch_anatomy>
"... com.sun.tools.javac.comp.Lower, the compiler phase which translates away
syntactic sugar, lowering structures to trees implementing the formerly
sugar-coated functionality. For example, Lower already had a method to
translate enum switches into a switch on an integer value retrieved from an
enum → int map. The initial strings in switch implementation uses a similar
technique: a single string switch in source code is lowered into a series of
two switches. The first switch is a new synthesized switch on the string's
hash code, which gets mapped to a label's ordinal position on the list of case
statements. The second switch is structurally identical to the original string
switch from the source except that the string case labels are replaced by
integer positions and the computed position from the synthesized switch is the
expression being switched on."

and about the compliance tests:

"The new tests verify that the proper structural checks are enforced for
string switches as well as verify that the proper execution paths are taken on
different inputs for switches with a variety of control flow shapes, including
multiple case labels, case labels with colliding hash codes, and nested switches."

N.b., this article links to
<http://blogs.sun.com/darcy/entry/how_to_cross_compile_for>
"... make sure to set the bootclasspath!"

Thanks for the chance to flex my google-fu.

> It would be sort of like everyone being issued a Ferrari powered
> electrically.


That would be a Tesla.

--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedi.../c/cf/Friz.jpg
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      06-06-2012
Joshua Cranmer wrote:
> Roedy Green wrote:
>>> has anyone benchmarked or decompiled to see how the new case string
>>> labels are compiled.

>>
>> I gather this was the confusing sentence. What should I have called
>> the feature?

>
> Using switch with Strings?


Strings in switch is the Project Coin name.

Also, string case labels.

The JLS calls them "switch labels", "case constants" and "case
labels".(§14.11) (Case constants actually are only the constants within the
case label, but that still pertains to Roedy's question.)

So do the programmers who implemented these constructs for Java.

But I had no trouble understanding that Roedy meant "string case labels" for
"case string labels". The mere word transposition seemed like a typo.

--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedi.../c/cf/Friz.jpg
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to replace all strings matching a pattern with correspondinglower case strings ? anonym Java 1 01-15-2009 07:29 PM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
how to case select with case-insensitive string ? Tee ASP .Net 3 06-23-2004 07:40 PM
Possible to turn on/off cookieless sessions dynamically on a case by case basis at run-time? Steve Franks ASP .Net 2 06-10-2004 02:04 PM
Scorsese Collection: Keep case vs Snap case Ray DVD Video 0 05-30-2004 04:04 AM



Advertisments