-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbsdiff.zig
More file actions
1430 lines (1209 loc) · 57.1 KB
/
bsdiff.zig
File metadata and controls
1430 lines (1209 loc) · 57.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2003-2005 Colin Percival
// Copyright 2024 Yoav Givati
// All rights reserved
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted providing that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
// IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// Note: This is a modified version of bsdiff from Colin Percival.
// The goal is to move compression from bzip2 sections in the patch file to zstd for the whole patch file,
// explore other performance improvements leveraging zig, and potentially optimize for tar file diffing
// used by electrobun.
// todo:
// 1. implement qsufsort and split
// 2. implement search and matchlen
// 3. implement diffb
// 4. add wrapper that takes two absolute file paths and test creating patches
// 5. implement bspatch.zig
// 6. test applying a patch file with checksum
// 7. compare TRDIFF10 with zstd compression with bsdiff4.
// 8. create a cli interace that takes two file paths and creates a patch file
// 9. make build zig fast and small to compare sizes and perf and use in cli
// 10. consider making a standalone repo
const std = @import("std");
const builtin = @import("builtin");
const zstd = @cImport({
@cInclude("zstd.h");
});
const libsais = @cImport({
@cInclude("zig_wrapper.h");
});
const vectorSize = std.simd.suggestVectorLength(u8) orelse 4;
// Result from processing a single chunk
const ChunkResult = struct {
// Control block entries: each entry is 3 x i64 (forwardLen, extraLen, seekBy)
controlData: []u8,
controlLen: usize,
// Diff bytes
diffData: []u8,
diffLen: usize,
// Extra bytes
extraData: []u8,
extraLen: usize,
// Where this chunk actually ended in newData (may extend past nominal boundary)
actualEndPos: usize,
// Where this chunk started
startPos: usize,
};
// 1. create a bsdiff implementation that supports bzip2 classic bsdiff, and a mode that doesn't compress the patch file at all (so you can apply any compression to the whole file)
// since only the electrobun cli needs to compress the file size doesn't matter, we just need prebuilt binaries to electrobun build on different platforms
// 2. compile binaries for every target platform of bsdiff and bspatch with/without bzip2 compression.
// 2. the electrobun launcher will import the bzip-less bspatch.zig implementation and use the bspatch.zig directly in launcher.zig
// 4. the launcher will have an alternate mode to decompress a zstd file.
// 5. the cli will just use exec to run bsdiff against two tar files, it can do this in parallel to compare multiple versions at the same time.
// 6. the cli will then compress all the patch files with the zstd node library, also in parallel. support maybe the last 5 versions of the app. (configurable)
// 5. the electrobun bun api will call the launcher in the mode when it needs to decompress a zstd compressed patch file and apply it.
// 1. update bsdiff to take file path args instead of hardcoded
// 2. build bsdiff binary for arm and make it available to the cli
// 3. update electrobun cli to use bsdiff and the node zstd to generate patches in parallel by spawning multiple processes
// Note: patches don't contain all the information to create the old file from the new file, so the last x versions need to be kept and downloaded.
// 4. write the cli logic to download, generate patches, and create the artifacts folder
// 5. update the launcher binary to include zstd decompress and bspatch.zig directly.
// 6. create electrobun update api to check for updates, download, apply patches, and restart the app.
// 7. then later move bsdiff to its own repo, implement bzip backwards compatability, and a npm package with typescript wrapper.
pub fn main() !void {
var allocator = std.heap.page_allocator;
var args = try std.process.argsWithAllocator(allocator);
defer args.deinit();
// skip the first arg which is the program name
_ = args.skip();
const oldFilePath = args.next() orelse "";
const newFilePath = args.next() orelse "";
const patchFilePath = args.next() orelse "";
// By default we compress the blocks with bzip2 to make patches compatible with
// the original bsdiff implementation.
// In electrobun we disable block compression and compress the whole patch file with zstd
// in a separate process.
const optionUseZstd = args.next() orelse "";
const useZstd = std.mem.eql(u8, optionUseZstd, "--use-zstd");
if (oldFilePath.len == 0 or newFilePath.len == 0 or patchFilePath.len == 0) {
std.debug.print("Usage: bsdiff <oldFilePath> <newFilePath> <patchFilePath>\n", .{});
std.debug.print("Usage: bsdiff <oldFilePath> <newFilePath> <patchFilePath> --use-zstd\n", .{});
std.process.exit(1);
}
const oldFile = try std.fs.cwd().openFile(oldFilePath, .{ .mode = .read_only });
defer oldFile.close();
const oldFileSize = try oldFile.getEndPos();
const oldFileBuff = try allocator.alloc(u8, oldFileSize);
defer allocator.free(oldFileBuff);
_ = try oldFile.readAll(oldFileBuff);
const newFile = try std.fs.cwd().openFile(newFilePath, .{ .mode = .read_only });
defer newFile.close();
const newFileSize = try newFile.getEndPos();
const newFileBuff = try allocator.alloc(u8, newFileSize);
defer allocator.free(newFileBuff);
_ = try newFile.readAll(newFileBuff);
// Log system info and file sizes
const cpuCount = try std.Thread.getCpuCount();
const oldSizeMB = @as(f64, @floatFromInt(oldFileSize)) / (1024.0 * 1024.0);
const newSizeMB = @as(f64, @floatFromInt(newFileSize)) / (1024.0 * 1024.0);
std.debug.print("System Info:\n", .{});
std.debug.print(" CPUs: {d}\n", .{cpuCount});
std.debug.print(" Platform: {s}\n", .{@tagName(builtin.target.cpu.arch)});
std.debug.print(" SIMD vector size: {d} bytes ({s})\n", .{vectorSize, if (vectorSize > 1) "enabled" else "disabled"});
std.debug.print("\n", .{});
std.debug.print("Input Files:\n", .{});
std.debug.print(" File A (old): {d:.1} MB\n", .{oldSizeMB});
std.debug.print(" File B (new): {d:.1} MB\n", .{newSizeMB});
std.debug.print("\n", .{});
std.debug.print("Generating Patch file to turn File A into File B...", .{});
std.debug.print("\n", .{});
const patch = try calculateDifferences(&allocator, oldFileBuff, newFileBuff, useZstd);
// Write the patch file, internal blocks compressed with bzip2
const patchFile = try std.fs.cwd().createFile(patchFilePath, .{});
defer patchFile.close();
_ = try patchFile.writeAll(patch);
}
pub fn calculateDifferences(allocator: *std.mem.Allocator, oldData: []const u8, newData: []const u8, useZstd: bool) ![]u8 {
if (!useZstd) {
std.debug.print("Block compression with bzip2 not yet implemented.\n", .{});
}
// Allocate memory for the suffix array based on the length of the old data
const suffixIndexes = try allocator.alloc(i64, oldData.len + 1);
defer allocator.free(suffixIndexes);
std.debug.print("Building suffix array with libsais64...\n", .{});
const saisStart = std.time.milliTimestamp();
// Use libsais64 for fast suffix array construction (3-7x faster than qsufsort)
const oldDataLen: i64 = @intCast(oldData.len);
const result = libsais.zig_libsais64_wrapper(
oldData.ptr,
suffixIndexes.ptr,
oldDataLen,
);
if (result != 0) {
std.debug.print("libsais64 failed with error code: {d}\n", .{result});
return error.SuffixArrayConstructionFailed;
}
const saisTime = std.time.milliTimestamp() - saisStart;
const saisTimeSec = @as(f64, @floatFromInt(saisTime)) / 1000.0;
std.debug.print("Suffix array built in {d:.2}s\n", .{saisTimeSec});
// Add sentinel value at the end (used by the original algorithm)
suffixIndexes[oldData.len] = @intCast(oldData.len);
const newsize = newData.len;
// Timing for diff phase
const diffPhaseStart = std.time.milliTimestamp();
// Determine number of chunks based on CPU count
const cpuCount = std.Thread.getCpuCount() catch 4;
// Use all available CPUs, but ensure chunks are at least 1MB to avoid excessive overhead
const minChunkSize: usize = 1024 * 1024; // 1MB minimum
const maxChunksBySize = newsize / minChunkSize;
const numChunks = @max(1, @min(cpuCount, maxChunksBySize));
std.debug.print("Parallel diff with {d} chunks on {d} CPUs...\n", .{ numChunks, cpuCount });
// Calculate chunk boundaries
const chunkSize = (newsize + numChunks - 1) / numChunks;
// Allocate results array and threads array
var chunkResults = try allocator.alloc(ChunkResult, numChunks);
defer allocator.free(chunkResults);
// Track completed chunks for progress reporting
var completedChunks: usize = 0;
var threads = try allocator.alloc(std.Thread, numChunks);
defer allocator.free(threads);
// Spawn threads for each chunk
for (0..numChunks) |i| {
const chunkStart = i * chunkSize;
const nominalEnd = @min((i + 1) * chunkSize, newsize);
threads[i] = try std.Thread.spawn(.{}, processChunkThread, .{
&chunkResults[i],
allocator.*,
suffixIndexes,
oldData,
newData,
chunkStart,
nominalEnd,
&completedChunks,
});
}
// Start a progress reporting thread
var progressRunning: bool = true;
const progressStart = std.time.milliTimestamp();
const progressThread = try std.Thread.spawn(.{}, reportDiffProgress, .{
&progressRunning,
&completedChunks,
numChunks,
progressStart,
});
// Wait for all chunk threads to complete
for (threads) |thread| {
thread.join();
}
// Stop the progress thread
@atomicStore(bool, &progressRunning, false, .seq_cst);
progressThread.join();
// Report diff phase timing
const diffPhaseTime = std.time.milliTimestamp() - diffPhaseStart;
const diffPhaseSec = @as(f64, @floatFromInt(diffPhaseTime)) / 1000.0;
std.debug.print("Diff phase: {d:.2}s (parallel)\n", .{diffPhaseSec});
// Merge chunk results, handling overlaps
// Strategy: Skip all entries from chunk N+1 that overlap with chunk N's actual end.
// If there's a gap (chunk N+1's first kept entry starts after chunk N's end),
// fill it with extra data copied directly from newData.
// First pass: calculate skip offsets and gaps for each chunk
const ChunkMergeInfo = struct {
controlSkip: usize, // bytes to skip in control data
diffSkip: usize, // bytes to skip in diff data
extraSkip: usize, // bytes to skip in extra data
gapStart: usize, // where the gap starts in newData (= prevActualEnd)
gapEnd: usize, // where the gap ends (= first kept entry's start position)
firstKeptEntryStart: usize, // newData position where first kept entry starts
};
var chunkMergeInfo = try allocator.alloc(ChunkMergeInfo, numChunks);
defer allocator.free(chunkMergeInfo);
var totalControlLen: usize = 0;
var totalDiffLen: usize = 0;
var totalExtraLen: usize = 0;
var totalGapBytes: usize = 0;
// Track the maximum coverage seen so far (not just previous chunk's end)
// This is critical because chunk N might extend further than chunk N+1
var maxActualEndSoFar: usize = 0;
for (0..numChunks) |i| {
if (i == 0) {
// First chunk: no overlap possible
chunkMergeInfo[i] = .{
.controlSkip = 0,
.diffSkip = 0,
.extraSkip = 0,
.gapStart = 0,
.gapEnd = 0,
.firstKeptEntryStart = chunkResults[i].startPos,
};
totalControlLen += chunkResults[i].controlLen;
totalDiffLen += chunkResults[i].diffLen;
totalExtraLen += chunkResults[i].extraLen;
maxActualEndSoFar = chunkResults[i].actualEndPos;
} else {
// Use the maximum coverage seen so far, not just the previous chunk's end
// This handles cases where chunk N extends further than chunks N+1, N+2, etc.
const prevActualEnd = maxActualEndSoFar;
const thisStart = chunkResults[i].startPos;
if (prevActualEnd <= thisStart) {
// No overlap - but there might be a gap
chunkMergeInfo[i] = .{
.controlSkip = 0,
.diffSkip = 0,
.extraSkip = 0,
.gapStart = prevActualEnd,
.gapEnd = thisStart,
.firstKeptEntryStart = thisStart,
};
const gapSize = thisStart - prevActualEnd;
totalGapBytes += gapSize;
totalControlLen += chunkResults[i].controlLen;
totalDiffLen += chunkResults[i].diffLen;
totalExtraLen += chunkResults[i].extraLen;
} else {
// Overlap: skip entries until we're past prevActualEnd
var newDataCovered: usize = thisStart;
var controlOffset: usize = 0;
var diffOffset: usize = 0;
var extraOffset: usize = 0;
while (controlOffset < chunkResults[i].controlLen) {
const forwardLen = offtin(chunkResults[i].controlData[controlOffset..][0..8]);
const extraLen = offtin(chunkResults[i].controlData[controlOffset + 8 ..][0..16][0..8]);
const entryEnd = newDataCovered + @as(usize, @intCast(forwardLen + extraLen));
if (entryEnd <= prevActualEnd) {
// Entry fully within overlap - skip it
newDataCovered = entryEnd;
controlOffset += 24;
diffOffset += @intCast(forwardLen);
extraOffset += @intCast(extraLen);
} else if (newDataCovered >= prevActualEnd) {
// Entry starts at or after overlap - keep it
break;
} else {
// Entry crosses the boundary - skip it entirely
// The gap will be filled with extra data
newDataCovered = entryEnd;
controlOffset += 24;
diffOffset += @intCast(forwardLen);
extraOffset += @intCast(extraLen);
}
}
// Calculate gap: from prevActualEnd to where we are now
const gapSize = if (newDataCovered > prevActualEnd) newDataCovered - prevActualEnd else 0;
chunkMergeInfo[i] = .{
.controlSkip = controlOffset,
.diffSkip = diffOffset,
.extraSkip = extraOffset,
.gapStart = prevActualEnd,
.gapEnd = newDataCovered,
.firstKeptEntryStart = newDataCovered,
};
totalGapBytes += gapSize;
// Use saturating subtraction to handle edge cases where all entries are skipped
const controlKept = if (chunkResults[i].controlLen > controlOffset) chunkResults[i].controlLen - controlOffset else 0;
const diffKept = if (chunkResults[i].diffLen > diffOffset) chunkResults[i].diffLen - diffOffset else 0;
const extraKept = if (chunkResults[i].extraLen > extraOffset) chunkResults[i].extraLen - extraOffset else 0;
totalControlLen += controlKept;
totalDiffLen += diffKept;
totalExtraLen += extraKept;
}
// Update max coverage tracking
maxActualEndSoFar = @max(maxActualEndSoFar, chunkResults[i].actualEndPos);
}
}
// Account for gap fill entries: each gap needs one control entry (24 bytes) and extra data
const numGaps = blk: {
var count: usize = 0;
for (0..numChunks) |i| {
if (chunkMergeInfo[i].gapEnd > chunkMergeInfo[i].gapStart) count += 1;
}
break :blk count;
};
totalControlLen += numGaps * 24;
totalExtraLen += totalGapBytes;
// Account for seek-only entries: each chunk after the first with kept entries needs one
// to adjust oldpos before reading the first kept entry's diff
const numSeekEntries = blk: {
var count: usize = 0;
for (1..numChunks) |i| {
if (chunkResults[i].controlLen > chunkMergeInfo[i].controlSkip) count += 1;
}
break :blk count;
};
totalControlLen += numSeekEntries * 24;
// Allocate merged buffers
var controlBlockStream = try allocator.alloc(u8, @max(totalControlLen, 64 * 1024));
var diffBlockStream = try allocator.alloc(u8, @max(totalDiffLen, 1024));
var extraBlockStream = try allocator.alloc(u8, @max(totalExtraLen + totalGapBytes, 1024));
// Copy data from chunks, inserting gap-fill entries where needed
// Track oldpos for adjusting seekBy values at chunk boundaries
var controlOffset: usize = 0;
var diffOffset: usize = 0;
var extraOffset: usize = 0;
var prevChunkEndOldpos: i64 = 0;
for (0..numChunks) |i| {
const info = chunkMergeInfo[i];
// If there's a gap, insert a control entry to fill it with extra data
// Clamp gap to valid range within newData
const clampedGapEnd = @min(info.gapEnd, newsize);
const clampedGapStart = @min(info.gapStart, clampedGapEnd);
const actualGapSize = clampedGapEnd - clampedGapStart;
if (actualGapSize > 0) {
// Control entry: diffBy=0, extraBy=gapSize, seekBy=0
// This tells bspatch to copy gapSize bytes from extra block to output
// Note: oldpos doesn't change (seekBy=0, diffBy=0)
offtout(0, controlBlockStream[controlOffset..][0..8]); // diffBy = 0
controlOffset += 8;
offtout(@intCast(actualGapSize), controlBlockStream[controlOffset..][0..8]); // extraBy = gapSize
controlOffset += 8;
offtout(0, controlBlockStream[controlOffset..][0..8]); // seekBy = 0
controlOffset += 8;
// Copy the gap bytes from newData to extra block
@memcpy(extraBlockStream[extraOffset..][0..actualGapSize], newData[clampedGapStart..clampedGapEnd]);
extraOffset += actualGapSize;
}
// Calculate skippedOldposDelta: how much oldpos would have moved through skipped entries
var skippedOldposDelta: i64 = 0;
var ctrlPos: usize = 0;
while (ctrlPos < info.controlSkip) {
const forwardLen = offtin(chunkResults[i].controlData[ctrlPos..][0..8]);
const seekByVal = offtin(chunkResults[i].controlData[ctrlPos + 16 ..][0..8]);
skippedOldposDelta += forwardLen + seekByVal;
ctrlPos += 24;
}
// Copy this chunk's remaining control/diff/extra data
const controlToCopy = chunkResults[i].controlLen - info.controlSkip;
const diffToCopy = chunkResults[i].diffLen - info.diffSkip;
const extraToCopy = chunkResults[i].extraLen - info.extraSkip;
if (controlToCopy > 0) {
// For chunks after the first, insert a "seek-only" entry to adjust oldpos
// The diff data was computed using the chunk's internal oldpos tracking,
// so we need oldpos to be at skippedOldposDelta before reading the first entry
if (i > 0) {
const seekAdjustment = skippedOldposDelta - prevChunkEndOldpos;
offtout(0, controlBlockStream[controlOffset..][0..8]); // diffBy = 0
controlOffset += 8;
offtout(0, controlBlockStream[controlOffset..][0..8]); // extraBy = 0
controlOffset += 8;
offtout(seekAdjustment, controlBlockStream[controlOffset..][0..8]); // seekBy = adjustment
controlOffset += 8;
// After this entry: oldpos = prevChunkEndOldpos + 0 + seekAdjustment = skippedOldposDelta
prevChunkEndOldpos = skippedOldposDelta;
}
// Copy the chunk's kept entries unchanged (no seekBy adjustment needed)
@memcpy(controlBlockStream[controlOffset..][0..controlToCopy], chunkResults[i].controlData[info.controlSkip..][0..controlToCopy]);
// Calculate where oldpos ends after this chunk's remaining entries
var chunkOldposDelta: i64 = 0;
var pos: usize = info.controlSkip;
while (pos < chunkResults[i].controlLen) {
const forwardLen = offtin(chunkResults[i].controlData[pos..][0..8]);
const seekByVal = offtin(chunkResults[i].controlData[pos + 16 ..][0..8]);
chunkOldposDelta += forwardLen + seekByVal;
pos += 24;
}
prevChunkEndOldpos += chunkOldposDelta;
controlOffset += controlToCopy;
}
if (diffToCopy > 0) {
@memcpy(diffBlockStream[diffOffset..][0..diffToCopy], chunkResults[i].diffData[info.diffSkip..][0..diffToCopy]);
diffOffset += diffToCopy;
}
if (extraToCopy > 0) {
@memcpy(extraBlockStream[extraOffset..][0..extraToCopy], chunkResults[i].extraData[info.extraSkip..][0..extraToCopy]);
extraOffset += extraToCopy;
}
// Free chunk data
allocator.free(chunkResults[i].controlData);
allocator.free(chunkResults[i].diffData);
allocator.free(chunkResults[i].extraData);
}
// Now compress the merged data
std.debug.print("Compressing...\n", .{});
const compressStart = std.time.milliTimestamp();
var streamingBytes = true;
var controlBlockInput = zstd.ZSTD_inBuffer{ .src = controlBlockStream.ptr, .size = controlOffset, .pos = 0 };
const controlBlockCompressed = try allocator.alloc(u8, zstd.ZSTD_compressBound(controlOffset));
var controlBlockOutput = zstd.ZSTD_outBuffer{ .dst = controlBlockCompressed.ptr, .size = controlBlockCompressed.len, .pos = 0 };
const controlBlockThread = try std.Thread.spawn(.{}, compressBlockStream, .{ &controlBlockInput, &controlBlockOutput, &streamingBytes });
var diffBlockInput = zstd.ZSTD_inBuffer{ .src = diffBlockStream.ptr, .size = diffOffset, .pos = 0 };
const diffBlockCompressed = try allocator.alloc(u8, zstd.ZSTD_compressBound(diffOffset));
var diffBlockOutput = zstd.ZSTD_outBuffer{ .dst = diffBlockCompressed.ptr, .size = diffBlockCompressed.len, .pos = 0 };
const diffBlockThread = try std.Thread.spawn(.{}, compressBlockStream, .{ &diffBlockInput, &diffBlockOutput, &streamingBytes });
var extraBlockInput = zstd.ZSTD_inBuffer{ .src = extraBlockStream.ptr, .size = extraOffset, .pos = 0 };
const extraBlockCompressed = try allocator.alloc(u8, zstd.ZSTD_compressBound(extraOffset));
var extraBlockOutput = zstd.ZSTD_outBuffer{ .dst = extraBlockCompressed.ptr, .size = extraBlockCompressed.len, .pos = 0 };
const extraBlockThread = try std.Thread.spawn(.{}, compressBlockStream, .{ &extraBlockInput, &extraBlockOutput, &streamingBytes });
// Wait a bit then signal completion
std.time.sleep(std.time.ns_per_ms * 10);
streamingBytes = false;
controlBlockThread.join();
diffBlockThread.join();
extraBlockThread.join();
const compressTime = std.time.milliTimestamp() - compressStart;
const compressTimeSec = @as(f64, @floatFromInt(compressTime)) / 1000.0;
std.debug.print("Compression: {d:.2}s\n", .{compressTimeSec});
// Header is
// 0 8 "BSDIFF40" or "TRDIFF10"
// 8 8 length of ctrl block i64
// 16 8 length of diff block i64
// 24 8 length of new file i64
// Placeholder for the buffer used in offtout
var buffer = [_]u8{0} ** 8;
// Combine header, diffBlock, and extraBlock into a single byte slice to return
const headerLength = 32;
var patch = try allocator.alloc(u8, headerLength + controlBlockOutput.pos + diffBlockOutput.pos + extraBlockOutput.pos);
// write the header compressed
@memcpy(patch[0..8], "TRDIFF10");
offtout(@intCast(controlBlockOutput.pos), &buffer);
@memcpy(patch[8..16], &buffer);
offtout(@intCast(diffBlockOutput.pos), &buffer);
@memcpy(patch[16..24], &buffer);
offtout(@intCast(newsize), &buffer);
@memcpy(patch[24..32], &buffer);
var patchFileOffset: usize = headerLength;
@memcpy(patch[patchFileOffset..][0..controlBlockOutput.pos], controlBlockCompressed.ptr);
patchFileOffset += controlBlockOutput.pos;
@memcpy(patch[patchFileOffset..][0..diffBlockOutput.pos], diffBlockCompressed.ptr);
patchFileOffset += diffBlockOutput.pos;
@memcpy(patch[patchFileOffset..][0..extraBlockOutput.pos], extraBlockCompressed.ptr);
patchFileOffset += extraBlockOutput.pos;
const patchSizeKB = @as(f64, @floatFromInt(patch.len)) / 1024.0;
const compressionRatio = (@as(f64, @floatFromInt(patch.len)) / @as(f64, @floatFromInt(newData.len))) * 100.0;
std.debug.print("Completed - Patch: {d:.2} KB ({d:.1}% of new size)\n", .{ patchSizeKB, compressionRatio });
return patch;
}
/// Process a chunk of the new file, finding matches in the old file.
/// If a match extends past the nominal boundary, we continue until the match ends.
fn processChunk(
allocator: std.mem.Allocator,
suffixIndexes: []i64,
oldData: []const u8,
newData: []const u8,
chunkStart: usize,
nominalEnd: usize,
) !ChunkResult {
const newsize = newData.len;
const oldsize = oldData.len;
// Allocate buffers for this chunk's output
// Chunks can extend significantly past their boundary if there's a long match
// Allocate generously - up to the full remaining file size in worst case
const remainingSize = newData.len - chunkStart;
const maxControlSize = @max((remainingSize / 8) * 24, 64 * 1024); // Rough estimate
var controlData = try allocator.alloc(u8, maxControlSize);
var controlLen: usize = 0;
var diffData = try allocator.alloc(u8, remainingSize);
var diffLen: usize = 0;
var extraData = try allocator.alloc(u8, remainingSize);
var extraLen: usize = 0;
// Initialize variables for tracking positions and scores
var scanIndex: i64 = @intCast(chunkStart);
var matchLength: i64 = 0;
var lastScanIndex: i64 = @intCast(chunkStart);
var lastMatchPosition: i64 = 0;
var lastOffset: i64 = 0;
var matchScore: i64 = 0;
var matchPosition: i64 = 0;
var scoreCounter: i64 = 0;
var forwardScore: i64 = 0;
var forwardLength: i64 = 0;
var backwardScore: i64 = 0;
var backwardLength: i64 = 0;
var overlapLength: i64 = 0;
var bestOverlapScore: i64 = 0;
var bestOverlapLength: i64 = 0;
// Track if we're in the middle of a match when we hit the boundary
var actualEndPos: usize = chunkStart;
// Main diff loop for this chunk
while (scanIndex < newsize) {
matchScore = 0;
scanIndex += matchLength;
scoreCounter = scanIndex;
// Search for matches
while (scanIndex < newsize) {
matchLength = @intCast(searchWithLCP(suffixIndexes, oldData, newData[@intCast(scanIndex)..], 0, @intCast(oldsize), &matchPosition));
while (scoreCounter < scanIndex + matchLength) {
const currentScanPos = scoreCounter + lastOffset;
if (currentScanPos >= 0 and currentScanPos < oldsize and oldData[@intCast(currentScanPos)] == newData[@intCast(scoreCounter)]) {
matchScore += 1;
}
scoreCounter += 1;
}
if (matchLength == matchScore and matchLength != 0) {
break;
}
if (matchLength > matchScore + 8) {
break;
}
if (scanIndex + lastOffset >= 0 and scanIndex + lastOffset < oldsize and oldData[@intCast(scanIndex + lastOffset)] == newData[@intCast(scanIndex)]) {
matchScore -= 1;
}
scanIndex += 1;
}
// Process the match
if (matchLength != matchScore or scanIndex >= newsize) {
scoreCounter = 0;
forwardScore = 0;
forwardLength = 0;
var i: i64 = 0;
// Calculate forward length
// When scanIndex >= newsize, we need to clamp to the actual file boundary
// to avoid creating control entries that extend past the new file
const effectiveScanIndex = @min(scanIndex, @as(i64, @intCast(newsize)));
while (lastScanIndex + i < effectiveScanIndex and lastMatchPosition + i < oldsize) {
if (oldData[@intCast(lastMatchPosition + i)] == newData[@intCast(lastScanIndex + i)]) {
scoreCounter += 1;
}
i += 1;
if (scoreCounter * 2 - i > forwardScore * 2 - forwardLength) {
forwardScore = scoreCounter;
forwardLength = i;
}
}
// Calculate backward length
backwardLength = 0;
if (scanIndex < newsize) {
scoreCounter = 0;
backwardScore = 0;
i = 1;
while (scanIndex >= lastScanIndex + i and matchPosition >= i) {
if (oldData[@intCast(matchPosition - i)] == newData[@intCast(scanIndex - i)]) {
scoreCounter += 1;
}
if (scoreCounter * 2 - i > backwardScore * 2 - backwardLength) {
backwardScore = scoreCounter;
backwardLength = i;
}
i += 1;
}
}
// Handle overlaps
if (lastScanIndex + forwardLength > scanIndex - backwardLength) {
overlapLength = (lastScanIndex + forwardLength) - (scanIndex - backwardLength);
scoreCounter = 0;
bestOverlapScore = 0;
bestOverlapLength = 0;
i = 0;
while (i < overlapLength) {
if (newData[@intCast(lastScanIndex + forwardLength - overlapLength + i)] == oldData[@intCast(lastMatchPosition + forwardLength - overlapLength + i)]) {
scoreCounter += 1;
}
if (newData[@intCast(scanIndex - backwardLength + i)] == oldData[@intCast(matchPosition - backwardLength + i)]) {
scoreCounter -= 1;
}
if (scoreCounter > bestOverlapScore) {
bestOverlapScore = scoreCounter;
bestOverlapLength = i + 1;
}
i += 1;
}
forwardLength += bestOverlapLength - overlapLength;
backwardLength -= bestOverlapLength;
}
// Write diff data
i = 0;
while (i < forwardLength) {
if (i + vectorSize <= forwardLength) {
const oldpos: usize = @intCast(lastMatchPosition + i);
const newpos: usize = @intCast(lastScanIndex + i);
const newVec: @Vector(vectorSize, u8) = newData[newpos..][0..vectorSize].*;
const oldVec: @Vector(vectorSize, u8) = oldData[oldpos..][0..vectorSize].*;
const resultVec = @subWithOverflow(newVec, oldVec)[0];
const resultArray: [vectorSize]u8 = resultVec;
@memcpy(diffData[diffLen + @as(usize, @intCast(i)) ..][0..vectorSize], &resultArray);
i += vectorSize;
continue;
}
const newByte: u8 = newData[@intCast(lastScanIndex + i)];
const oldByte: u8 = oldData[@intCast(lastMatchPosition + i)];
diffData[diffLen + @as(usize, @intCast(i))] = @subWithOverflow(newByte, oldByte)[0];
i += 1;
}
// Write extra data
const newBytesAdded = (scanIndex - backwardLength) - (lastScanIndex + forwardLength);
i = 0;
while (i < newBytesAdded) {
extraData[extraLen + @as(usize, @intCast(i))] = newData[@intCast(lastScanIndex + forwardLength + i)];
i += 1;
}
// Write control block
const readDiffBy = forwardLength;
const readExtraBy = newBytesAdded;
const seekBy = (matchPosition - backwardLength) - (lastMatchPosition + forwardLength);
offtout(readDiffBy, controlData[controlLen..][0..8]);
controlLen += 8;
offtout(readExtraBy, controlData[controlLen..][0..8]);
controlLen += 8;
offtout(seekBy, controlData[controlLen..][0..8]);
controlLen += 8;
diffLen += @intCast(readDiffBy);
extraLen += @intCast(readExtraBy);
// Update positions
lastScanIndex = scanIndex - backwardLength;
lastMatchPosition = matchPosition - backwardLength;
lastOffset = matchPosition - scanIndex;
// Update actual end position
actualEndPos = @intCast(lastScanIndex);
// Check if we've passed the nominal boundary after completing this match
// We stop once we've finished a match that ends past the boundary
if (@as(usize, @intCast(lastScanIndex)) >= nominalEnd) {
break;
}
}
}
return ChunkResult{
.controlData = controlData,
.controlLen = controlLen,
.diffData = diffData,
.diffLen = diffLen,
.extraData = extraData,
.extraLen = extraLen,
.actualEndPos = actualEndPos,
.startPos = chunkStart,
};
}
/// Progress reporting thread for parallel diff
fn reportDiffProgress(
running: *bool,
completedChunks: *usize,
numChunks: usize,
startTime: i64,
) void {
var lastPrintTime: i64 = startTime;
while (@atomicLoad(bool, running, .seq_cst)) {
std.time.sleep(std.time.ns_per_s * 1); // Check every second
const now = std.time.milliTimestamp();
const elapsed = now - startTime;
const elapsedSec = @as(f64, @floatFromInt(elapsed)) / 1000.0;
const timeSinceLastPrint = now - lastPrintTime;
// Print every 10 seconds if still running
if (timeSinceLastPrint >= 10000) {
const completed = @atomicLoad(usize, completedChunks, .seq_cst);
if (completed < numChunks) {
const percent = (@as(f64, @floatFromInt(completed)) / @as(f64, @floatFromInt(numChunks))) * 100.0;
std.debug.print("Diffing... {d}/{d} chunks complete ({d:.0}%) - {d:.0}s elapsed\n", .{ completed, numChunks, percent, elapsedSec });
lastPrintTime = now;
}
}
}
}
/// Thread wrapper for processChunk
fn processChunkThread(
result: *ChunkResult,
allocator: std.mem.Allocator,
suffixIndexes: []i64,
oldData: []const u8,
newData: []const u8,
chunkStart: usize,
nominalEnd: usize,
completedChunks: *usize,
) void {
result.* = processChunk(allocator, suffixIndexes, oldData, newData, chunkStart, nominalEnd) catch |err| {
std.debug.print("Chunk processing error: {}\n", .{err});
_ = @atomicRmw(usize, completedChunks, .Add, 1, .seq_cst);
return;
};
_ = @atomicRmw(usize, completedChunks, .Add, 1, .seq_cst);
}
var totalCompressedSize: usize = 0;
// Notes on Zstd:
// compressed output between single shot and streaming with a single frame is roughly the same.
// with a single frame zstd takes roughly 20% longer to compress the same data. but with streaming you can compress in parallel to the actual diffing in another thread.
fn compressBlock(allocator: *std.mem.Allocator, block: []const u8) !void {
// non-streaming
const maxCompressedSize = zstd.ZSTD_compressBound(block.len);
const compressedBlock = try allocator.alloc(u8, maxCompressedSize);
// defer allocator.free(compressedBlock);
const compressedSize = zstd.ZSTD_compress(compressedBlock.ptr, compressedBlock.len, block.ptr, block.len, 22);
totalCompressedSize += compressedSize;
// return compressedSize;
// return compressedBlock[0..compressedSize];
}
fn compressBlockStream(input: *zstd.ZSTD_inBuffer, output: *zstd.ZSTD_outBuffer, streamingBytes: *bool) !void {
const cstream = zstd.ZSTD_createCStream();
defer {
_ = zstd.ZSTD_freeCStream(cstream);
}
// Note: Experimentally there's no significant difference between 19 and 22 with regard to compression output
// creates smaller output at 19 than bzip2. However there is a huge time cost between 19 and 22, roughly 20% longer.
// setting this to 22 the total bsdiff time matches the original bsdiff c implementation
// setting this to 19 the total bsdiff time is roughly 20% faster than the original bsdiff c implementation
// in both cases the output is roughly 10% smaller.
// obviously this will vary based on the data being compressed.
_ = zstd.ZSTD_initCStream(cstream, 19);
while (streamingBytes.* or input.pos < input.size) {
if (input.pos < input.size) {
_ = zstd.ZSTD_compressStream(cstream, output, input);
} else {
std.time.sleep(std.time.ns_per_ms * 10);
}
}
_ = zstd.ZSTD_endStream(cstream, output);
}
// offtin reads an int64 (little endian) from buf
fn offtin(buf: []const u8) i64 {
var y: u64 = @as(u64, buf[0]);
y |= @as(u64, buf[1]) << 8;
y |= @as(u64, buf[2]) << 16;
y |= @as(u64, buf[3]) << 24;
y |= @as(u64, buf[4]) << 32;
y |= @as(u64, buf[5]) << 40;
y |= @as(u64, buf[6]) << 48;
y |= @as(u64, buf[7]) << 56;
if (y & 0x8000000000000000 != 0) {
return -@as(i64, @bitCast(y & 0x7FFFFFFFFFFFFFFF));
}
return @as(i64, @bitCast(y));
}
// offtout puts an int64 (little endian) to buf
fn offtout(x: i64, buf: []u8) void {
var y: u64 = undefined;
if (x < 0) {
y = @as(u64, @bitCast(-x)) | 0x8000000000000000;
} else {
y = @as(u64, @bitCast(x));
}
buf[0] = @intCast(y & 0xFF);
buf[1] = @intCast((y >> 8) & 0xFF);
buf[2] = @intCast((y >> 16) & 0xFF);
buf[3] = @intCast((y >> 24) & 0xFF);
buf[4] = @intCast((y >> 32) & 0xFF);
buf[5] = @intCast((y >> 40) & 0xFF);
buf[6] = @intCast((y >> 48) & 0xFF);
buf[7] = @intCast((y >> 56) & 0xFF);
}
/// Boundary-tracking binary search to find the longest match.
/// By tracking match lengths at boundaries, we skip redundant comparisons,
/// achieving O(m + log n) instead of O(m * log n).
fn searchWithLCP(suffixIndexes: []i64, oldData: []const u8, newData: []const u8, from: usize, to: usize, bestMatchPosition: *i64) usize {
// Use iterative approach with LCP acceleration
var lo: usize = from;
var hi: usize = to;
var loMatch: usize = 0; // chars matching at lo boundary
var hiMatch: usize = 0; // chars matching at hi boundary
const oldDataSize = oldData.len;
const newDataSize = newData.len;
// Initial match lengths at boundaries
loMatch = matchlenFast(oldData[@intCast(suffixIndexes[lo])..], newData);
hiMatch = matchlenFast(oldData[@intCast(suffixIndexes[hi])..], newData);
var bestMatch: usize = loMatch;
bestMatchPosition.* = suffixIndexes[lo];
if (hiMatch > bestMatch) {
bestMatch = hiMatch;
bestMatchPosition.* = suffixIndexes[hi];
}
while (hi - lo > 1) {
const mid = lo + (hi - lo) / 2;
const midSuffixPos: usize = @intCast(suffixIndexes[mid]);
// Start comparison from the minimum of the two boundary matches
// This is the key LCP optimization - we know at least this many chars must match
const skipLen = @min(loMatch, hiMatch);
// Compare starting from skipLen
const compareLen = @min(oldDataSize - midSuffixPos, newDataSize);
var midMatch: usize = skipLen;
// Continue matching from skipLen
if (skipLen < compareLen) {
midMatch += matchlenFrom(oldData[midSuffixPos + skipLen ..], newData[skipLen..]);
}
// Update best match if this is better
if (midMatch > bestMatch) {
bestMatch = midMatch;
bestMatchPosition.* = suffixIndexes[mid];
}
// Decide which half to search based on lexicographic comparison
if (midMatch < compareLen and midMatch < newDataSize) {
// We stopped matching at position midMatch
if (midSuffixPos + midMatch < oldDataSize and oldData[midSuffixPos + midMatch] < newData[midMatch]) {
// Suffix at mid is less than query, search right half
lo = mid;
loMatch = midMatch;
} else {
// Suffix at mid is greater than query, search left half
hi = mid;
hiMatch = midMatch;
}
} else {
// Full match up to the limit, decide based on lengths
if (compareLen <= newDataSize) {
// Need longer suffixes, search left (smaller indices = longer suffixes in sorted order when equal prefix)
hi = mid;
hiMatch = midMatch;
} else {
lo = mid;
loMatch = midMatch;
}
}
}
return bestMatch;
}
/// Helper function: match length starting from an offset (for LCP-accelerated search)
fn matchlenFrom(oldData: []const u8, newData: []const u8) usize {
const minSize = @min(oldData.len, newData.len);