Conversation
Adds StringView::CoreExt module with a pool-backed String#view method, activated via require "string_view/core_ext": str = "Hello, World!" str.view(0, 5) # => StringView backed by a cached Pool - New C file: string_view_core_ext.c with WeakKeyMap-based pool cache - Pool cache automatically cleans up when strings are GC'd - String#view freezes the string implicitly (required for safe views) - pool_view and cStringViewPool made non-static for cross-file access - Pool refreshes cached base pointer on each view call
String#view core extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds an opt-in
String#viewmethod viarequire "string_view/core_ext"that creates pool-backed StringView objects directly from any string:Implementation
StringView::CoreExtmodule in C (string_view_core_ext.c) with aview(byte_offset, byte_length)methodlib/string_view/core_ext.rbincludes the module intoStringObjectSpace::WeakKeyMap— automatically cleaned up when the string is GC'dpool_viewdirectly in C (no Ruby method dispatch on the hot path);rb_funcallonly on cache miss (once per string)String#viewimplicitly freezes the string before creating the pool (required for safe views)Changes
ext/string_view/string_view_core_ext.c,lib/string_view/core_ext.rb,test/test_core_ext.rbstring_view_pool.c:cStringViewPoolandpool_viewmade non-static for cross-file access; pool refreshes cachedbasepointer on eachviewcallstring_view.h: addedextern cStringViewPool,pool_view, andInit_string_view_core_extdeclarationsstring_view.c: callsInit_string_view_core_ext()fromInit_string_viewextconf.rb: addedstring_view_core_ext.cto sourcesREADME.md: documented theString#viewextensiontest_string_method_coverage.rb: excludesCoreExtmethods from coverage check