255115ee
Refine instance upload planning contract
a73x 2026-04-08 18:50
Commit message
src/renderer.zig
| Old | New | ||
|---|---|---|---|
| @@ -319,19 +319,27 @@ const InstanceUploadRequest = struct { | |||
| 319 | write_len: u32, | 319 | write_len: u32, |
| 320 | }; | 320 | }; |
| 321 | 321 | ||
| 322 | const InstanceUploadMode = enum { | ||
| 323 | partial, | ||
| 324 | full, | ||
| 325 | invalid_range, | ||
| 326 | }; | ||
| 327 | |||
| 322 | const InstanceUploadDecision = struct { | 328 | const InstanceUploadDecision = struct { |
| 323 | needed_capacity: u32, | 329 | needed_capacity: ?u32, |
| 324 | needs_growth: bool, | 330 | upload_mode: InstanceUploadMode, |
| 325 | force_full_upload: bool, | ||
| 326 | }; | 331 | }; |
| 327 | 332 | ||
| 328 | fn planInstanceUpload(req: InstanceUploadRequest) InstanceUploadDecision { | 333 | fn planInstanceUpload(req: InstanceUploadRequest) InstanceUploadDecision { |
| 329 | const needed_capacity = std.math.add(u32, req.offset_instances, req.write_len) catch std.math.maxInt(u32); | 334 | const needed_capacity = std.math.add(u32, req.offset_instances, req.write_len) catch { |
| 330 | const needs_growth = needed_capacity > req.current_capacity; | 335 | return .{ |
| 336 | .needed_capacity = null, | ||
| 337 | .upload_mode = .invalid_range, | ||
| 338 | }; | ||
| 339 | }; | ||
| 331 | return .{ | 340 | return .{ |
| 332 | .needed_capacity = needed_capacity, | 341 | .needed_capacity = needed_capacity, |
| 333 | .needs_growth = needs_growth, | 342 | .upload_mode = if (needed_capacity > req.current_capacity) .full else .partial, |
| 334 | .force_full_upload = needs_growth, | ||
| 335 | }; | 343 | }; |
| 336 | } | 344 | } |
| 337 | 345 | ||
| @@ -1375,8 +1383,8 @@ test "range upload falls back to full upload when capacity must grow" { | |||
| 1375 | .write_len = 4, | 1383 | .write_len = 4, |
| 1376 | }); | 1384 | }); |
| 1377 | 1385 | ||
| 1378 | try std.testing.expect(decision.needs_growth); | 1386 | try std.testing.expectEqual(@as(?u32, 10), decision.needed_capacity); |
| 1379 | try std.testing.expect(decision.force_full_upload); | 1387 | try std.testing.expectEqual(InstanceUploadMode.full, decision.upload_mode); |
| 1380 | } | 1388 | } |
| 1381 | 1389 | ||
| 1382 | test "range upload stays partial when capacity is sufficient" { | 1390 | test "range upload stays partial when capacity is sufficient" { |
| @@ -1386,6 +1394,39 @@ test "range upload stays partial when capacity is sufficient" { | |||
| 1386 | .write_len = 3, | 1394 | .write_len = 3, |
| 1387 | }); | 1395 | }); |
| 1388 | 1396 | ||
| 1389 | try std.testing.expect(!decision.needs_growth); | 1397 | try std.testing.expectEqual(@as(?u32, 7), decision.needed_capacity); |
| 1390 | try std.testing.expect(!decision.force_full_upload); | 1398 | try std.testing.expectEqual(InstanceUploadMode.partial, decision.upload_mode); |
| 1399 | } | ||
| 1400 | |||
| 1401 | test "range upload stays partial on an exact fit" { | ||
| 1402 | const decision = planInstanceUpload(.{ | ||
| 1403 | .current_capacity = 7, | ||
| 1404 | .offset_instances = 4, | ||
| 1405 | .write_len = 3, | ||
| 1406 | }); | ||
| 1407 | |||
| 1408 | try std.testing.expectEqual(@as(?u32, 7), decision.needed_capacity); | ||
| 1409 | try std.testing.expectEqual(InstanceUploadMode.partial, decision.upload_mode); | ||
| 1410 | } | ||
| 1411 | |||
| 1412 | test "range upload allows zero-length writes without forcing growth" { | ||
| 1413 | const decision = planInstanceUpload(.{ | ||
| 1414 | .current_capacity = 8, | ||
| 1415 | .offset_instances = 8, | ||
| 1416 | .write_len = 0, | ||
| 1417 | }); | ||
| 1418 | |||
| 1419 | try std.testing.expectEqual(@as(?u32, 8), decision.needed_capacity); | ||
| 1420 | try std.testing.expectEqual(InstanceUploadMode.partial, decision.upload_mode); | ||
| 1421 | } | ||
| 1422 | |||
| 1423 | test "range upload reports overflow explicitly" { | ||
| 1424 | const decision = planInstanceUpload(.{ | ||
| 1425 | .current_capacity = std.math.maxInt(u32), | ||
| 1426 | .offset_instances = std.math.maxInt(u32), | ||
| 1427 | .write_len = 1, | ||
| 1428 | }); | ||
| 1429 | |||
| 1430 | try std.testing.expectEqual(@as(?u32, null), decision.needed_capacity); | ||
| 1431 | try std.testing.expectEqual(InstanceUploadMode.invalid_range, decision.upload_mode); | ||
| 1391 | } | 1432 | } |