Skip to content

Conversation

jserv
Copy link
Contributor

@jserv jserv commented Sep 21, 2025

  • Single allocation per node (54% memory reduction)
  • Eliminate double traversal in insert (3-4x faster)
  • Comprehensive error handling and overflow protection
  • Improve test suite
    • Red-black tree property validation
    • Memory stress testing (100K elements)
    • Edge case coverage

Summary by cubic

Reworked the red-black tree map for much lower memory and faster inserts. Nodes use a single allocation (≈54% less memory), and insert now does one traversal (≈3–4x faster). Expanded tests add RB property checks, 100K-element stress, and edge cases.

  • New Features

    • Single-allocation node layout for key/value and metadata.
    • Safer construction with size and overflow checks; graceful allocation failures.
    • Insert path: one traversal with RB fix-ups; faster lookup without temp nodes.
    • New APIs: map_size, map_first/map_last, map_next/map_prev.
    • Stronger delete/clear logic with underflow guard.
    • Test suite: RB property validation, 100K memory stress, performance scaling, boundary and empty-map cases.
  • Migration

    • Enum names: _CMP_LESS/_CMP_EQUAL/_CMP_GREATER → MAP_CMP_LESS/MAP_CMP_EQUAL/MAP_CMP_GREATER (e.g., breakpoint.c updated).
    • Signatures now use const where appropriate:
      • map_insert(map_t, const void* key, const void* val)
      • map_find(map_t, map_iter_t*, const void* key)
      • map_at_end(map_t, const map_iter_t*)
    • Update comparator helpers if referenced directly; map_cmp_int/map_cmp_uint remain, now force-inlined.

@jserv jserv requested a review from TheCloudlet September 21, 2025 10:36
Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 4 files

Prompt for AI agents (all 2 issues)

Understand the root cause of the following 2 issues and fix them.


<file name="src/map.c">

<violation number="1" location="src/map.c:475">
node-&gt;data may be misaligned due to missing padding after key; align data offset (and adjust allocation size) to avoid UB on strict-alignment platforms.</violation>
</file>

<file name="tests/map/test-map.c">

<violation number="1" location="tests/map/test-map.c:133">
Masking right_red with ~1 is inconsistent with implementation (~3), risking invalid pointer if additional tag bits are used.</violation>
</file>

React with 👍 or 👎 to teach cubic. Mention @cubic-dev-ai to give feedback, ask questions, or re-run the review.

This implements a single allocation strategy that combines node, key,
and value into one contiguous memory block, reducing memory usage by
54%. It optimizes insertion by eliminating double tree traversal,
achieving 3-4x faster performance.

In addition, this enhances the test suite with red-black tree property
validation, memory stress testing for 100,000 elements, and edge case
coverage.
@jserv jserv merged commit d22b787 into master Sep 22, 2025
20 of 24 checks passed
@jserv jserv deleted the refine-map branch September 22, 2025 17:09
@jserv jserv added this to the release-2025.2 milestone Oct 1, 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.

2 participants