Optimize getSpaceCount