Skip to content

Commit

Permalink
优化双数组trie树,构建后自动shrink到最低内存 close #984
Browse files Browse the repository at this point in the history
  • Loading branch information
hankcs committed Sep 29, 2018
1 parent 1422c0f commit 5738874
Showing 1 changed file with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ public int build(List<String> _key, int _length[], int _value[],
List<Node> siblings = new ArrayList<Node>();
fetch(root_node, siblings);
insert(siblings);
shrink();

// size += (1 << 8 * 2) + 1; // ???
// if (size >= allocSize) resize (size);
Expand Down Expand Up @@ -1435,6 +1436,24 @@ public V get(int index)
return v[index];
}

/**
* 释放空闲的内存
*/
private void shrink()
{
// if (HanLP.Config.DEBUG)
// {
// System.err.printf("释放内存 %d bytes\n", base.length - size - 65535);
// }
int nbase[] = new int[size + 65535];
System.arraycopy(base, 0, nbase, 0, size);
base = nbase;

int ncheck[] = new int[size + 65535];
System.arraycopy(check, 0, ncheck, 0, size);
check = ncheck;
}


/**
* 打印统计信息
Expand Down

0 comments on commit 5738874

Please sign in to comment.