Skip to content

spanner: large number of unnecessary allocations constructing mutations in BatchWrite #12680

@pbotros

Description

@pbotros

Client

Spanner

Environment

All, on latest master

Code and Dependencies

In spanner/mutation.go, mutationsProto unnecessary allocates slices and rand objects:

func mutationsProto(ms []*Mutation) ([]*sppb.Mutation, *sppb.Mutation, error) {
	var selectedMutation *Mutation
	var nonInsertMutations []*Mutation
...
	if len(nonInsertMutations) > 0 {
		selectedMutation = nonInsertMutations[rand.New(rand.NewSource(time.Now().UnixNano())).Intn(len(nonInsertMutations))]
	}
...
}

All production callers of this function ignore the second return value. The only functionality of tracking selectedMutation, nonInsertMutations seem to be for test.

Expected behavior

There are fewer unnecessary allocations.

Actual behavior

Allocations due to rand and slices only used for test cause massive amounts of allocations, increasing CPU and memory usage. See screenshot for a heap profile taken on a pod that performs a large number of BatchWrite calls.

Screenshots

Heap profile viewing "alloc - space" showing large amounts of allocated space:

Image

Metadata

Metadata

Assignees

Labels

api: spannerIssues related to the Spanner API.triage meI really want to be triaged.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions