Pig
  1. Pig
  2. PIG-2835

Optimizing the convertion from bytes to Integer/Long

    Details

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

      Description

      Currently Pig doesn't support lazy see/de, so as one of the best practices, we recommend users not to declare types in the schema so that Pig will guess the right types and cast them lazily. However, if Pig guesses a wrong type, especially mistakes a double field as an integer field, the overhead of casting is tremendous due to the exception handling.

      See Utf8StorageConverter#bytesToIntege. It first casts bytes to Integer by Integer.parseInt(), and if exception occurs, it tries to cast it to Double by Double.parseDouble() and convert it back to Integer. The problem is that the exception handling can be 10x slower than the actual casting. bytesToLong has the same problem. Below is a mini-benchmark:

              
              int i;
              Exception ex = null;
              long start = System.nanoTime();
              for (i = 0; i < 100000000; i++) {
                  try {
                      // Double.parseDouble(i+ ".0");
                      // Integer.parseInt(i + ".0");
                      Integer.parseInt(i + "");
                      // Double.parseDouble(i + "");
                  } catch (NumberFormatException e) {
                      ex = e;
                  }
              }
              System.out.println("time: " + (System.nanoTime() - start)
                      / 1000000000.0);
              if (ex != null) {
                  ex.printStackTrace();
              }
      

      And the results:

      casting running time(sec)
      Double.parseDouble(i+ ".0"); 17
      Integer.parseInt(i + ".0"); 118
      Integer.parseInt(i + ""); 13
      Double.parseDouble(i + ""); 16

      We can see Integer.parseInt(i + ".0") is 10x slower than the other due to the exception handling.

      This issue was found when I benchmark TPC-H Query 1, for which Pig was terribly slower than Hive:

      LineItems = LOAD '$input/lineitem' USING PigStorage('|') AS (orderkey, partkey, suppkey, linenumber, quantity, extendedprice, discount, tax, returnflag, linestatus, shipdate, commitdate, receiptdate, shipinstruct, shipmode, comment);
      SubLineItems = FILTER LineItems BY shipdate <= '1998-09-02';
      
      SubLine = FOREACH SubLineItems GENERATE returnflag, linestatus, quantity, extendedprice, extendedprice*(1-discount) AS disc_price, extendedprice*(1-discount)*(1+tax) AS charge, discount;
      
      StatusGroup = GROUP SubLine BY (returnflag, linestatus);
      
      PriceSummary = FOREACH StatusGroup GENERATE group.returnflag AS returnflag, group.linestatus AS linestatus, SUM(SubLine.quantity) AS sum_qty, SUM(SubLine.extendedprice) AS sum_base_price, SUM(SubLine.disc_price) as sum_disc_price, SUM(SubLine.charge) as sum_charge, AVG(SubLine.quantity) as avg_qty, AVG(SubLine.extendedprice) as avg_price, AVG(SubLine.discount) as avg_disc, COUNT(SubLine) as count_order;
      
      SortedSummary = ORDER PriceSummary BY returnflag, linestatus;
      
      STORE SortedSummary INTO '$output/Q1out';
      

      After declaring three double fields as double, the performance was boosted.

      pig without types pig with three doubles hive
      76 min 34 min 16 min

      Besides recommending users to declare actual double fields as double, we can also improve the casting to avoid this happening. Maybe the easiest way is to remove the Integer.parseInt and only use the Double.parseDouble and convert back to Integer. The mini benchmark above shows Double.parseDouble + range checking + Integer.valueOf(Double.intValue()) takes about 17 seconds. I think the small percent of extra overhead (3 seconds compared to Integer.parseInt()) is acceptable as it won't be the dominant bottleneck?

        Issue Links

          Activity

          Hide
          Jie Li added a comment -

          Always casting bytes to double doesn't work with Long, as Long might lose accuracy when represented as Double.

          Attached a patch that includes a sanity check for whether the bytes are integer/long. It passes when all characters are '0''9' or leading '', otherwise the check fails and we skip the conversion from bytes to integer/long and directly cast it as double.

          This sanity check will optimize the cases when Pig guesses the double field as int/long by eliminating the exception handling. The TPC-H Q1 shows it achieves the same performance as explicitly declaring double fields as double.

          Show
          Jie Li added a comment - Always casting bytes to double doesn't work with Long, as Long might lose accuracy when represented as Double. Attached a patch that includes a sanity check for whether the bytes are integer/long. It passes when all characters are '0' '9' or leading ' ', otherwise the check fails and we skip the conversion from bytes to integer/long and directly cast it as double. This sanity check will optimize the cases when Pig guesses the double field as int/long by eliminating the exception handling. The TPC-H Q1 shows it achieves the same performance as explicitly declaring double fields as double.
          Hide
          Jie Li added a comment -

          Oops, happened to use some markup. The sanity check passes when all characters are '0'~'9' or leading minus.

          Show
          Jie Li added a comment - Oops, happened to use some markup. The sanity check passes when all characters are '0'~'9' or leading minus.
          Hide
          Dmitriy V. Ryaboy added a comment -

          +1, applied to trunk. Thanks Jie!

          Show
          Dmitriy V. Ryaboy added a comment - +1, applied to trunk. Thanks Jie!
          Hide
          Jie Li added a comment -

          Thanks Dmitriy for committing this. We had a discussion on this and wondered if Pig can default the numeric type to integer instead of double. With double as the default type, this problem should not occur. Opened a separate jira PIG-2902 just in case we want to come back to it.

          Show
          Jie Li added a comment - Thanks Dmitriy for committing this. We had a discussion on this and wondered if Pig can default the numeric type to integer instead of double. With double as the default type, this problem should not occur. Opened a separate jira PIG-2902 just in case we want to come back to it.

            People

            • Assignee:
              Jie Li
              Reporter:
              Jie Li
            • Votes:
              1 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development