Skip to content

Refactor: DRY up validation, split extension into per-class files#6

Merged
paracycle merged 3 commits intomainfrom
uk-refactor
Mar 19, 2026
Merged

Refactor: DRY up validation, split extension into per-class files#6
paracycle merged 3 commits intomainfrom
uk-refactor

Conversation

@paracycle
Copy link
Copy Markdown
Member

@paracycle paracycle commented Mar 19, 2026

Summary

  • Extract shared inline helpers (sv_check_string, sv_check_bounds, sv_init_fields) and a SV_STRICT_SEARCH macro to eliminate duplicated validation and strict search guard code
  • Fix overflow-safe bounds checking in pool_view, sv_initialize, and sv_reset (previously off + len could wrap on large inputs)
  • Split the single string_view.c into string_view.h, string_view.c, string_view_strict.c, and string_view_pool.c for cleaner class separation
  • Pool views are no longer marked as frozen (they were silently mutated after reset!)
  • Pool growth is capped at 4096 views per batch to prevent runaway allocation
  • pool_memsize now reports the cost of pre-allocated views for accurate ObjectSpace.memsize_of

Benchmark verification

Full benchmark suite run before and after the split — all results within noise (±5%). No performance regression. SV_INLINE helpers confirmed fully inlined via nm (no symbols in binary).

Extract shared inline helpers and macros to reduce duplication:

- sv_check_string(): inline type validation (3 call sites)
- sv_check_bounds(): inline overflow-safe bounds check (3 call sites)
- sv_init_fields(): shared struct initialization (3 call sites)
- SV_STRICT_SEARCH(): macro for strict Regexp-guard wrappers (4 functions)

Net reduction: 39 insertions, 73 deletions.
Reorder file sections so each class's code is contiguous:
1. Shared infrastructure (struct, TypedData, validators, init helper)
2. StringView (internal helpers, construction, tiers 1-3, bang methods)
3. StringView::Strict (WouldAllocate, strict search guards)
4. StringView::Pool (struct, GC, alloc, grow, view, reset, backing)
5. Init

Pure code motion — no functional changes.
Extract StringView::Strict and StringView::Pool into their own
compilation units with a shared header:

- string_view.h: structs, extern globals, SV_INLINE helpers, cross-file
  function declarations
- string_view.c: core StringView implementation + Init_string_view
- string_view_strict.c: WouldAllocate, strict search guards,
  Init_string_view_strict
- string_view_pool.c: pool struct, GC, alloc/grow/view/reset,
  Init_string_view_pool

Benchmarked before and after — no performance regression. All SV_INLINE
helpers are fully inlined across compilation units (verified via nm).
@paracycle paracycle merged commit 5868967 into main Mar 19, 2026
7 checks passed
@paracycle paracycle deleted the uk-refactor branch March 19, 2026 00:12
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