2

我有一个函数应该将 X 舍入到最接近的 A 倍数。

这是 JavaScript 中的代码:

var round = function (x, a) {
  "use strict";

  if (typeof a === "undefined") {
    return Math.round(x);
  }

  var half = a / 2,
      sign = this.sign(x),
      absolute = Math.abs(x),
      factor = absolute / a|0,
      remainder = absolute % a;

  return sign * a * (factor + (remainder < half ? 0 : 1));
};

当我--trace_deopt --code_comments在 Chrome 的 V8 中执行操作时,我在日志中收到此消息:

[deoptimizing (DEOPT eager): begin 0x988464b35b1 round (opt #13) @13, FP to SP delta: 24]
 591             ;;; deoptimize at <0:70499> tagged-to-i: not a heap number
 592   translating round => node=67, height=48
 593     0x7fff571f2ed0: [top + 96] <- 0x11b5fb674cf9 ; rdi 0x11b5fb674cf9 <JS Object>
 594     0x7fff571f2ec8: [top + 88] <- 0x2459b11f1d89 ; r8 0x2459b11f1d89 <Number: 45>
 595     0x7fff571f2ec0: [top + 80] <- 0x2d00000000 ; [sp + 40] 45
 596     0x7fff571f2eb8: [top + 72] <- 0xbef1cdacfe5 ; caller's pc
 597     0x7fff571f2eb0: [top + 64] <- 0x7fff571f3090 ; caller's fp
 598     0x7fff571f2ea8: [top + 56] <- 0x11b5fb674d61 ; [sp + 0] 0x11b5fb674d61 <FixedArray[133]>
 599     0x7fff571f2ea8: [top + 56] <- 0x11b5fb674d61; context
 600     0x7fff571f2ea0: [top + 48] <- 0x988464b35b1; function
 601     0x7fff571f2e98: [top + 40] <- 22 ; rcx (smi)
 602     0x7fff571f2e90: [top + 32] <- 0x34565d404121 <undefined> ; literal
 603     0x7fff571f2e88: [top + 24] <- 0x34565d404121 <undefined> ; literal
 604     0x7fff571f2e80: [top + 16] <- 0x34565d404121 <undefined> ; literal
 605     0x7fff571f2e78: [top + 8] <- 0x34565d404121 <undefined> ; literal
 606     0x7fff571f2e70: [top + 0] <- 1 ; rsi (smi)
 607 [deoptimizing (eager): end 0x988464b35b1 round @13 => node=67, pc=0xbef1cd97be6, state=TOS_REG, alignment=no padding, took 0.084 ms]
 608 [removing optimized code for: round]
 609 [evicting entry from optimizing code map (notify deoptimized) for 0x34565d492be1 <SharedFunctionInfo round>]

有谁知道是什么tagged-to-i: not a heap number意思?

提前致谢!

4

1 回答 1

2

tagged-to-i: not a heap number本质上意味着 V8 正在尝试将不是原始数字的值转换为整数。

最初我认为x翻译框架中的第一个参数 ( ) 是一个Number对象,因为它被打印为<Number: 45>,但事实证明 V8 以HeapNumber这种方式打印对象 [1]。在正常情况下,拆箱堆号的尝试不应导致非堆号去优化。所以它要么是由于不同的变量而导致的 deopt - 要么是 deoptimization 的原因不准确。

;; this
0x7fff571f2ed0: [top + 96] <- 0x11b5fb674cf9 ; rdi 0x11b5fb674cf9 <JS Object>
;; x
0x7fff571f2ec8: [top + 88] <- 0x2459b11f1d89 ; r8 0x2459b11f1d89 <Number: 45>
;; a
0x7fff571f2ec0: [top + 80] <- 0x2d00000000 ; [sp + 40] 45

[1] https://github.com/v8/v8-git-mirror/blob/4f9193e047d50b1ffbca95e8185576af82c722b3/src/objects.cc#L1546-L1551

于 2014-12-23T09:09:43.763 回答