optimize became a big if-else/if-else

optimize became a big if-else/if-else, prepare to remove recursive call in optimize.
avoid recursion in optimize

avoid recursion in optimize, so that can optimize recursive function call.