堆栈溢出使用hhvm的benchmarkgame适应严格的哈克


Stack overflow using hhvm for benchmarkgame adapted to strict Hack

由于Hack在严格模式下更快,我试图将第一个benchmarkgame翻译成dito,但遇到了在非严格版本中从未发生过的堆栈溢出问题(与使用对象的递归调用不同?)。为什么?如何增加堆栈大小?谷歌搜索"php堆栈溢出"是没有用的,你可以想象。^^

问题发生在第一个递归调用上。

我的代码:

<?hh // strict
class Tree {
  public function bottomUpTree(?num $item, ?num $depth) : array<?num>
  {
    if ($depth === null) return array(null,null,$item);
    if ($item !== null) {
      $item2 = $item + $item;
      $depth--;
      return array( // <--- Stack overflow here
        $this->bottomUpTree($item2-1,$depth),
        $this->bottomUpTree($item2,$depth),
        $item);
    }
    else {
      throw new Exception("Fatal");
    }
  }
  public function itemCheck(array<?int, ?int> $treeNode) : int {
    if ($treeNode !== null && $treeNode[2] !== null) {
      $someNumber = $treeNode[2];
      $a = $someNumber + 10;
      return $someNumber
        + ($treeNode[0][0] === null ? $this->itemCheck($treeNode[0]) : $treeNode[0][2])
        - ($treeNode[1][0] === null ? $this->itemCheck($treeNode[1]) : $treeNode[1][2]);
    }
    else {
      throw new Exception("Fatal");
    }
  }
  public function run($argc, $argv) {
    $minDepth = 4;
    $n = ($argc == 2) ? $argv[1] : 1;
    $maxDepth = max($minDepth + 2, $n);
    $stretchDepth = $maxDepth + 1;
    $stretchTree = $this->bottomUpTree(0, $stretchDepth);
    printf("stretch tree of depth %d't check: %d'n",
      $stretchDepth, $this->itemCheck($stretchTree));
    unset($stretchTree);
    $longLivedTree = $this->bottomUpTree(0, $maxDepth);
    $iterations = 1 << ($maxDepth);
    do {
      $check = 0;
      for($i = 1; $i <= $iterations; ++$i)
      {
        $t = $this->bottomUpTree($i, $minDepth);
        $check += $this->itemCheck($t);
        unset($t);
        $t = $this->bottomUpTree(-$i, $minDepth);
        $check += $this->itemCheck($t);
        unset($t);
      }
      printf("%d't trees of depth %d't check: %d'n", $iterations<<1, $minDepth, $check);
      $minDepth += 2;
      $iterations >>= 2;
    }
    while($minDepth <= $maxDepth);
    printf("long lived tree of depth %d't check: %d'n",
      $maxDepth, $this->itemCheck($longLivedTree));
  }
}
$tree = new Tree();
$tree->run($argc, $argv);

您的递归永远不会碰到基本情况,并且在递归时传递的值永远不会为null,因此递归将停止。

如果$depth为0以及null,则可能需要退出。