Avro
  1. Avro
  2. AVRO-1074

Suggested optimization for Utf8.length()

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.6.3
    • Fix Version/s: 1.7.0
    • Component/s: java
    • Labels:
      None

      Description

      Wanted to suggest a small optimization. It is quite common for applications to do a quick check (along with null-ness) for 0-length of string-typed fields. Today's Utf8.length() implementation is:

      return toString().length;

      I believe this could easily get optimized to return this instead:

      return length == 0 ? 0 : toString().length();

      Thus avoiding the need to create the string representation for this common use case (and scenario).

      1. AVRO-1074.patch
        0.5 kB
        Doug Cutting

        Activity

        Hide
        wolfgang hoschek added a comment -

        An alternative might be for Utf8.toString() to return the "" String constant if Utf8.length == 0. This avoids having to construct a new String object when empty on Utf8.toString(). It also efficiently addresses the Utf8.length() use case brought up by Hernan, so this version might be preferable overall.

        Show
        wolfgang hoschek added a comment - An alternative might be for Utf8.toString() to return the "" String constant if Utf8.length == 0. This avoids having to construct a new String object when empty on Utf8.toString(). It also efficiently addresses the Utf8.length() use case brought up by Hernan, so this version might be preferable overall.
        Hide
        Hernan Otero added a comment -

        That makes sense.

        Show
        Hernan Otero added a comment - That makes sense.
        Hide
        Doug Cutting added a comment -

        Here's a patch that implements Wolfgang's suggestion. Tests pass.

        Show
        Doug Cutting added a comment - Here's a patch that implements Wolfgang's suggestion. Tests pass.
        Hide
        Doug Cutting added a comment -

        I committed this.

        Show
        Doug Cutting added a comment - I committed this.

          People

          • Assignee:
            Doug Cutting
            Reporter:
            Hernan Otero
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development