On existing Blender could rebuild tree many times (on freeing each ID). Use a flag instead of immediately rebuilding.