Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-allocate memory to reduce the number of allocations #272

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion convertor/convertor.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func ToBytes(value any) ([]byte, error) {
// ToChar convert string to char slice.
// Play: https://go.dev/play/p/JJ1SvbFkVdM
func ToChar(s string) []string {
c := make([]string, 0)
c := make([]string, 0, len(s))
if len(s) == 0 {
c = append(c, "")
}
Expand Down
2 changes: 1 addition & 1 deletion datastructure/link/doublylink.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (dl *DoublyLink[T]) Size() int {

// Values return slice of all doubly linklist node value
func (dl *DoublyLink[T]) Values() []T {
result := []T{}
result := make([]T, 0, dl.length)
current := dl.Head
for current != nil {
result = append(result, current.Value)
Expand Down
2 changes: 1 addition & 1 deletion datastructure/link/singlylink.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (sl *SinglyLink[T]) Size() int {

// Values return slice of all singly linklist node value
func (sl *SinglyLink[T]) Values() []T {
result := []T{}
result := make([]T, 0, sl.length)
current := sl.Head
for current != nil {
result = append(result, current.Value)
Expand Down
2 changes: 1 addition & 1 deletion datastructure/queue/arrayqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewArrayQueue[T any](capacity int) *ArrayQueue[T] {

// Data return slice of queue data
func (q *ArrayQueue[T]) Data() []T {
items := []T{}
items := make([]T, 0, q.tail-q.head)
for i := q.head; i < q.tail; i++ {
items = append(items, q.data[i])
}
Expand Down
2 changes: 1 addition & 1 deletion datastructure/queue/linkedqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewLinkedQueue[T any]() *LinkedQueue[T] {

// Data return slice of queue data
func (q *LinkedQueue[T]) Data() []T {
res := []T{}
res := make([]T, 0, q.length)
current := q.head

for current != nil {
Expand Down
2 changes: 1 addition & 1 deletion datastructure/stack/linkedstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewLinkedStack[T any]() *LinkedStack[T] {

// Data return stack data
func (s *LinkedStack[T]) Data() []T {
res := []T{}
res := make([]T, 0, s.length)
current := s.top

for current != nil {
Expand Down
2 changes: 1 addition & 1 deletion datastructure/tree/tree_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func printTreeNodes[T any](nodes []*datastructure.TreeNode[T], level, maxLevel i

printSpaces(firstSpaces)

newNodes := []*datastructure.TreeNode[T]{}
newNodes := make([]*datastructure.TreeNode[T], 0, len(nodes)*2)
for _, node := range nodes {
if node != nil {
fmt.Printf("%v", node.Value)
Expand Down
6 changes: 4 additions & 2 deletions fileutil/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"encoding/csv"
"errors"
"fmt"
"github.com/duke-git/lancet/v2/validator"
"io"
"io/fs"
"net/http"
Expand All @@ -24,6 +23,8 @@ import (
"sort"
"strings"
"sync"

"github.com/duke-git/lancet/v2/validator"
)

// FileReader is a reader supporting offset seeking and reading one
Expand Down Expand Up @@ -819,6 +820,7 @@ func WriteMapsToCsv(filepath string, records []map[string]any, appendToExistingF
if len(headers) > 0 {
columnHeaders = headers[0]
} else {
columnHeaders = make([]string, 0, len(records[0]))
for key := range records[0] {
columnHeaders = append(columnHeaders, key)
}
Expand All @@ -832,7 +834,7 @@ func WriteMapsToCsv(filepath string, records []map[string]any, appendToExistingF
}

for _, record := range records {
var row []string
row := make([]string, 0, len(columnHeaders))
for _, h := range columnHeaders {
row = append(row, fmt.Sprintf("%v", record[h]))
}
Expand Down
2 changes: 1 addition & 1 deletion netutil/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func ConvertMapToQueryString(param map[string]any) string {
if param == nil {
return ""
}
var keys []string
keys := make([]string, 0, len(param))
for key := range param {
keys = append(keys, key)
}
Expand Down
1 change: 1 addition & 0 deletions stream/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ func (s Stream[T]) Skip(n int) Stream[T] {
return FromSlice(source)
}

source = make([]T, 0, l-n)
for i := n; i < l; i++ {
source = append(source, s.source[i])
}
Expand Down
2 changes: 1 addition & 1 deletion structs/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ func (s *Struct) ToMap() (map[string]any, error) {

// Fields returns all the struct fields within a slice
func (s *Struct) Fields() []*Field {
var fields []*Field
fieldNum := s.rvalue.NumField()
fields := make([]*Field, 0, fieldNum)
for i := 0; i < fieldNum; i++ {
v := s.rvalue.Field(i)
sf := s.rtype.Field(i)
Expand Down
2 changes: 1 addition & 1 deletion xerror/xerror.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func Wrap(cause error, message ...any) *XError {
err := newXError()

if len(message) > 0 {
var newMsgs []string
newMsgs := make([]string, 0, len(message))
for _, m := range message {
newMsgs = append(newMsgs, fmt.Sprintf("%v", m))
}
Expand Down