@@ -19,21 +19,12 @@ type BuildMap struct {
1919 BuildStorageOffset uint64
2020}
2121
22- func (mapping * BuildMap ) Copy () * BuildMap {
23- return & BuildMap {
24- Offset : mapping .Offset ,
25- Length : mapping .Length ,
26- BuildId : mapping .BuildId ,
27- BuildStorageOffset : mapping .BuildStorageOffset ,
28- }
29- }
30-
3122func CreateMapping (
3223 buildId * uuid.UUID ,
3324 dirty * bitset.BitSet ,
3425 blockSize int64 ,
35- ) []* BuildMap {
36- var mappings []* BuildMap
26+ ) []BuildMap {
27+ var mappings []BuildMap
3728
3829 var startBlock uint
3930 var blockLength uint
@@ -47,7 +38,7 @@ func CreateMapping(
4738 }
4839
4940 if blockLength > 0 {
50- m := & BuildMap {
41+ m := BuildMap {
5142 Offset : uint64 (startBlock ) * uint64 (blockSize ),
5243 BuildId : * buildId ,
5344 Length : uint64 (blockLength ) * uint64 (blockSize ),
@@ -64,7 +55,7 @@ func CreateMapping(
6455 }
6556
6657 if blockLength > 0 {
67- mappings = append (mappings , & BuildMap {
58+ mappings = append (mappings , BuildMap {
6859 Offset : uint64 (startBlock ) * uint64 (blockSize ),
6960 BuildId : * buildId ,
7061 Length : uint64 (blockLength ) * uint64 (blockSize ),
@@ -82,26 +73,26 @@ func CreateMapping(
8273//
8374// It returns a new set of mappings that covers the whole size.
8475func MergeMappings (
85- baseMapping []* BuildMap ,
86- diffMapping []* BuildMap ,
87- ) []* BuildMap {
76+ baseMapping []BuildMap ,
77+ diffMapping []BuildMap ,
78+ ) []BuildMap {
8879 if len (diffMapping ) == 0 {
8980 return baseMapping
9081 }
9182
92- baseMappingCopy := make ([]* BuildMap , len (baseMapping ))
83+ baseMappingCopy := make ([]BuildMap , len (baseMapping ))
9384
9485 copy (baseMappingCopy , baseMapping )
9586
9687 baseMapping = baseMappingCopy
9788
98- mappings := make ([]* BuildMap , 0 )
89+ mappings := make ([]BuildMap , 0 )
9990
10091 var baseIdx int
10192 var diffIdx int
10293
10394 for baseIdx < len (baseMapping ) && diffIdx < len (diffMapping ) {
104- base := baseMapping [baseIdx ]
95+ base := & baseMapping [baseIdx ]
10596 diff := diffMapping [diffIdx ]
10697
10798 if base .Length == 0 {
@@ -119,7 +110,7 @@ func MergeMappings(
119110 // base is before diff and there is no overlap
120111 // add base to the result, because it will not be overlapping by any diff
121112 if base .Offset + base .Length <= diff .Offset {
122- mappings = append (mappings , base )
113+ mappings = append (mappings , * base )
123114
124115 baseIdx ++
125116
@@ -153,15 +144,13 @@ func MergeMappings(
153144 leftBaseLength := int64 (diff .Offset ) - int64 (base .Offset )
154145
155146 if leftBaseLength > 0 {
156- leftBase := & BuildMap {
147+ mappings = append ( mappings , BuildMap {
157148 Offset : base .Offset ,
158149 Length : uint64 (leftBaseLength ),
159150 BuildId : base .BuildId ,
160151 // the build storage offset is the same as the base mapping
161152 BuildStorageOffset : base .BuildStorageOffset ,
162- }
163-
164- mappings = append (mappings , leftBase )
153+ })
165154 }
166155
167156 mappings = append (mappings , diff )
@@ -172,14 +161,12 @@ func MergeMappings(
172161 rightBaseLength := int64 (base .Length ) - rightBaseShift
173162
174163 if rightBaseLength > 0 {
175- rightBase := & BuildMap {
164+ baseMapping [ baseIdx ] = BuildMap {
176165 Offset : base .Offset + uint64 (rightBaseShift ),
177166 Length : uint64 (rightBaseLength ),
178167 BuildId : base .BuildId ,
179168 BuildStorageOffset : base .BuildStorageOffset + uint64 (rightBaseShift ),
180169 }
181-
182- baseMapping [baseIdx ] = rightBase
183170 } else {
184171 baseIdx ++
185172 }
@@ -199,14 +186,12 @@ func MergeMappings(
199186 rightBaseLength := int64 (base .Length ) - rightBaseShift
200187
201188 if rightBaseLength > 0 {
202- rightBase := & BuildMap {
189+ baseMapping [ baseIdx ] = BuildMap {
203190 Offset : base .Offset + uint64 (rightBaseShift ),
204191 Length : uint64 (rightBaseLength ),
205192 BuildId : base .BuildId ,
206193 BuildStorageOffset : base .BuildStorageOffset + uint64 (rightBaseShift ),
207194 }
208-
209- baseMapping [baseIdx ] = rightBase
210195 } else {
211196 baseIdx ++
212197 }
@@ -220,14 +205,12 @@ func MergeMappings(
220205 leftBaseLength := int64 (diff .Offset ) - int64 (base .Offset )
221206
222207 if leftBaseLength > 0 {
223- leftBase := & BuildMap {
208+ mappings = append ( mappings , BuildMap {
224209 Offset : base .Offset ,
225210 Length : uint64 (leftBaseLength ),
226211 BuildId : base .BuildId ,
227212 BuildStorageOffset : base .BuildStorageOffset ,
228- }
229-
230- mappings = append (mappings , leftBase )
213+ })
231214 }
232215
233216 baseIdx ++
@@ -245,29 +228,25 @@ func MergeMappings(
245228}
246229
247230// NormalizeMappings joins adjacent mappings that have the same buildId.
248- func NormalizeMappings (mappings []* BuildMap ) []* BuildMap {
231+ func NormalizeMappings (mappings []BuildMap ) []BuildMap {
249232 if len (mappings ) == 0 {
250233 return nil
251234 }
252235
253- result := make ([]* BuildMap , 0 , len (mappings ))
236+ result := make ([]BuildMap , 0 , len (mappings ))
254237
255- // Start with a copy of the first mapping
256- current := mappings [0 ].Copy ()
238+ current := mappings [0 ]
257239
258240 for i := 1 ; i < len (mappings ); i ++ {
259241 mp := mappings [i ]
260- if mp .BuildId != current .BuildId {
261- // BuildId changed, add the current map to results and start a new one
262- result = append (result , current )
263- current = mp .Copy () // New copy
264- } else {
265- // Same BuildId, just add the length
242+ if mp .BuildId == current .BuildId {
266243 current .Length += mp .Length
244+ } else {
245+ result = append (result , current )
246+ current = mp
267247 }
268248 }
269249
270- // Add the last mapping
271250 result = append (result , current )
272251
273252 return result
0 commit comments