You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/02/02 13:19:54 UTC

[GitHub] [spark] LuciferYang edited a comment on pull request #35350: [SPARK-38052][SQL] Refactor UnsafeRow#isFixedLength and UnsafeRow#isMutable use looping

LuciferYang edited a comment on pull request #35350:
URL: https://github.com/apache/spark/pull/35350#issuecomment-1027925630


   The following two methods are simulated and tested with nested objects:
   
   I do a test for the following 2 methods:
   
   ```java 
   // use loop
   public static boolean isNumber1(Object o) {
           while (true) {
               if (o instanceof NestedObject) {
                   o = ((NestedObject) o).innerValue();
                   continue;
               }
   
               return o instanceof Number;
           }
       }
   
      // use recursion
       public static boolean isNumber2(Object o) {
           if(o instanceof NestedObject) {
               return isNumber2(((NestedObject) o).innerValue());
           }
           return o instanceof Number;
       }
   ```
   
   The test code as follows:
   
   ``` scala
   object LoopAndRecursionBenchmark extends BenchmarkBase {
   
     override def runBenchmarkSuite(mainArgs: Array[String]): Unit = {
       val valuesPerIteration = 10000;
       doTest(valuesPerIteration, create1LayerNestedObject(), 1)
       doTest(valuesPerIteration, create3LayerNestedObject(), 3)
       doTest(valuesPerIteration, create5LayerNestedObject(), 5)
      ...
         doTest(valuesPerIteration, create19LayerNestedObject(), 19)
     }
   
     def doTest(
         valuesPerIteration: Int, obj: NestedObject, layer: Int): Unit = {
   
       val benchmark = new Benchmark(
         s"Test loop and Recursion $layer",
         valuesPerIteration,
         output = output)
   
       benchmark.addCase("Use loop") { _: Int =>
         for (_ <- 0L until valuesPerIteration) {
           TestLoopUtils.isNumber1(obj)
         }
       }
   
       benchmark.addCase("Use Recursion") { _: Int =>
         for (_ <- 0L until valuesPerIteration) {
           TestLoopUtils.isNumber2(obj)
         }
       }
       benchmark.run()
     }
   
     def create1LayerNestedObject(): NestedObject = {
       new NestedObject(1)
     }
   
     def create3LayerNestedObject(): NestedObject = {
       new NestedObject(new NestedObject(new NestedObject(1)))
     }
   
     def create5LayerNestedObject(): NestedObject = {
       new NestedObject(new NestedObject(create3LayerNestedObject()))
     }
     ....
    def create19LayerNestedObject(): NestedObject = {
       new NestedObject(new NestedObject(create17LayerNestedObject()))
     }
   }
   
   class NestedObject(inner: Any) {
     def innerValue(): Any = inner
   }
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org