递归到迭代变换

Recursive to Iterative Transformation

本文关键字:变换 迭代 递归      更新时间:2023-10-16

我一直在尝试将我的代码从递归函数重写为迭代函数。

我想我应该问一下是否有什么一般的事情需要考虑/技巧/指南等…关于从递归代码到迭代代码。

。我不能完全理解如何让下面的代码迭代,主要是由于递归内部的循环,它进一步依赖并调用下一个递归。

struct entry
{
    uint8_t values[8];
    int32_t num_values;
    std::array<entry, 256>* next_table;
    void push_back(uint8_t value) {values[num_values++] = value;}
};
struct node
{
    node*               children; // +0 right, +1 left
    uint8_t             value;
    uint8_t             is_leaf;
};
void build_tables(node* root, std::array<std::array<entry, 8>, 255>& tables, int& table_count)
{
    int table_index = root->value; // root is always a non-leave, thus value is the current table index.
    for(int n = 0; n < 256; ++n)
    {
        auto current = root;
        // Recurse the the huffman tree bit by bit for this table entry
        for(int i = 0; i < 8; ++i)
        {
            current = current->children + ((n >> i) & 1); // Travel to the next node    current->children[0] is left child and current->children[1] is right child. If current is a leaf then current->childen[0/1] point to the root.
            if(current->is_leaf)
                tables[table_index][n].push_back(current->value);
        }
        if(!current->is_leaf)
        {
            if(current->value == 0) // For non-leaves, the "value" is the sub-table index for this particular non-leave node
            {
                current->value = table_count++;
                build_tables(current, tables, table_count);
            }
            tables[table_index][n].next_table = &tables[current->value];
        }
        else
            tables[table_index][n].next_table = &tables[0];
    }   
}

由于tablestable_count总是引用相同的对象,您可以通过将tablestable_countbuild_tables的参数列表中取出,将它们存储为临时结构体的成员,然后执行以下操作来获得较小的性能增益:

struct build_tables_struct
{
  build_tables_struct(std::array<std::array<entry, 8>, 255>& tables, int& table_count) :
    tables(tables), table_count(table_count) {}
  std::array<std::array<entry, 8>, 255>& tables;
  int& table_count;
  build_tables_worker(node* root) 
  {
     ...
     build_tables_worker(current); // instead of build_tables(current, tables, table_count);
     ...
  }
}
void build_tables(node* root, std::array<std::array<entry, 8>, 255>& tables, int& table_count)
{
  build_tables_struct(tables, table_count).build_tables_worker(root);
}

当然,这只适用于你的编译器不够聪明,无法自己进行优化的情况。

你唯一能使这个非递归的方法是自己管理堆栈。我怀疑这是否会比递归版本快得多。

说了这么多,我怀疑你的性能问题是递归。将三个引用参数压入堆栈并调用函数,我认为与函数所做的工作相比,这不是一个巨大的负担。