 ### Dynprog: Avoid millions (billions?) of small allocations

```Previously we created small arrays of three integers to store information for backtracking.
Now we have a structure storing the same information so that we can allocate an array of structures.
Therefore we can allocate the whole array at once, and we don't need anymore to have plenty of allocations of three integers```
parent 756ef3c2
 ... ... @@ -102,12 +102,9 @@ DynProg::DynProg(const string &x, const string &y, DynProgMode mode, const Cost& S[i] = new int[n+1]; } B = new int**[m+1]; B = new backtrack_info*[m+1]; for (int i = 0; i <= m; i++) { B[i] = new int*[n+1]; for (int j = 0; j <= n; j++) { B[i][j] = new int; } B[i] = new backtrack_info[n+1]; } this -> mode = mode; ... ... @@ -131,11 +128,6 @@ DynProg::~DynProg() { } delete [] S; for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { delete [] B[i][j]; } } for (int i = 0; i <= m; i++) { delete [] B[i]; } ... ... @@ -209,45 +201,45 @@ int DynProg::compute() } if (best == 0){ B[i][j] = FIN; B[i][j].type = FIN; } } S[i][j] = best ; if (best==subst){ B[i][j] = i-1 ; B[i][j] = j-1 ; B[i][j] = SUBST ; B[i][j].i = i-1 ; B[i][j].j = j-1 ; B[i][j].type = SUBST ; } if (best==inser){ B[i][j] = i-1 ; B[i][j] = j ; B[i][j] = INSER ; B[i][j].i = i-1 ; B[i][j].j = j ; B[i][j].type = INSER ; } if (best==delet){ B[i][j] = i ; B[i][j] = j-1 ; B[i][j] = DELET ; B[i][j].i = i ; B[i][j].j = j-1 ; B[i][j].type = DELET ; } if (best==homo2x){ B[i][j] = i-2 ; if (B[i][j] < 0) B[i][j] = 0 ; B[i][j] = j-1 ; B[i][j] = HOMO2X ; B[i][j].i = i-2 ; if (B[i][j].i < 0) B[i][j].i = 0 ; B[i][j].j = j-1 ; B[i][j].type = HOMO2X ; } if (best==homo2y){ B[i][j] = i-1 ; B[i][j] = j-2 ; if (B[i][j] < 0) B[i][j] = 0 ; B[i][j] = HOMO2Y ; B[i][j].i = i-1 ; B[i][j].j = j-2 ; if (B[i][j].j < 0) B[i][j].j = 0 ; B[i][j].type = HOMO2Y ; } if (mode == Local || mode == LocalEndWithSomeDeletions) { if (best == 0){ B[i][j] = FIN; B[i][j].type = FIN; } } ... ... @@ -256,10 +248,10 @@ int DynProg::compute() if (mode == Local || mode == LocalEndWithSomeDeletions) { for (int i=0; i<=n; i++){ B[i] = FIN; B[i].type = FIN; } for (int i=0; i<=m; i++){ B[i] = FIN; B[i].type = FIN; } } ... ... @@ -274,12 +266,12 @@ int DynProg::compute() best_j = j ; } for (int i=1; i<=n; i++){ B[i] = 0 ; B[i] = i-1 ; B[i] = DELET; B[i].i = 0 ; B[i].j = i-1 ; B[i].type = DELET; } for (int i=0; i<=m; i++){ B[i] = FIN; B[i].type = FIN; } } ... ... @@ -295,12 +287,12 @@ int DynProg::compute() } for (int i=0; i<=n; i++){ B[i] = FIN; B[i].type = FIN; } for (int i=0; i<=m; i++){ B[i] = i-1 ; B[i] = 0 ; B[i] = INSER; B[i].i = i-1 ; B[i].j = 0 ; B[i].type = INSER; } } ... ... @@ -312,16 +304,16 @@ int DynProg::compute() best_score = S[m][n]; for (int i=0; i<=m; i++){ B[i] = i-1 ; B[i] = 0 ; B[i] = INSER; B[i].i = i-1 ; B[i].j = 0 ; B[i].type = INSER; } for (int i=1; i<=n; i++){ B[i] = 0 ; B[i] = i-1 ; B[i] = DELET; B[i].i = 0 ; B[i].j = i-1 ; B[i].type = DELET; } B = FIN; B.type = FIN; } if (reverse_x) ... ... @@ -370,12 +362,12 @@ void DynProg::backtrack() ostringstream back; while ( B[ti][tj] != FIN ){ while ( B[ti][tj].type != FIN ){ tmpi=B[ti][tj]; tmpj=B[ti][tj]; tmpi=B[ti][tj].i; tmpj=B[ti][tj].j; if (B[ti][tj] == SUBST ){ if (B[ti][tj].type == SUBST ){ linkgap[g1]=g2; back_s1 << x[ti-1]; g1--; ... ... @@ -389,7 +381,7 @@ void DynProg::backtrack() } } if (B[ti][tj] == INSER ){ if (B[ti][tj].type == INSER ){ linkgap[g1]=g2; back_s1 << x[ti-1]; g1--; ... ... @@ -398,7 +390,7 @@ void DynProg::backtrack() back_tr << "."; } if (B[ti][tj] == DELET){ if (B[ti][tj].type == DELET){ linkgap[g1]=g2; back_s1 << " "; gap1[g1]++; ... ... @@ -407,7 +399,7 @@ void DynProg::backtrack() back_tr << "."; } if (B[ti][tj] == HOMO2X ){ if (B[ti][tj].type == HOMO2X ){ linkgap[g1]=g2; back_s1 << x[ti-1] << x[ti-2]; g1--; ... ... @@ -420,7 +412,7 @@ void DynProg::backtrack() } if (B[ti][tj] == HOMO2Y ){ if (B[ti][tj].type == HOMO2Y ){ linkgap[g1]=g2; back_s1 << " " << x[ti-1] ; gap1[g1]++; ... ...
 ... ... @@ -15,6 +15,12 @@ using namespace std; float identity_percent(int score); typedef struct { int i; int j; int type; } backtrack_info; class Cost { public: ... ... @@ -103,7 +109,7 @@ class DynProg friend ostream& operator<<(ostream& out, const DynProg& dp); int **S; int ***B; backtrack_info **B; int *gap1 ; int *linkgap ; int *gap2 ; ... ...
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment