@@ -31,7 +31,6 @@ import (
3131 "github.com/pingcap/tidb/pkg/util/chunk"
3232 "github.com/pingcap/tidb/pkg/util/collate"
3333 "github.com/pingcap/tidb/pkg/util/fastrand"
34- "github.com/pingcap/tidb/pkg/util/intest"
3534 "github.com/pingcap/tidb/pkg/util/sqlexec"
3635 "github.com/pingcap/tipb/go-tipb"
3736 "github.com/twmb/murmur3"
@@ -40,7 +39,7 @@ import (
4039// SampleItem is an item of sampled column value.
4140type SampleItem struct {
4241 // Value is the sampled column value.
43- Value * types.Datum
42+ Value types.Datum
4443 // Handle is the handle of the sample in its key.
4544 // This property is used to calculate Ordinal in fast analyze.
4645 Handle kv.Handle
@@ -49,38 +48,14 @@ type SampleItem struct {
4948 Ordinal int
5049}
5150
52- // EmptySampleItemSize is the size of an empty SampleItem on 64-bit platforms: 32 = 8 (Value pointer ) + 16 (Handle interface ) + 8 (Ordinal int) .
51+ // EmptySampleItemSize is the size of empty SampleItem, 96 = 72 (datum ) + 8 (int ) + 16 .
5352const EmptySampleItemSize = int64 (unsafe .Sizeof (SampleItem {}))
5453
55- // CopySampleItems returns a deep copy of SampleItem slice.
56- func CopySampleItems (items []* SampleItem ) []* SampleItem {
57- n := make ([]* SampleItem , len (items ))
58- for i , item := range items {
59- ni := & SampleItem {
60- Handle : item .Handle ,
61- Ordinal : item .Ordinal ,
62- }
63- if item .Value != nil {
64- ni .Value = & types.Datum {}
65- item .Value .Copy (ni .Value )
66- }
67- n [i ] = ni
68- }
69- return n
70- }
71-
7254func sortSampleItems (sc * stmtctx.StatementContext , items []* SampleItem ) error {
7355 var err error
74- if intest .InTest {
75- for _ , item := range items {
76- if item .Value == nil {
77- return errors .Errorf ("sample item value is nil" )
78- }
79- }
80- }
8156 slices .SortStableFunc (items , func (i , j * SampleItem ) int {
8257 var cmp int
83- cmp , err = i .Value .Compare (sc .TypeCtx (), j .Value , collate .GetBinaryCollator ())
58+ cmp , err = i .Value .Compare (sc .TypeCtx (), & j .Value , collate .GetBinaryCollator ())
8459 if err != nil {
8560 return - 1
8661 }
@@ -130,11 +105,7 @@ func (c *SampleCollector) MergeSampleCollector(sc *stmtctx.StatementContext, rc
130105 terror .Log (errors .Trace (err ))
131106 }
132107 for _ , item := range rc .Samples {
133- if item .Value == nil {
134- terror .Log (errors .Errorf ("sample item value is nil" ))
135- continue
136- }
137- err := c .collect (sc , * item .Value )
108+ err := c .collect (sc , item .Value )
138109 terror .Log (errors .Trace (err ))
139110 }
140111}
@@ -173,8 +144,7 @@ func SampleCollectorFromProto(collector *tipb.SampleCollector) *SampleCollector
173144 for _ , val := range collector .Samples {
174145 // When store the histogram bucket boundaries to kv, we need to limit the length of the value.
175146 if len (val ) <= MaxSampleValueLength {
176- tmp := types .NewBytesDatum (val )
177- item := & SampleItem {Value : & tmp }
147+ item := & SampleItem {Value : types .NewBytesDatum (val )}
178148 s .Samples = append (s .Samples , item )
179149 }
180150 }
@@ -202,15 +172,15 @@ func (c *SampleCollector) collect(sc *stmtctx.StatementContext, d types.Datum) e
202172 // to the underlying slice, GC can't free them which lead to memory leak eventually.
203173 // TODO: Refactor the proto to avoid copying here.
204174 if len (c .Samples ) < int (c .MaxSampleSize ) {
205- newItem := & SampleItem {Value : & types. Datum {} }
206- d .Copy (newItem .Value )
175+ newItem := & SampleItem {}
176+ d .Copy (& newItem .Value )
207177 c .Samples = append (c .Samples , newItem )
208178 } else {
209179 shouldAdd := int64 (fastrand .Uint64N (uint64 (c .seenValues ))) < c .MaxSampleSize
210180 if shouldAdd {
211181 idx := int (fastrand .Uint32N (uint32 (c .MaxSampleSize )))
212- newItem := & SampleItem {Value : & types. Datum {} }
213- d .Copy (newItem .Value )
182+ newItem := & SampleItem {}
183+ d .Copy (& newItem .Value )
214184 // To keep the order of the elements, we use delete and append, not direct replacement.
215185 c .Samples = slices .Delete (c .Samples , idx , idx + 1 )
216186 c .Samples = append (c .Samples , newItem )
0 commit comments