如何在Clojure数组处理中摆脱clojure/lang/RT.aset和clojure/lang/RT.intCast?

3
我尝试在Clojure中尽可能快地对复数数组进行乘法运算。所选数据结构是两个元素的映射,:re:im,每个元素都是基本的Java本机双精度数组,以实现低内存开销。根据http://clojure.org/reference/java_interop,我使用了基本类型数组的确切类型规范。有了这些提示,aget被转换为本机数组dload操作,但存在两个效率问题,即循环计数器不是int而是long,因此每次索引数组时,计数器都会使用调用clojure/lang/RT.intCast将其转换为int。而aset也没有转换为本机操作,而是转换为调用clojure/lang/RT.aset。还有一个效率问题是checkcast。它检查每个循环,以确保数组实际上是双精度数组。
这段Clojure代码的运行时间比等效的Java代码多30%(不包括启动时间)。这个函数能否重写为Clojure,以使其更快?Clojure代码中需要优化的函数是“multiply-complex-arrays”。
(def size 65536)

(defn get-zero-complex-array
    []
    {:re (double-array size)
     :im (double-array size)})

(defn multiply-complex-arrays
    [a b]
    (let [
        a-re-array (doubles (get a :re))
        a-im-array (doubles (get a :im))
        b-re-array (doubles (get b :re))
        b-im-array (doubles (get b :im))
        res-re-array (double-array size)
        res-im-array (double-array size)
        ]
        (loop [i (int 0) size (int size)]
            (if (< i size)
                (let [
                    a-re (aget a-re-array i)
                    a-im (aget a-im-array i)
                    b-re (aget b-re-array i)
                    b-im (aget b-im-array i)
                    ]
                    (aset res-re-array i (- (* a-re b-re) (* a-im b-im)))
                    (aset res-im-array i (+ (* a-re b-im) (* b-re a-im)))
                    (recur (unchecked-inc i) size))
                {:re res-re-array :im res-im-array}))))

(let [
    res (loop [i (int 0) a (get-zero-complex-array)]
            (if (< i 30000)
                (recur (inc i) (multiply-complex-arrays a a))
                a))
    ]
    (println (aget (get res :re) 0)))

multiply-complex-arrays 的主循环生成的 Java 汇编代码如下:

  91: lload         8
  93: lload         10
  95: lcmp
  96: ifge          216
  99: aload_2
 100: checkcast     #51                 // class "[D"
 103: lload         8
 105: invokestatic  #46                 // Method clojure/lang/RT.intCast:(J)I
 108: daload
 109: dstore        12
 111: aload_3
 112: checkcast     #51                 // class "[D"
 115: lload         8
 117: invokestatic  #46                 // Method clojure/lang/RT.intCast:(J)I
 120: daload
 121: dstore        14
 123: aload         4
 125: checkcast     #51                 // class "[D"
 128: lload         8
 130: invokestatic  #46                 // Method clojure/lang/RT.intCast:(J)I
 133: daload
 134: dstore        16
 136: aload         5
 138: checkcast     #51                 // class "[D"
 141: lload         8
 143: invokestatic  #46                 // Method clojure/lang/RT.intCast:(J)I
 146: daload
 147: dstore        18
 149: aload         6
 151: checkcast     #51                 // class "[D"
 154: lload         8
 156: invokestatic  #46                 // Method clojure/lang/RT.intCast:(J)I
 159: dload         12
 161: dload         16
 163: dmul
 164: dload         14
 166: dload         18
 168: dmul
 169: dsub
 170: invokestatic  #55                 // Method clojure/lang/RT.aset:([DID)D
 173: pop2
 174: aload         7
 176: checkcast     #51                 // class "[D"
 179: lload         8
 181: invokestatic  #46                 // Method clojure/lang/RT.intCast:(J)I
 184: dload         12
 186: dload         18
 188: dmul
 189: dload         16
 191: dload         14
 193: dmul
 194: dadd
 195: invokestatic  #55                 // Method clojure/lang/RT.aset:([DID)D
 198: pop2
 199: lload         8
 201: lconst_1
 202: ladd
 203: lload         10
 205: lstore        10
 207: lstore        8
 209: goto          91

Java 代码:

class ComplexArray {

    static final int SIZE = 1 << 16;

    double re[];

    double im[];

    ComplexArray(double re[], double im[]) {
        this.re = re;
        this.im = im;
    }

    static ComplexArray getZero() {
        return new ComplexArray(new double[SIZE], new double[SIZE]);
    }

    ComplexArray multiply(ComplexArray second) {
        double resultRe[] = new double[SIZE];
        double resultIm[] = new double[SIZE];
        for (int i = 0; i < SIZE; i++) {
            double aRe = this.re[i];
            double aIm = this.im[i];
            double bRe = second.re[i];
            double bIm = second.im[i];
            resultRe[i] = aRe * bRe - aIm * bIm;
            resultIm[i] = aRe * bIm + bRe * aIm;
        }
        return new ComplexArray(resultRe, resultIm);
    }

    public static void main(String args[]) {
        ComplexArray a = getZero();
        for (int i = 0; i < 30000; i++) {
            a = a.multiply(a);
        }
        System.out.println(a.re[0]);
    }
}

在Java代码中组装相同的循环:

  13: iload         4
  15: ldc           #5                  // int 65536
  17: if_icmpge     92
  20: aload_0
  21: getfield      #2                  // Field re:[D
  24: iload         4
  26: daload
  27: dstore        5
  29: aload_0
  30: getfield      #3                  // Field im:[D
  33: iload         4
  35: daload
  36: dstore        7
  38: aload_1
  39: getfield      #2                  // Field re:[D
  42: iload         4
  44: daload
  45: dstore        9
  47: aload_1
  48: getfield      #3                  // Field im:[D
  51: iload         4
  53: daload
  54: dstore        11
  56: aload_2
  57: iload         4
  59: dload         5
  61: dload         9
  63: dmul
  64: dload         7
  66: dload         11
  68: dmul
  69: dsub
  70: dastore
  71: aload_3
  72: iload         4
  74: dload         5
  76: dload         11
  78: dmul
  79: dload         9
  81: dload         7
  83: dmul
  84: dadd
  85: dastore
  86: iinc          4, 1
  89: goto          13

为什么不直接使用Clojure中的Java实现呢? - OlegTheCat
@OlegTheCat有趣的引用语是来自http://clojure.org/reference/java_interop的“生成的代码速度完全相同”。 我想知道这个例子是否是规则(并且Clojure中的数组处理可以高效)还是一个例外。 - Fyodor Menshikov
@FyodorMenshikov 你能详细说明一下你在问题中提到的获取汇编代码的具体步骤吗? - Sam Estep
1
@SamEstep 获取汇编的具体步骤如下:1. lein new app tmp 2. 编辑 tmp/src/tmp/code.clj - 将 def sizedefn get-zero-complex-arraydefn multiply-complex-arrays 放置在 (:gen-class))(defn -main 之间。3. 运行 lein uberjar。4. 从 target/uberjar/tmp-0.1.0-SNAPSHOT.jar 中提取 tmp/core$multiply_complex_arrays.class。5. 执行 javap -p -c core$multiply_complex_arrays >src - Fyodor Menshikov
4
(set! *unchecked-math* true)这行代码将会把intCast的调用转换成l2i指令。 - sw1nn
显示剩余6条评论
1个回答

2
你是如何对这段代码进行基准测试的?我建议使用类似criterium的工具或至少在比较时间之前进行多次执行。像checkcast这样的操作应该会在JIT热身后被优化掉。我还建议使用最新的JVM,-server和-XX:+AggressiveOpts。
一般来说,我认为最好不要强制Clojure在循环中使用int - 相反,接受long作为循环计数器,使用(set! *unchecked-math* true),并让Clojure将longs向下转换为ints以索引数组。虽然这看起来像是额外的工作,但我对现代硬件/JVM/JIT的印象是,差异比你预期的要小得多(因为你主要使用64位整数)。此外,看起来你正在将size作为循环变量,但它从未改变 - 也许你这样做是为了避免与i类型不匹配,但我会在循环之前将size(作为long)设定好,并对i进行长增量和比较。
有时可以通过在循环之前let某些变量来减少checkcast的数量。虽然很容易用肉眼判断代码何时不需要它们,但编译器实际上并没有对此进行任何分析,而是将其留给JIT来优化(它通常非常擅长,或者在你的代码中根本不重要)。
(set! *unchecked-math* :warn-on-boxed)

(def ^long ^:const size 65536)

(defn get-zero-complex-array []
  {:re (double-array size)
   :im (double-array size)})

(defn multiply-complex-arrays [a b]
  (let [a-re-array (doubles (get a :re))
        a-im-array (doubles (get a :im))
        b-re-array (doubles (get b :re))
        b-im-array (doubles (get b :im))
        res-re-array (double-array size)
        res-im-array (double-array size)
        s (long size)]
    (loop [i 0]
      (if (< i s)
        (let [a-re (aget a-re-array i)
              a-im (aget a-im-array i)
              b-re (aget b-re-array i)
              b-im (aget b-im-array i)]
          (aset res-re-array i (- (* a-re b-re) (* a-im b-im)))
          (aset res-im-array i (+ (* a-re b-im) (* b-re a-im)))
          (recur (inc i)))
        {:re res-re-array :im res-im-array}))))

(defn compute []
  (let [res (loop [i 0 a (get-zero-complex-array)]
              (if (< i 30000)
                (recur (inc i) (multiply-complex-arrays a a))
                a))]
    (aget (get res :re) 0)))

在使用了*unchecked-math*后,时间从1.34 Java变为了1.22 Java,在将size从循环中移出并递归到let中(比您建议的在双数组中使用高2行),时间缩短到1.17 Java,并使用^long ^:const和类型为longi,时间缩短到1.15 Java。Alex,感谢您宝贵的建议。 - Fyodor Menshikov
我使用Windows下的time程序输出来测量墙上时间。我运行了5次并得到中位数。为了获得Java时间的倍数,我从Java和Clojure中执行30000个循环所需的时间以及执行1个循环所需的时间中减去时间,然后将Clojure差异除以Java差异。 - Fyodor Menshikov
这个问题与竞赛编程有关,所以代码没有超过10秒的运行时间,因此我在热身几分钟后就不再进行测量。 - Fyodor Menshikov
(set! *unchecked-math* :warn-on-boxed) 是一个全局设置吗?如何将其应用于一个函数甚至只是一个循环? - Fyodor Menshikov
1
*unchecked-math* 是一个动态变量,因此它既有根绑定(false),也可以在每个线程中设置(这里)。除非你创建一个在函数周围设置和重置值的宏,否则它不能应用于单个函数或循环。 - Alex Miller
如果Clojure不解释代码而是将其编译成字节码,那么如何在每个线程上设置 *unchecked-math* ? 在函数的字节码中,检查要么存在,要么不存在。 它看起来更像是C ++中的编译器预处理指令,因此其范围可能应具有静态性质。 是否有关于 *unchecked-math* 的详细文档或教程? 可以在不使用 "set!" 的情况下将 *unchecked-math* 应用于特定的aget和aset吗? - Fyodor Menshikov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接