1

由于 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);
4

1 回答 1

0

您的递归永远不会遇到基本情况并停止,因为您在递归时传递的值永远不会为空。

您可能想退出 if $depthis 0 以及 if it is null

于 2015-06-29T09:57:48.450 回答