Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: optimize slice clearing operation (#78)
**Description** This PR optimizes the initialization of slice a by replacing `a = []int{}` with `a = a[:0]`. This change reduces unnecessary memory allocation and improves performance. **Motivation** When a is initialized with `a = []int{}`, a new, empty slice is created, which allocates memory for the underlying array. However, if a is already allocated with a non-zero capacity, reslicing it with `a = a[:0]` reuses the existing underlying array, avoiding unnecessary memory allocation. **Benefits** 1. Reduces memory allocation and garbage collection overhead 2. Improves performance by reusing existing memory allocation 3. Simplifies code by eliminating unnecessary memory allocation **Notes** This optimization is safe because it is a basic and ordinary optimization. Signed-off-by: Young Xu <[email protected]>
- Loading branch information