锁定老帖子 主题:Clojure的recur尾递归优化探秘
精华帖 (16) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-07-11
最后修改:2010-07-11
(defn recur-fibo [n]
(letfn [(fib [current next n] (if (zero? n) current ;recur将递归调用fib函数 (recur next (+ current next) (dec n))))] (fib 0 1 n)))
1 // access flags 1
2 public invoke(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; throws java/lang/Exception 3 L0 4 LINENUMBER 2 L0 5 L1 6 LINENUMBER 4 L1 7 L2 8 LINENUMBER 4 L2 9 ALOAD 3 10 INVOKESTATIC clojure/lang/Numbers.isZero (Ljava/lang/Object;)Z 11 IFEQ L3 12 ALOAD 1 13 GOTO L4 14 L5 15 POP 16 L3 17 ALOAD 2 18 L6 19 LINENUMBER 6 L6 20 ALOAD 1 21 ALOAD 2 22 INVOKESTATIC clojure/lang/Numbers.add (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Number; 23 L7 24 LINENUMBER 6 L7 25 ALOAD 3 26 INVOKESTATIC clojure/lang/Numbers.dec (Ljava/lang/Object;)Ljava/lang/Number; 27 ASTORE 3 28 ASTORE 2 29 ASTORE 1 30 GOTO L0 31 L4 32 L8 33 LOCALVARIABLE this Ljava/lang/Object; L0 L8 0 34 LOCALVARIABLE current Ljava/lang/Object; L0 L8 1 35 LOCALVARIABLE next Ljava/lang/Object; L0 L8 2 36 LOCALVARIABLE n Ljava/lang/Object; L0 L8 3 37 ARETURN 38 MAXSTACK = 0 39 MAXLOCALS = 0
//覆盖变量
for (int i = loopLocals.count() - 1; i >= 0; i--) { LocalBinding lb = (LocalBinding) loopLocals.nth(i); Class primc = lb.getPrimitiveType(); if (primc != null) { gen.visitVarInsn(Type.getType(primc).getOpcode(Opcodes.ISTORE), lb.idx); } else { gen.visitVarInsn(OBJECT_TYPE.getOpcode(Opcodes.ISTORE), lb.idx); } } //执行跳转 gen.goTo(loopLabel);
1 L0
2 LINENUMBER 1 L0 3 ACONST_NULL 4 ASTORE 2 5 NEW user$recur_fibo__4346$fib__4348 6 DUP 7 INVOKESPECIAL user$recur_fibo__4346$fib__4348.<init> ()V 8 ASTORE 2 9 ALOAD 2 10 CHECKCAST user$recur_fibo__4346$fib__4348 11 POP 12 L1 13 L2 14 LINENUMBER 7 L2 15 ALOAD 2 16 CHECKCAST clojure/lang/IFn 17 GETSTATIC user$recur_fibo__4346.const__2 : Ljava/lang/Object; 18 GETSTATIC user$recur_fibo__4346.const__3 : Ljava/lang/Object; 19 ALOAD 1 20 ACONST_NULL 21 ASTORE 1 22 ACONST_NULL 23 ASTORE 2 24 INVOKEINTERFACE clojure/lang/IFn.invoke (Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; 25 L3 26 LOCALVARIABLE fib Ljava/lang/Object; L1 L3 2 27 L4 28 LOCALVARIABLE this Ljava/lang/Object; L0 L4 0 29 LOCALVARIABLE n Ljava/lang/Object; L0 L4 1 30 ARETURN
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
浏览 2606 次