WordPress function saves a post twice and updates all posts

It duplicate because when you run the wp_update_post(), it will use the wp_insert_post() function and the action save_post will run again.

please use the filter wp_insert_post_data to filter the value before save. https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data

Example:

function wpse309780_filter_post_data($data , $postarr) {
    $data['post_name'] = wp_count_posts( 'post' )->publish;
    return $data;
}

add_filter( 'wp_insert_post_data', 'wpse309780_filter_post_data' );