Skip to content

Make unsafe borrow checking O(1) via active-borrow tracking#97

Merged
DanexCodr merged 2 commits into
mainfrom
copilot/improve-borrow-checker-zero-cost
Apr 13, 2026
Merged

Make unsafe borrow checking O(1) via active-borrow tracking#97
DanexCodr merged 2 commits into
mainfrom
copilot/improve-borrow-checker-zero-cost

Conversation

Copilot AI commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

Borrow mutation checks in unsafe code were doing dynamic scans across locals/fields/slots, making enforcement cost scale with context size. This change moves borrow checking to constant-time lookups while preserving current pointer-safety behavior.

  • Core change: replace scan-based checks with indexed borrow state

    • Added active-borrow index in ExecutionContext keyed by container identity + element index.
    • AssignmentHandler.ensureNoActiveBorrow(...) now queries ctx.hasActiveBorrow(container, index) instead of traversing runtime maps/lists.
  • Tracking lifecycle wired into all relevant mutation paths

    • Added tracked replacement flow so borrow counts stay accurate when values move or are overwritten.
    • Integrated tracking updates into:
      • local variable set/remove/scope pop,
      • slot writes,
      • object field writes (via new setObjectField path),
      • indexed container writes where replaced values may contain nested pointers.
  • Assignment path cleanup for consistency

    • Updated field and scoped-variable assignment sites to use context-managed write APIs, ensuring borrow tracking and state mutation remain coupled.
private void ensureNoActiveBorrow(Object container, long index, ExecutionContext ctx) {
    if (!isBorrowCheckerActive(ctx)) return;
    if (ctx.hasActiveBorrow(container, index)) {
        throwBorrowMutationViolation(index);
    }
}

Copilot AI and others added 2 commits April 13, 2026 04:55
Agent-Logs-Url: https://github.com/DanexCodr/Coderive/sessions/0c347a1f-9029-4fa9-afd6-fca74ce0196a

Co-authored-by: DanexCodr <216312766+DanexCodr@users.noreply.github.com>
Agent-Logs-Url: https://github.com/DanexCodr/Coderive/sessions/0c347a1f-9029-4fa9-afd6-fca74ce0196a

Co-authored-by: DanexCodr <216312766+DanexCodr@users.noreply.github.com>
@DanexCodr DanexCodr marked this pull request as ready for review April 13, 2026 05:41
@DanexCodr DanexCodr merged commit f381da9 into main Apr 13, 2026
@DanexCodr DanexCodr deleted the copilot/improve-borrow-checker-zero-cost branch April 13, 2026 05:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants