Skip to content

Conversation

Boshen
Copy link
Member

@Boshen Boshen commented Sep 29, 2025

Summary

This PR optimizes IndexVec::push by removing redundant bounds checking, improving performance in hot paths.

Changes

  • Enhanced Idx trait with MAX constant and unsafe fn from_usize_unchecked
  • Updated macro implementation to support new trait requirements
  • Optimized push to use unchecked index creation

Rationale

The bounds check in push is redundant because:

  • Vec's length is always valid for the index type
  • If length exceeded the index type's MAX, allocation would fail first (Vec cannot allocate more than isize::MAX bytes)
  • The unsafe usage is sound and well-documented

Test Plan

  • All existing tests pass
  • No breaking changes to public API (only additions)
  • Release build successful

🤖 Generated with Claude Code

Enhanced the Idx trait to enable bounds-check-free push operations by:
- Adding MAX associated constant for the maximum valid index value
- Adding unsafe from_usize_unchecked for unchecked index creation
- Providing default impl of from_usize that validates bounds

The push method now uses from_usize_unchecked since Vec's length is
always valid - if it exceeded the index type's MAX, allocation would
have failed long before reaching this point.

This optimization eliminates unnecessary bounds checking in the hot
path while maintaining safety through the type system.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@Boshen Boshen self-assigned this Sep 29, 2025
@Boshen Boshen merged commit c832d96 into main Sep 30, 2025
4 checks passed
@Boshen Boshen deleted the perf/remove-push-bounds-check branch September 30, 2025 04:01
@Boshen Boshen mentioned this pull request Sep 30, 2025
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.

1 participant