case strings

Discussion in 'Java' started by Roedy Green, Jun 3, 2012.

  1. Roedy Green

    Roedy Green Guest

    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
    ..
    Roedy Green, Jun 3, 2012
    #1
    1. Advertising

  2. Roedy Green

    Daniel Pitts Guest

    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.
    Daniel Pitts, Jun 3, 2012
    #2
    1. Advertising

  3. Roedy Green

    markspace Guest

    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.
    markspace, Jun 4, 2012
    #3
  4. Roedy Green

    Roedy Green Guest

    On Sun, 03 Jun 2012 13:37:13 -0700, Daniel Pitts
    <> 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
    ..
    Roedy Green, Jun 4, 2012
    #4
  5. Roedy Green

    Roedy Green Guest

    On Sun, 03 Jun 2012 11:40:03 -0700, Roedy Green
    <> 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
    ..
    Roedy Green, Jun 4, 2012
    #5
  6. "Roedy Green" <> wrote in message
    news:eek:...
    > On Sun, 03 Jun 2012 11:40:03 -0700, Roedy Green
    > <> 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.
    Mike Schilling, Jun 4, 2012
    #6
  7. On 6/4/2012 10:57 AM, Roedy Green wrote:
    > On Sun, 03 Jun 2012 11:40:03 -0700, Roedy Green
    > <> 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
    Joshua Cranmer, Jun 4, 2012
    #7
  8. Roedy Green

    Lew Guest

    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/wikipedia/commons/c/cf/Friz.jpg
    Lew, Jun 6, 2012
    #8
  9. Roedy Green

    Lew Guest

    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/wikipedia/commons/c/cf/Friz.jpg
    Lew, Jun 6, 2012
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Franks
    Replies:
    2
    Views:
    1,247
    Steve Franks
    Jun 10, 2004
  2. Tee
    Replies:
    3
    Views:
    7,793
    Herfried K. Wagner [MVP]
    Jun 23, 2004
  3. Janice

    lower case to upper case

    Janice, Dec 10, 2004, in forum: C Programming
    Replies:
    17
    Views:
    1,172
    Richard Bos
    Dec 14, 2004
  4. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    754
    Malcolm
    Jun 24, 2006
  5. anonym
    Replies:
    1
    Views:
    1,013
    Knute Johnson
    Jan 15, 2009
Loading...

Share This Page