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

Conversation

larskanis
Copy link
Member

@larskanis larskanis commented Jul 17, 2020

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 larskanis force-pushed the avoid-string-reloc-v1 branch 3 times, most recently from 4aafac0 to 12aa771 Compare July 21, 2020 19:36
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.
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.

None yet

1 participant