Skip to content

Commit

Permalink
perf: optimize slice clearing operation (#78)
Browse files Browse the repository at this point in the history
**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 <xuthus5@gmail.com>
  • Loading branch information
xuthus5 committed Jun 26, 2024
1 parent e61e99b commit a9068f3
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions opengemini/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ func (c *client) internalBatchSend(ctx context.Context, database string, resourc
}
needFlush = false
ticker.Reset(tickInterval)
points = []*Point{}
cbs = []WriteCallback{}
points = points[:0]
cbs = cbs[:0]
}
}
}
Expand Down

0 comments on commit a9068f3

Please sign in to comment.