append
can be combined into a single call CRT-P0001443 for i := 10; i < 50; i++ {
444 regions = append(regions, testRegionInfo{uint64(i), []uint64{1, 2, 3}, smallHotPeerByte, 0, smallHotPeerQuery})
445 if i < 20 {
446 regions = append(regions, testRegionInfo{uint64(i), []uint64{2, 1, 3}, smallHotPeerByte, 0, smallHotPeerQuery})447 regions = append(regions, testRegionInfo{uint64(i), []uint64{3, 1, 2}, smallHotPeerByte, 0, smallHotPeerQuery})
448 }
449 }
1012
1013 events := []*etcdEvent{}
1014 // Split target keyspace group event arrives first.
1015 events = append(events, generateKeyspaceGroupPutEvent(2, []uint32{2} /* Mock 2 replicas */, []string{svcAddr, svcAddr}, &endpoint.SplitState{1016 SplitSource: 1,
1017 }))
1018 // Split source keyspace group event arrives later.
291 events := []*etcdEvent{}
292 // Assign keyspace group 0 to this host/pod/keyspace-group-manager.
293 // final result: assigned [0], loaded [0]
294 events = append(events, generateKeyspaceGroupPutEvent(0, []uint32{0}, []string{svcAddr})) 295 // Assign keyspace group 1 to this host/pod/keyspace-group-manager.
296 // final result: assigned [0,1], loaded [0,1]
297 events = append(events, generateKeyspaceGroupPutEvent(1, []uint32{1}, []string{"unknown", svcAddr}))
270 reported += str
271 }
272 }
273 status.Details = append(status.Details, "Stores that have not dispatched plan: "+strings.Trim(undispatched, ", ")) 274 status.Details = append(status.Details, "Stores that have reported to PD: "+strings.Trim(reported, ", "))
275 status.Details = append(status.Details, "Stores that have not reported to PD: "+strings.Trim(unreported, ", "))
276 } else {
227func (suite *balanceSchedulerPlanAnalyzeTestSuite) TestAnalyzerResult5() {
228 re := suite.Require()
229 plans := make([]Plan, 0)
230 plans = append(plans, &BalanceSchedulerPlan{Source: suite.stores[4], Step: 0, Status: NewStatus(StatusStoreRemoveLimitThrottled)})231 plans = append(plans, &BalanceSchedulerPlan{Source: suite.stores[3], Region: suite.regions[0], Step: 1, Status: NewStatus(StatusRegionNotMatchRule)})
232 plans = append(plans, &BalanceSchedulerPlan{Source: suite.stores[2], Region: suite.regions[0], Step: 1, Status: NewStatus(StatusRegionNotMatchRule)})
233 plans = append(plans, &BalanceSchedulerPlan{Source: suite.stores[1], Target: suite.stores[0], Step: 2, Status: NewStatus(StatusStoreScoreDisallowed)})
Multiple calls for append
could be combined into a single call for append
.
The append
built-in function appends elements to the end of a slice. If it has
sufficient capacity, the destination is resliced to accommodate the new
elements, but if capacity is not enough, then append
will allocate a new
underlying array and return the updated slice. Therefore, it is necessary to
store the result of append, often in the variable holding the slice itself.
Appending calls in a single call of append
allocates memory just once to
accommodate all the elements to be appended. Whereas multiple calls to append
introduce many overheads, most notably being the possibility of more calls for
memory allocation because the total number of elements to be appended over
multiple calls of append
is unknown beforehand, resulting in inaccurate
preallocation.
Read more about a
similar implementation for append
.
Hence, a single call to append
is recommended.
xs = append(xs, 1)
xs = append(xs, 2)
xs = append(xs, 3)
xs = append(xs, 4)
xs = append(xs, 5)
xs = append(xs, 1, 2, 3, 4, 5)