@@ -22,22 +22,12 @@ type BuildMap struct {
2222 FrameTable * storage.FrameTable
2323}
2424
25- << << << < HEAD
26- func (mapping * BuildMap ) Copy () * BuildMap {
27- return & BuildMap {
28- Offset : mapping .Offset ,
29- Length : mapping .Length ,
30- BuildId : mapping .BuildId ,
31- BuildStorageOffset : mapping .BuildStorageOffset ,
32- FrameTable : mapping .FrameTable ,
33- }
34- }
35-
36- // SetFrames associates compression frame information with this mapping,
37- // starting the scan from frame index `from` and
38- // returning the next cursor position. Use this when applying frames to a
39- // sorted sequence of mappings to avoid O(N²) rescanning.
40- func (mapping * BuildMap ) SetFrames (frameTable * storage.FrameTable , from int ) (int , error ) {
25+ // setFrames associates compression frame information with mapping,
26+ // ApplyFrames extracts the FrameTable subset that covers mapping's storage range,
27+ // assigns it to mapping.FrameTable, and returns the next cursor position.
28+ // Use cursor-chaining when applying frames to a sorted sequence of mappings
29+ // to avoid O(N²) rescanning.
30+ func ApplyFrames (mapping * BuildMap , frameTable * storage.FrameTable , from int ) (int , error ) {
4131 if frameTable == nil {
4232 return from , nil
4333 }
@@ -58,8 +48,6 @@ func (mapping *BuildMap) SetFrames(frameTable *storage.FrameTable, from int) (in
5848 return next , nil
5949}
6050
61- == == == =
62- >> >> >> > f98f20f7d1f207b34d12f6e8b570e4c10c35aa31
6351func CreateMapping (
6452 buildId * uuid.UUID ,
6553 dirty * bitset.BitSet ,
@@ -114,15 +102,9 @@ func CreateMapping(
114102//
115103// It returns a new set of mappings that covers the whole size.
116104func MergeMappings (
117- << << << < HEAD
118- baseMapping []* BuildMap ,
119- diffMapping []* BuildMap ,
120- ) ([]* BuildMap , error ) {
121- == == == =
122105 baseMapping []BuildMap ,
123106 diffMapping []BuildMap ,
124- ) []BuildMap {
125- >> >> >> > f98f20f7d1f207b34d12f6e8b570e4c10c35aa31
107+ ) ([]BuildMap , error ) {
126108 if len (diffMapping ) == 0 {
127109 return baseMapping , nil
128110 }
@@ -195,24 +177,19 @@ func MergeMappings(
195177 frameCursor := 0
196178
197179 if leftBaseLength > 0 {
198- mappings = append (mappings , BuildMap {
199- Offset : base.Offset ,
200- Length : uint64 (leftBaseLength ),
201- BuildId : base.BuildId ,
202- // the build storage offset is the same as the base mapping
180+ leftBase := BuildMap {
181+ Offset : base .Offset ,
182+ Length : uint64 (leftBaseLength ),
183+ BuildId : base .BuildId ,
203184 BuildStorageOffset : base .BuildStorageOffset ,
204- << << << < HEAD
205185 }
206186 var err error
207- frameCursor , err = leftBase. SetFrames ( base.FrameTable , 0 )
187+ frameCursor , err = ApplyFrames ( & leftBase , base .FrameTable , 0 )
208188 if err != nil {
209189 return nil , fmt .Errorf ("set frames for left split at offset %d: %w" , leftBase .Offset , err )
210190 }
211191
212192 mappings = append (mappings , leftBase )
213- == == == =
214- })
215- >> >> >> > f98f20f7d1f207b34d12f6e8b570e4c10c35aa31
216193 }
217194
218195 mappings = append (mappings , diff )
@@ -223,20 +200,17 @@ func MergeMappings(
223200 rightBaseLength := int64 (base .Length ) - rightBaseShift
224201
225202 if rightBaseLength > 0 {
226- baseMapping [ baseIdx ] = BuildMap {
203+ rightBase : = BuildMap {
227204 Offset : base .Offset + uint64 (rightBaseShift ),
228205 Length : uint64 (rightBaseLength ),
229206 BuildId : base .BuildId ,
230207 BuildStorageOffset : base .BuildStorageOffset + uint64 (rightBaseShift ),
231208 }
232- << << << < HEAD
233- if _, err := rightBase.SetFrames (base.FrameTable , frameCursor ); err != nil {
209+ if _ , err := ApplyFrames (& rightBase , base .FrameTable , frameCursor ); err != nil {
234210 return nil , fmt .Errorf ("set frames for right split at offset %d: %w" , rightBase .Offset , err )
235211 }
236212
237213 baseMapping [baseIdx ] = rightBase
238- == == == =
239- >> >> >> > f98f20f7d1f207b34d12f6e8b570e4c10c35aa31
240214 } else {
241215 baseIdx ++
242216 }
@@ -256,20 +230,17 @@ func MergeMappings(
256230 rightBaseLength := int64 (base .Length ) - rightBaseShift
257231
258232 if rightBaseLength > 0 {
259- baseMapping [ baseIdx ] = BuildMap {
233+ rightBase : = BuildMap {
260234 Offset : base .Offset + uint64 (rightBaseShift ),
261235 Length : uint64 (rightBaseLength ),
262236 BuildId : base .BuildId ,
263237 BuildStorageOffset : base .BuildStorageOffset + uint64 (rightBaseShift ),
264238 }
265- << << << < HEAD
266- if _, err := rightBase.SetFrames (base.FrameTable , 0 ); err != nil {
239+ if _ , err := ApplyFrames (& rightBase , base .FrameTable , 0 ); err != nil {
267240 return nil , fmt .Errorf ("set frames for right split at offset %d: %w" , rightBase .Offset , err )
268241 }
269242
270243 baseMapping [baseIdx ] = rightBase
271- == == == =
272- >> >> >> > f98f20f7d1f207b34d12f6e8b570e4c10c35aa31
273244 } else {
274245 baseIdx ++
275246 }
@@ -283,21 +254,17 @@ func MergeMappings(
283254 leftBaseLength := int64 (diff .Offset ) - int64 (base .Offset )
284255
285256 if leftBaseLength > 0 {
286- mappings = append ( mappings , BuildMap {
257+ leftBase := BuildMap {
287258 Offset : base .Offset ,
288259 Length : uint64 (leftBaseLength ),
289260 BuildId : base .BuildId ,
290261 BuildStorageOffset : base .BuildStorageOffset ,
291- << << << < HEAD
292262 }
293- if _, err := leftBase. SetFrames ( base.FrameTable , 0 ); err != nil {
263+ if _ , err := ApplyFrames ( & leftBase , base .FrameTable , 0 ); err != nil {
294264 return nil , fmt .Errorf ("set frames for left split at offset %d: %w" , leftBase .Offset , err )
295265 }
296266
297267 mappings = append (mappings , leftBase )
298- == == == =
299- })
300- >> >> >> > f98f20f7d1f207b34d12f6e8b570e4c10c35aa31
301268 }
302269
303270 baseIdx ++
@@ -315,55 +282,34 @@ func MergeMappings(
315282}
316283
317284// NormalizeMappings joins adjacent mappings that have the same buildId.
318- << << << < HEAD
319285// When merging mappings, FrameTables are also merged by extending the first
320286// mapping's FrameTable with frames from subsequent mappings.
321- func NormalizeMappings (mappings []* BuildMap ) []* BuildMap {
322- == == == =
323287func NormalizeMappings (mappings []BuildMap ) []BuildMap {
324- >> >> >> > f98f20f7d1f207b34d12f6e8b570e4c10c35aa31
325288 if len (mappings ) == 0 {
326289 return nil
327290 }
328291
329292 result := make ([]BuildMap , 0 , len (mappings ))
330293
331- << << << < HEAD
332- // Start with a copy of the first mapping (Copy() now includes FrameTable)
333- current := mappings[0 ].Copy ()
294+ current := mappings [0 ]
334295
335296 for i := 1 ; i < len (mappings ); i ++ {
336297 mp := mappings [i ]
337298 if mp .BuildId != current .BuildId {
338- // BuildId changed, add the current map to results and start a new one
339299 result = append (result , current )
340- current = mp. Copy () // New copy (includes FrameTable)
300+ current = mp
341301 } else {
342302 // Same BuildId, merge: add the length and extend FrameTable
343303 current .Length += mp .Length
344304
345305 // Extend FrameTable if the mapping being merged has one
346306 if mp .FrameTable != nil {
347307 if current .FrameTable == nil {
348- // Current has no FrameTable but merged one does - take it
349308 current .FrameTable = mp .FrameTable
350309 } else {
351- // Both have FrameTables - extend current's with mp's frames
352- // The frames are contiguous subsets, so we append non-overlapping frames
353310 current .FrameTable = mergeFrameTables (current .FrameTable , mp .FrameTable )
354311 }
355312 }
356- == == == =
357- current := mappings [0 ]
358-
359- for i := 1 ; i < len (mappings ); i + + {
360- mp := mappings [i ]
361- if mp.BuildId == current.BuildId {
362- current.Length += mp.Length
363- } else {
364- result = append (result , current )
365- current = mp
366- >> >> >> > f98f20f7d1f207b34d12f6e8b570e4c10c35aa31
367313 }
368314 }
369315
0 commit comments