Clojure 2024年度调查!中分享您的想法。

欢迎!请查阅关于页面以了解更多此平台的工作方式。

0
编译器

目前,Clojure编译器总是将值发送到set!表达式并转换为boxed形式,如果需要则取消boxed。可以通过将AssignExpr增强为MaybePrimitiveExpr来避免不必要的boxed/unboxed。

假设以下代码

`
public class Test {
public int x;
}

`

`
(defn foo []
(let [a (int 1)]

(set! (.-x (Test.)) a)
"asd"))

`

之前

`

public static java.lang.Object invokeStatic();

descriptor: ()Ljava/lang/Object;
flags: ACC_PUBLIC, ACC_STATIC
Code:
  stack=3, locals=1, args_size=0
     0: lconst_1
     1: invokestatic  #17                 // Method clojure/lang/RT.intCast:(J)I
     4: istore_0
     5: new           #19                 // class Test
     8: dup
     9: invokespecial #20                 // Method Test."<init>":()V
    12: checkcast     #19                 // class Test
    15: iload_0
    16: invokestatic  #26                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
    19: dup_x1
    20: checkcast     #28                 // class java/lang/Number
    23: invokestatic  #31                 // Method clojure/lang/RT.intCast:(Ljava/lang/Object;)I
    26: putfield      #35                 // Field Test.x:I
    29: pop
    30: ldc           #37                 // String asd
    32: areturn
  LocalVariableTable:
    Start  Length  Slot  Name   Signature
        5      27     0     a   I
  LineNumberTable:
    line 3: 0
    line 4: 1
    line 5: 19

`

之后

` public static java.lang.Object invokeStatic();

descriptor: ()Ljava/lang/Object;
flags: ACC_PUBLIC, ACC_STATIC
Code:
  stack=3, locals=1, args_size=0
     0: lconst_1
     1: invokestatic  #17                 // Method clojure/lang/RT.intCast:(J)I
     4: istore_0
     5: new           #19                 // class Test
     8: dup
     9: invokespecial #20                 // Method Test."<init>":()V
    12: checkcast     #19                 // class Test
    15: iload_0
    16: dup_x1
    17: putfield      #24                 // Field Test.x:I
    20: pop
    21: ldc           #26                 // String asd
    23: areturn
  LocalVariableTable:
    Start  Length  Slot  Name   Signature
        5      18     0     a   I
  LineNumberTable:
    line 3: 0
    line 4: 1
    line 5: 16

`

补丁: 0001-CLJ-2282-don't-unnecessarily-box-primitive-set-val.patch

1 答案

0
参考:https://clojure.atlassian.net/browse/CLJ-2282(由bronsa报告)
...