Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid relocation of strings by moving the content to heap memory #811

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Commits on Jul 21, 2020

  1. Avoid relocation of strings by moving the content to heap memory

    This is due to the fact that ruby-2.7+ relocates emdedded strings by GC.compact .
    See https://bugs.ruby-lang.org/issues/17023 for more description of the issue.
    
    Unfortunately there's no good way to pin string values to a fixed memory location in the ruby C API.
    The only way seems to be rb_gc_register_address(), but this function would keep a reference to the string object forever, so that it will never be freed.
    
    To work around this issue the string capacity is expanded, so that the string content is moved from embedded string memory to ordinary heap memory.
    The underlying string buffer of such strings is never relocated at GC.compact .
    
    There are some issues:
    
    1. String expanding fails with frozen strings, but frozen strings are relocated as well.
    
    2. All strings smaller than 24 characters are copied when passed to the C function.
       This increases call time by up to 70% although this copying isn't required in almost all cases.
       It's only needed when GC.compact is used and the string in question is accessed after GC.compact.
    
    3. The string object is modified in a way that changes it's RSTRING_PTR().
       That isn't visible in ruby, but could be noticed in conjunction with other C extensions.
    larskanis committed Jul 21, 2020
    Configuration menu
    Copy the full SHA
    d301e55 View commit details
    Browse the repository at this point in the history