case strings

R

Roedy Green

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
..
 
D

Daniel Pitts

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.
 
M

markspace

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.
 
R

Roedy Green

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
..
 
R

Roedy Green

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
..
 
L

Lew

Roedy said:
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.
 
L

Lew

Joshua said:
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.
 

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

Similar Threads

JDK 1.7.0_05 is out 1
classpath puzzle 7
font variants 1
string case clauses 7
Case insensitve Comparator 3
case sensitive filenames 62
case sensitive file names 14
constructing a constant HashMap 19

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top