* [ruby-core:119801] [Ruby master Feature#20878] A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)`
@ 2024-11-07 10:31 byroot (Jean Boussier) via ruby-core
2024-11-07 14:54 ` [ruby-core:119813] " Eregon (Benoit Daloze) via ruby-core
` (11 more replies)
0 siblings, 12 replies; 13+ messages in thread
From: byroot (Jean Boussier) via ruby-core @ 2024-11-07 10:31 UTC (permalink / raw)
To: ruby-core; +Cc: byroot (Jean Boussier)
Issue #20878 has been reported by byroot (Jean Boussier).
----------------------------------------
Feature #20878: A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)`
https://bugs.ruby-lang.org/issues/20878
* Author: byroot (Jean Boussier)
* Status: Open
----------------------------------------
### Context
A common use case when writing C extensions is to generate text or bytes into a buffer, and to return it back
wrapped into a Ruby String. Examples are `JSON.generate(obj) -> String`, and all other format serializers,
compression libraries such as `ZLib.deflate`, etc, but also methods such as `Time.strftime`,
### Current Solution
#### Work in a buffer and copy the result
The most often used solution is to work with a native buffer and to manage a native allocated buffer,
and once the generation is done, call `rb_str_new*` to copy the result inside memory managed by Ruby.
It works, but isn't very efficient because it cause an extra copy and an extra `free()`.
On `ruby/json` macro-benchmarks, this represent around 5% of the time spent in `JSON.generate`.
```c
static void fbuffer_free(FBuffer *fb)
{
if (fb->ptr && fb->type == FBUFFER_HEAP_ALLOCATED) {
ruby_xfree(fb->ptr);
}
}
static VALUE fbuffer_to_s(FBuffer *fb)
{
VALUE result = rb_utf8_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));
fbuffer_free(fb);
return result;
}
```
#### Work inside RString allocated memory
Another way this is currently done, is to allocate an `RString` using `rb_str_buf_new`,
and write into it with various functions such as `rb_str_catf`,
or writing past `RString.len` through `RSTRING_PTR` and then resize it with `rb_str_set_len`.
The downside with this approach is that it contains a lot of inefficiencies, as `rb_str_set_len` will perform
numerous safety checks, compute coderange, and write the string terminator on every invocation.
Another major inneficiency is that this API make it hard to be in control of the buffer
growth, so it can result in a lot more `realloc()` calls than manually managing the buffer.
This method is used by `Kernel#sprintf`, `Time#strftime` etc, and when I attempted to improve `Time#strftime`
performance, this problem showed up as the biggest bottleneck:
- https://github.com/ruby/ruby/pull/11547
- https://github.com/ruby/ruby/pull/11544
- https://github.com/ruby/ruby/pull/11542
### Proposed API
I think a more effcient way to do this would be to work with a native buffer, and then build a RString
that "adopt" the memory region.
Technically, you can currently do this by reaching directly into `RString` members, but I don't think it's clean,
and a dedicated API would be preferable:
```c
/**
* Similar to rb_str_new(), but it adopts the pointer instead of copying.
*
* @param[in] ptr A memory region of `capa` bytes length. MUST have been allocated with `ruby_xmalloc`
* @param[in] len Length of the string, in bytes, not including the
* terminating NUL character, not including extra capacity.
* @param[in] capa The usable length of `ptr`, in bytes, including the
* terminating NUL character.
* @param[in] enc Encoding of `ptr`.
* @exception rb_eArgError `len` is negative.
* @return An instance of ::rb_cString, of `len` bytes length, `capa - 1` bytes capacity,
* and of `enc` encoding.
* @pre At least `capa` bytes of continuous memory region shall be
* accessible via `ptr`.
* @pre `ptr` MUST have been allocated with `ruby_xmalloc`.
* @pre `ptr` MUST not be manually freed after `rb_enc_str_adopt` has been called.
* @note `enc` can be a null pointer. It can also be seen as a routine
* identical to rb_usascii_str_new() then.
*/
rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc);
```
An alternative to the `adopt` term, could be `move`.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 13+ messages in thread
* [ruby-core:119813] [Ruby master Feature#20878] A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)`
2024-11-07 10:31 [ruby-core:119801] [Ruby master Feature#20878] A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)` byroot (Jean Boussier) via ruby-core
@ 2024-11-07 14:54 ` Eregon (Benoit Daloze) via ruby-core
2024-11-07 15:38 ` [ruby-core:119815] " byroot (Jean Boussier) via ruby-core
` (10 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: Eregon (Benoit Daloze) via ruby-core @ 2024-11-07 14:54 UTC (permalink / raw)
To: ruby-core; +Cc: Eregon (Benoit Daloze)
Issue #20878 has been updated by Eregon (Benoit Daloze).
LGTM, +1.
Maybe simply `rb_str_adopt()` for the name?
That way it's closer to `rb_str_new()`, and these days all String C API taking a C string should also take an encoding anyway so we don't need `enc_` and enc-less variants.
----------------------------------------
Feature #20878: A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)`
https://bugs.ruby-lang.org/issues/20878#change-110501
* Author: byroot (Jean Boussier)
* Status: Open
----------------------------------------
### Context
A common use case when writing C extensions is to generate text or bytes into a buffer, and to return it back
wrapped into a Ruby String. Examples are `JSON.generate(obj) -> String`, and all other format serializers,
compression libraries such as `ZLib.deflate`, etc, but also methods such as `Time.strftime`,
### Current Solution
#### Work in a buffer and copy the result
The most often used solution is to work with a native buffer and to manage a native allocated buffer,
and once the generation is done, call `rb_str_new*` to copy the result inside memory managed by Ruby.
It works, but isn't very efficient because it cause an extra copy and an extra `free()`.
On `ruby/json` macro-benchmarks, this represent around 5% of the time spent in `JSON.generate`.
```c
static void fbuffer_free(FBuffer *fb)
{
if (fb->ptr && fb->type == FBUFFER_HEAP_ALLOCATED) {
ruby_xfree(fb->ptr);
}
}
static VALUE fbuffer_to_s(FBuffer *fb)
{
VALUE result = rb_utf8_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));
fbuffer_free(fb);
return result;
}
```
#### Work inside RString allocated memory
Another way this is currently done, is to allocate an `RString` using `rb_str_buf_new`,
and write into it with various functions such as `rb_str_catf`,
or writing past `RString.len` through `RSTRING_PTR` and then resize it with `rb_str_set_len`.
The downside with this approach is that it contains a lot of inefficiencies, as `rb_str_set_len` will perform
numerous safety checks, compute coderange, and write the string terminator on every invocation.
Another major inneficiency is that this API make it hard to be in control of the buffer
growth, so it can result in a lot more `realloc()` calls than manually managing the buffer.
This method is used by `Kernel#sprintf`, `Time#strftime` etc, and when I attempted to improve `Time#strftime`
performance, this problem showed up as the biggest bottleneck:
- https://github.com/ruby/ruby/pull/11547
- https://github.com/ruby/ruby/pull/11544
- https://github.com/ruby/ruby/pull/11542
### Proposed API
I think a more effcient way to do this would be to work with a native buffer, and then build a RString
that "adopt" the memory region.
Technically, you can currently do this by reaching directly into `RString` members, but I don't think it's clean,
and a dedicated API would be preferable:
```c
/**
* Similar to rb_str_new(), but it adopts the pointer instead of copying.
*
* @param[in] ptr A memory region of `capa` bytes length. MUST have been allocated with `ruby_xmalloc`
* @param[in] len Length of the string, in bytes, not including the
* terminating NUL character, not including extra capacity.
* @param[in] capa The usable length of `ptr`, in bytes, including the
* terminating NUL character.
* @param[in] enc Encoding of `ptr`.
* @exception rb_eArgError `len` is negative.
* @return An instance of ::rb_cString, of `len` bytes length, `capa - 1` bytes capacity,
* and of `enc` encoding.
* @pre At least `capa` bytes of continuous memory region shall be
* accessible via `ptr`.
* @pre `ptr` MUST have been allocated with `ruby_xmalloc`.
* @pre `ptr` MUST not be manually freed after `rb_enc_str_adopt` has been called.
* @note `enc` can be a null pointer. It can also be seen as a routine
* identical to rb_usascii_str_new() then.
*/
rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc);
```
An alternative to the `adopt` term, could be `move`.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 13+ messages in thread
* [ruby-core:119815] [Ruby master Feature#20878] A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)`
2024-11-07 10:31 [ruby-core:119801] [Ruby master Feature#20878] A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)` byroot (Jean Boussier) via ruby-core
2024-11-07 14:54 ` [ruby-core:119813] " Eregon (Benoit Daloze) via ruby-core
@ 2024-11-07 15:38 ` byroot (Jean Boussier) via ruby-core
2024-11-07 16:40 ` [ruby-core:119816] " nobu (Nobuyoshi Nakada) via ruby-core
` (9 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: byroot (Jean Boussier) via ruby-core @ 2024-11-07 15:38 UTC (permalink / raw)
To: ruby-core; +Cc: byroot (Jean Boussier)
Issue #20878 has been updated by byroot (Jean Boussier).
> Maybe simply rb_str_adopt() for the name?
I don't have a strong opinion here, I just went with the current convention.
On another note:
> `ptr` MUST have been allocated with `ruby_xmalloc`.
I'm actually not sure this really need to be a MUST, I suppose what is a MUST is that the pointer should be `freeable` with `ruby_xfree`, but that's it.
----------------------------------------
Feature #20878: A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)`
https://bugs.ruby-lang.org/issues/20878#change-110503
* Author: byroot (Jean Boussier)
* Status: Open
----------------------------------------
### Context
A common use case when writing C extensions is to generate text or bytes into a buffer, and to return it back
wrapped into a Ruby String. Examples are `JSON.generate(obj) -> String`, and all other format serializers,
compression libraries such as `ZLib.deflate`, etc, but also methods such as `Time.strftime`,
### Current Solution
#### Work in a buffer and copy the result
The most often used solution is to work with a native buffer and to manage a native allocated buffer,
and once the generation is done, call `rb_str_new*` to copy the result inside memory managed by Ruby.
It works, but isn't very efficient because it cause an extra copy and an extra `free()`.
On `ruby/json` macro-benchmarks, this represent around 5% of the time spent in `JSON.generate`.
```c
static void fbuffer_free(FBuffer *fb)
{
if (fb->ptr && fb->type == FBUFFER_HEAP_ALLOCATED) {
ruby_xfree(fb->ptr);
}
}
static VALUE fbuffer_to_s(FBuffer *fb)
{
VALUE result = rb_utf8_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));
fbuffer_free(fb);
return result;
}
```
#### Work inside RString allocated memory
Another way this is currently done, is to allocate an `RString` using `rb_str_buf_new`,
and write into it with various functions such as `rb_str_catf`,
or writing past `RString.len` through `RSTRING_PTR` and then resize it with `rb_str_set_len`.
The downside with this approach is that it contains a lot of inefficiencies, as `rb_str_set_len` will perform
numerous safety checks, compute coderange, and write the string terminator on every invocation.
Another major inneficiency is that this API make it hard to be in control of the buffer
growth, so it can result in a lot more `realloc()` calls than manually managing the buffer.
This method is used by `Kernel#sprintf`, `Time#strftime` etc, and when I attempted to improve `Time#strftime`
performance, this problem showed up as the biggest bottleneck:
- https://github.com/ruby/ruby/pull/11547
- https://github.com/ruby/ruby/pull/11544
- https://github.com/ruby/ruby/pull/11542
### Proposed API
I think a more effcient way to do this would be to work with a native buffer, and then build a RString
that "adopt" the memory region.
Technically, you can currently do this by reaching directly into `RString` members, but I don't think it's clean,
and a dedicated API would be preferable:
```c
/**
* Similar to rb_str_new(), but it adopts the pointer instead of copying.
*
* @param[in] ptr A memory region of `capa` bytes length. MUST have been allocated with `ruby_xmalloc`
* @param[in] len Length of the string, in bytes, not including the
* terminating NUL character, not including extra capacity.
* @param[in] capa The usable length of `ptr`, in bytes, including the
* terminating NUL character.
* @param[in] enc Encoding of `ptr`.
* @exception rb_eArgError `len` is negative.
* @return An instance of ::rb_cString, of `len` bytes length, `capa - 1` bytes capacity,
* and of `enc` encoding.
* @pre At least `capa` bytes of continuous memory region shall be
* accessible via `ptr`.
* @pre `ptr` MUST have been allocated with `ruby_xmalloc`.
* @pre `ptr` MUST not be manually freed after `rb_enc_str_adopt` has been called.
* @note `enc` can be a null pointer. It can also be seen as a routine
* identical to rb_usascii_str_new() then.
*/
rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc);
```
An alternative to the `adopt` term, could be `move`.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 13+ messages in thread
* [ruby-core:119816] [Ruby master Feature#20878] A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)`
2024-11-07 10:31 [ruby-core:119801] [Ruby master Feature#20878] A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)` byroot (Jean Boussier) via ruby-core
2024-11-07 14:54 ` [ruby-core:119813] " Eregon (Benoit Daloze) via ruby-core
2024-11-07 15:38 ` [ruby-core:119815] " byroot (Jean Boussier) via ruby-core
@ 2024-11-07 16:40 ` nobu (Nobuyoshi Nakada) via ruby-core
2024-11-07 17:14 ` [ruby-core:119819] " byroot (Jean Boussier) via ruby-core
` (8 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: nobu (Nobuyoshi Nakada) via ruby-core @ 2024-11-07 16:40 UTC (permalink / raw)
To: ruby-core; +Cc: nobu (Nobuyoshi Nakada)
Issue #20878 has been updated by nobu (Nobuyoshi Nakada).
I think it is unsafe for memory leak, in comparison with "RString allocated memory".
----------------------------------------
Feature #20878: A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)`
https://bugs.ruby-lang.org/issues/20878#change-110504
* Author: byroot (Jean Boussier)
* Status: Open
----------------------------------------
### Context
A common use case when writing C extensions is to generate text or bytes into a buffer, and to return it back
wrapped into a Ruby String. Examples are `JSON.generate(obj) -> String`, and all other format serializers,
compression libraries such as `ZLib.deflate`, etc, but also methods such as `Time.strftime`,
### Current Solution
#### Work in a buffer and copy the result
The most often used solution is to work with a native buffer and to manage a native allocated buffer,
and once the generation is done, call `rb_str_new*` to copy the result inside memory managed by Ruby.
It works, but isn't very efficient because it cause an extra copy and an extra `free()`.
On `ruby/json` macro-benchmarks, this represent around 5% of the time spent in `JSON.generate`.
```c
static void fbuffer_free(FBuffer *fb)
{
if (fb->ptr && fb->type == FBUFFER_HEAP_ALLOCATED) {
ruby_xfree(fb->ptr);
}
}
static VALUE fbuffer_to_s(FBuffer *fb)
{
VALUE result = rb_utf8_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));
fbuffer_free(fb);
return result;
}
```
#### Work inside RString allocated memory
Another way this is currently done, is to allocate an `RString` using `rb_str_buf_new`,
and write into it with various functions such as `rb_str_catf`,
or writing past `RString.len` through `RSTRING_PTR` and then resize it with `rb_str_set_len`.
The downside with this approach is that it contains a lot of inefficiencies, as `rb_str_set_len` will perform
numerous safety checks, compute coderange, and write the string terminator on every invocation.
Another major inneficiency is that this API make it hard to be in control of the buffer
growth, so it can result in a lot more `realloc()` calls than manually managing the buffer.
This method is used by `Kernel#sprintf`, `Time#strftime` etc, and when I attempted to improve `Time#strftime`
performance, this problem showed up as the biggest bottleneck:
- https://github.com/ruby/ruby/pull/11547
- https://github.com/ruby/ruby/pull/11544
- https://github.com/ruby/ruby/pull/11542
### Proposed API
I think a more effcient way to do this would be to work with a native buffer, and then build a RString
that "adopt" the memory region.
Technically, you can currently do this by reaching directly into `RString` members, but I don't think it's clean,
and a dedicated API would be preferable:
```c
/**
* Similar to rb_str_new(), but it adopts the pointer instead of copying.
*
* @param[in] ptr A memory region of `capa` bytes length. MUST have been allocated with `ruby_xmalloc`
* @param[in] len Length of the string, in bytes, not including the
* terminating NUL character, not including extra capacity.
* @param[in] capa The usable length of `ptr`, in bytes, including the
* terminating NUL character.
* @param[in] enc Encoding of `ptr`.
* @exception rb_eArgError `len` is negative.
* @return An instance of ::rb_cString, of `len` bytes length, `capa - 1` bytes capacity,
* and of `enc` encoding.
* @pre At least `capa` bytes of continuous memory region shall be
* accessible via `ptr`.
* @pre `ptr` MUST have been allocated with `ruby_xmalloc`.
* @pre `ptr` MUST not be manually freed after `rb_enc_str_adopt` has been called.
* @note `enc` can be a null pointer. It can also be seen as a routine
* identical to rb_usascii_str_new() then.
*/
rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc);
```
An alternative to the `adopt` term, could be `move`.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 13+ messages in thread
* [ruby-core:119819] [Ruby master Feature#20878] A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)`
2024-11-07 10:31 [ruby-core:119801] [Ruby master Feature#20878] A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)` byroot (Jean Boussier) via ruby-core
` (2 preceding siblings ...)
2024-11-07 16:40 ` [ruby-core:119816] " nobu (Nobuyoshi Nakada) via ruby-core
@ 2024-11-07 17:14 ` byroot (Jean Boussier) via ruby-core
2024-11-08 0:02 ` [ruby-core:119828] " shyouhei (Shyouhei Urabe) via ruby-core
` (7 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: byroot (Jean Boussier) via ruby-core @ 2024-11-07 17:14 UTC (permalink / raw)
To: ruby-core; +Cc: byroot (Jean Boussier)
Issue #20878 has been updated by byroot (Jean Boussier).
> I think it is unsafe for memory leak, in comparison with "RString allocated memory".
I'm sorry I don't follow, could you expand on what you mean is unsafe? The entire "adopt" idea?
----------------------------------------
Feature #20878: A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)`
https://bugs.ruby-lang.org/issues/20878#change-110507
* Author: byroot (Jean Boussier)
* Status: Open
----------------------------------------
### Context
A common use case when writing C extensions is to generate text or bytes into a buffer, and to return it back
wrapped into a Ruby String. Examples are `JSON.generate(obj) -> String`, and all other format serializers,
compression libraries such as `ZLib.deflate`, etc, but also methods such as `Time.strftime`,
### Current Solution
#### Work in a buffer and copy the result
The most often used solution is to work with a native buffer and to manage a native allocated buffer,
and once the generation is done, call `rb_str_new*` to copy the result inside memory managed by Ruby.
It works, but isn't very efficient because it cause an extra copy and an extra `free()`.
On `ruby/json` macro-benchmarks, this represent around 5% of the time spent in `JSON.generate`.
```c
static void fbuffer_free(FBuffer *fb)
{
if (fb->ptr && fb->type == FBUFFER_HEAP_ALLOCATED) {
ruby_xfree(fb->ptr);
}
}
static VALUE fbuffer_to_s(FBuffer *fb)
{
VALUE result = rb_utf8_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));
fbuffer_free(fb);
return result;
}
```
#### Work inside RString allocated memory
Another way this is currently done, is to allocate an `RString` using `rb_str_buf_new`,
and write into it with various functions such as `rb_str_catf`,
or writing past `RString.len` through `RSTRING_PTR` and then resize it with `rb_str_set_len`.
The downside with this approach is that it contains a lot of inefficiencies, as `rb_str_set_len` will perform
numerous safety checks, compute coderange, and write the string terminator on every invocation.
Another major inneficiency is that this API make it hard to be in control of the buffer
growth, so it can result in a lot more `realloc()` calls than manually managing the buffer.
This method is used by `Kernel#sprintf`, `Time#strftime` etc, and when I attempted to improve `Time#strftime`
performance, this problem showed up as the biggest bottleneck:
- https://github.com/ruby/ruby/pull/11547
- https://github.com/ruby/ruby/pull/11544
- https://github.com/ruby/ruby/pull/11542
### Proposed API
I think a more effcient way to do this would be to work with a native buffer, and then build a RString
that "adopt" the memory region.
Technically, you can currently do this by reaching directly into `RString` members, but I don't think it's clean,
and a dedicated API would be preferable:
```c
/**
* Similar to rb_str_new(), but it adopts the pointer instead of copying.
*
* @param[in] ptr A memory region of `capa` bytes length. MUST have been allocated with `ruby_xmalloc`
* @param[in] len Length of the string, in bytes, not including the
* terminating NUL character, not including extra capacity.
* @param[in] capa The usable length of `ptr`, in bytes, including the
* terminating NUL character.
* @param[in] enc Encoding of `ptr`.
* @exception rb_eArgError `len` is negative.
* @return An instance of ::rb_cString, of `len` bytes length, `capa - 1` bytes capacity,
* and of `enc` encoding.
* @pre At least `capa` bytes of continuous memory region shall be
* accessible via `ptr`.
* @pre `ptr` MUST have been allocated with `ruby_xmalloc`.
* @pre `ptr` MUST not be manually freed after `rb_enc_str_adopt` has been called.
* @note `enc` can be a null pointer. It can also be seen as a routine
* identical to rb_usascii_str_new() then.
*/
rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc);
```
An alternative to the `adopt` term, could be `move`.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 13+ messages in thread
* [ruby-core:119828] [Ruby master Feature#20878] A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)`
2024-11-07 10:31 [ruby-core:119801] [Ruby master Feature#20878] A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)` byroot (Jean Boussier) via ruby-core
` (3 preceding siblings ...)
2024-11-07 17:14 ` [ruby-core:119819] " byroot (Jean Boussier) via ruby-core
@ 2024-11-08 0:02 ` shyouhei (Shyouhei Urabe) via ruby-core
2024-11-08 3:20 ` [ruby-core:119830] " nobu (Nobuyoshi Nakada) via ruby-core
` (6 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: shyouhei (Shyouhei Urabe) via ruby-core @ 2024-11-08 0:02 UTC (permalink / raw)
To: ruby-core; +Cc: shyouhei (Shyouhei Urabe)
Issue #20878 has been updated by shyouhei (Shyouhei Urabe).
byroot (Jean Boussier) wrote in #note-4:
> > I think it is unsafe for memory leak, in comparison with "RString allocated memory".
>
> I'm sorry I don't follow, could you expand on what you mean is unsafe? The entire "adopt" idea?
There is no reason for us to believe that the `const char *ptr` was allocated by malloc. It could be done by mmap or dlopen or anything. Ruby cannot garbage collect the string because it simply doesn't know how. Memory leak here is kind of inevitable.
----------------------------------------
Feature #20878: A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)`
https://bugs.ruby-lang.org/issues/20878#change-110516
* Author: byroot (Jean Boussier)
* Status: Open
----------------------------------------
### Context
A common use case when writing C extensions is to generate text or bytes into a buffer, and to return it back
wrapped into a Ruby String. Examples are `JSON.generate(obj) -> String`, and all other format serializers,
compression libraries such as `ZLib.deflate`, etc, but also methods such as `Time.strftime`,
### Current Solution
#### Work in a buffer and copy the result
The most often used solution is to work with a native buffer and to manage a native allocated buffer,
and once the generation is done, call `rb_str_new*` to copy the result inside memory managed by Ruby.
It works, but isn't very efficient because it cause an extra copy and an extra `free()`.
On `ruby/json` macro-benchmarks, this represent around 5% of the time spent in `JSON.generate`.
```c
static void fbuffer_free(FBuffer *fb)
{
if (fb->ptr && fb->type == FBUFFER_HEAP_ALLOCATED) {
ruby_xfree(fb->ptr);
}
}
static VALUE fbuffer_to_s(FBuffer *fb)
{
VALUE result = rb_utf8_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));
fbuffer_free(fb);
return result;
}
```
#### Work inside RString allocated memory
Another way this is currently done, is to allocate an `RString` using `rb_str_buf_new`,
and write into it with various functions such as `rb_str_catf`,
or writing past `RString.len` through `RSTRING_PTR` and then resize it with `rb_str_set_len`.
The downside with this approach is that it contains a lot of inefficiencies, as `rb_str_set_len` will perform
numerous safety checks, compute coderange, and write the string terminator on every invocation.
Another major inneficiency is that this API make it hard to be in control of the buffer
growth, so it can result in a lot more `realloc()` calls than manually managing the buffer.
This method is used by `Kernel#sprintf`, `Time#strftime` etc, and when I attempted to improve `Time#strftime`
performance, this problem showed up as the biggest bottleneck:
- https://github.com/ruby/ruby/pull/11547
- https://github.com/ruby/ruby/pull/11544
- https://github.com/ruby/ruby/pull/11542
### Proposed API
I think a more effcient way to do this would be to work with a native buffer, and then build a RString
that "adopt" the memory region.
Technically, you can currently do this by reaching directly into `RString` members, but I don't think it's clean,
and a dedicated API would be preferable:
```c
/**
* Similar to rb_str_new(), but it adopts the pointer instead of copying.
*
* @param[in] ptr A memory region of `capa` bytes length. MUST have been allocated with `ruby_xmalloc`
* @param[in] len Length of the string, in bytes, not including the
* terminating NUL character, not including extra capacity.
* @param[in] capa The usable length of `ptr`, in bytes, including the
* terminating NUL character.
* @param[in] enc Encoding of `ptr`.
* @exception rb_eArgError `len` is negative.
* @return An instance of ::rb_cString, of `len` bytes length, `capa - 1` bytes capacity,
* and of `enc` encoding.
* @pre At least `capa` bytes of continuous memory region shall be
* accessible via `ptr`.
* @pre `ptr` MUST have been allocated with `ruby_xmalloc`.
* @pre `ptr` MUST not be manually freed after `rb_enc_str_adopt` has been called.
* @note `enc` can be a null pointer. It can also be seen as a routine
* identical to rb_usascii_str_new() then.
*/
rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc);
```
An alternative to the `adopt` term, could be `move`.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 13+ messages in thread
* [ruby-core:119830] [Ruby master Feature#20878] A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)`
2024-11-07 10:31 [ruby-core:119801] [Ruby master Feature#20878] A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)` byroot (Jean Boussier) via ruby-core
` (4 preceding siblings ...)
2024-11-08 0:02 ` [ruby-core:119828] " shyouhei (Shyouhei Urabe) via ruby-core
@ 2024-11-08 3:20 ` nobu (Nobuyoshi Nakada) via ruby-core
2024-11-08 7:53 ` [ruby-core:119834] " byroot (Jean Boussier) via ruby-core
` (5 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: nobu (Nobuyoshi Nakada) via ruby-core @ 2024-11-08 3:20 UTC (permalink / raw)
To: ruby-core; +Cc: nobu (Nobuyoshi Nakada)
Issue #20878 has been updated by nobu (Nobuyoshi Nakada).
byroot (Jean Boussier) wrote in #note-4:
> > I think it is unsafe for memory leak, in comparison with "RString allocated memory".
>
> I'm sorry I don't follow, could you expand on what you mean is unsafe? The entire "adopt" idea?
Whenever you allocate a new object, there is a risk of a memory error.
In that case, who will look after the pointer that is about to be "adopted"?
----------------------------------------
Feature #20878: A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)`
https://bugs.ruby-lang.org/issues/20878#change-110518
* Author: byroot (Jean Boussier)
* Status: Open
----------------------------------------
### Context
A common use case when writing C extensions is to generate text or bytes into a buffer, and to return it back
wrapped into a Ruby String. Examples are `JSON.generate(obj) -> String`, and all other format serializers,
compression libraries such as `ZLib.deflate`, etc, but also methods such as `Time.strftime`,
### Current Solution
#### Work in a buffer and copy the result
The most often used solution is to work with a native buffer and to manage a native allocated buffer,
and once the generation is done, call `rb_str_new*` to copy the result inside memory managed by Ruby.
It works, but isn't very efficient because it cause an extra copy and an extra `free()`.
On `ruby/json` macro-benchmarks, this represent around 5% of the time spent in `JSON.generate`.
```c
static void fbuffer_free(FBuffer *fb)
{
if (fb->ptr && fb->type == FBUFFER_HEAP_ALLOCATED) {
ruby_xfree(fb->ptr);
}
}
static VALUE fbuffer_to_s(FBuffer *fb)
{
VALUE result = rb_utf8_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));
fbuffer_free(fb);
return result;
}
```
#### Work inside RString allocated memory
Another way this is currently done, is to allocate an `RString` using `rb_str_buf_new`,
and write into it with various functions such as `rb_str_catf`,
or writing past `RString.len` through `RSTRING_PTR` and then resize it with `rb_str_set_len`.
The downside with this approach is that it contains a lot of inefficiencies, as `rb_str_set_len` will perform
numerous safety checks, compute coderange, and write the string terminator on every invocation.
Another major inneficiency is that this API make it hard to be in control of the buffer
growth, so it can result in a lot more `realloc()` calls than manually managing the buffer.
This method is used by `Kernel#sprintf`, `Time#strftime` etc, and when I attempted to improve `Time#strftime`
performance, this problem showed up as the biggest bottleneck:
- https://github.com/ruby/ruby/pull/11547
- https://github.com/ruby/ruby/pull/11544
- https://github.com/ruby/ruby/pull/11542
### Proposed API
I think a more effcient way to do this would be to work with a native buffer, and then build a RString
that "adopt" the memory region.
Technically, you can currently do this by reaching directly into `RString` members, but I don't think it's clean,
and a dedicated API would be preferable:
```c
/**
* Similar to rb_str_new(), but it adopts the pointer instead of copying.
*
* @param[in] ptr A memory region of `capa` bytes length. MUST have been allocated with `ruby_xmalloc`
* @param[in] len Length of the string, in bytes, not including the
* terminating NUL character, not including extra capacity.
* @param[in] capa The usable length of `ptr`, in bytes, including the
* terminating NUL character.
* @param[in] enc Encoding of `ptr`.
* @exception rb_eArgError `len` is negative.
* @return An instance of ::rb_cString, of `len` bytes length, `capa - 1` bytes capacity,
* and of `enc` encoding.
* @pre At least `capa` bytes of continuous memory region shall be
* accessible via `ptr`.
* @pre `ptr` MUST have been allocated with `ruby_xmalloc`.
* @pre `ptr` MUST not be manually freed after `rb_enc_str_adopt` has been called.
* @note `enc` can be a null pointer. It can also be seen as a routine
* identical to rb_usascii_str_new() then.
*/
rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc);
```
An alternative to the `adopt` term, could be `move`.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 13+ messages in thread
* [ruby-core:119834] [Ruby master Feature#20878] A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)`
2024-11-07 10:31 [ruby-core:119801] [Ruby master Feature#20878] A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)` byroot (Jean Boussier) via ruby-core
` (5 preceding siblings ...)
2024-11-08 3:20 ` [ruby-core:119830] " nobu (Nobuyoshi Nakada) via ruby-core
@ 2024-11-08 7:53 ` byroot (Jean Boussier) via ruby-core
2024-11-08 8:43 ` [ruby-core:119835] " shyouhei (Shyouhei Urabe) via ruby-core
` (4 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: byroot (Jean Boussier) via ruby-core @ 2024-11-08 7:53 UTC (permalink / raw)
To: ruby-core; +Cc: byroot (Jean Boussier)
Issue #20878 has been updated by byroot (Jean Boussier).
> There is no reason for us to believe that the const char *ptr was allocated by malloc.
The proposed function documentation state that the pointer MUST have been allocated with `ruby_xmalloc`.
> henever you allocate a new object, there is a risk of a memory error. In that case, who will look after the pointer that is about to be "adopted"?
I see. From my understanding, the only possible error is OutOfMemory, what if `rb_enc_str_adopt` would directly call `ruby_xfree` on the pointer in such case? Would that cover your concern?
----------------------------------------
Feature #20878: A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)`
https://bugs.ruby-lang.org/issues/20878#change-110522
* Author: byroot (Jean Boussier)
* Status: Open
----------------------------------------
### Context
A common use case when writing C extensions is to generate text or bytes into a buffer, and to return it back
wrapped into a Ruby String. Examples are `JSON.generate(obj) -> String`, and all other format serializers,
compression libraries such as `ZLib.deflate`, etc, but also methods such as `Time.strftime`,
### Current Solution
#### Work in a buffer and copy the result
The most often used solution is to work with a native buffer and to manage a native allocated buffer,
and once the generation is done, call `rb_str_new*` to copy the result inside memory managed by Ruby.
It works, but isn't very efficient because it cause an extra copy and an extra `free()`.
On `ruby/json` macro-benchmarks, this represent around 5% of the time spent in `JSON.generate`.
```c
static void fbuffer_free(FBuffer *fb)
{
if (fb->ptr && fb->type == FBUFFER_HEAP_ALLOCATED) {
ruby_xfree(fb->ptr);
}
}
static VALUE fbuffer_to_s(FBuffer *fb)
{
VALUE result = rb_utf8_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));
fbuffer_free(fb);
return result;
}
```
#### Work inside RString allocated memory
Another way this is currently done, is to allocate an `RString` using `rb_str_buf_new`,
and write into it with various functions such as `rb_str_catf`,
or writing past `RString.len` through `RSTRING_PTR` and then resize it with `rb_str_set_len`.
The downside with this approach is that it contains a lot of inefficiencies, as `rb_str_set_len` will perform
numerous safety checks, compute coderange, and write the string terminator on every invocation.
Another major inneficiency is that this API make it hard to be in control of the buffer
growth, so it can result in a lot more `realloc()` calls than manually managing the buffer.
This method is used by `Kernel#sprintf`, `Time#strftime` etc, and when I attempted to improve `Time#strftime`
performance, this problem showed up as the biggest bottleneck:
- https://github.com/ruby/ruby/pull/11547
- https://github.com/ruby/ruby/pull/11544
- https://github.com/ruby/ruby/pull/11542
### Proposed API
I think a more effcient way to do this would be to work with a native buffer, and then build a RString
that "adopt" the memory region.
Technically, you can currently do this by reaching directly into `RString` members, but I don't think it's clean,
and a dedicated API would be preferable:
```c
/**
* Similar to rb_str_new(), but it adopts the pointer instead of copying.
*
* @param[in] ptr A memory region of `capa` bytes length. MUST have been allocated with `ruby_xmalloc`
* @param[in] len Length of the string, in bytes, not including the
* terminating NUL character, not including extra capacity.
* @param[in] capa The usable length of `ptr`, in bytes, including the
* terminating NUL character.
* @param[in] enc Encoding of `ptr`.
* @exception rb_eArgError `len` is negative.
* @return An instance of ::rb_cString, of `len` bytes length, `capa - 1` bytes capacity,
* and of `enc` encoding.
* @pre At least `capa` bytes of continuous memory region shall be
* accessible via `ptr`.
* @pre `ptr` MUST have been allocated with `ruby_xmalloc`.
* @pre `ptr` MUST not be manually freed after `rb_enc_str_adopt` has been called.
* @note `enc` can be a null pointer. It can also be seen as a routine
* identical to rb_usascii_str_new() then.
*/
rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc);
```
An alternative to the `adopt` term, could be `move`.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 13+ messages in thread
* [ruby-core:119835] [Ruby master Feature#20878] A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)`
2024-11-07 10:31 [ruby-core:119801] [Ruby master Feature#20878] A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)` byroot (Jean Boussier) via ruby-core
` (6 preceding siblings ...)
2024-11-08 7:53 ` [ruby-core:119834] " byroot (Jean Boussier) via ruby-core
@ 2024-11-08 8:43 ` shyouhei (Shyouhei Urabe) via ruby-core
2024-11-08 8:56 ` [ruby-core:119836] " rhenium (Kazuki Yamaguchi) via ruby-core
` (3 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: shyouhei (Shyouhei Urabe) via ruby-core @ 2024-11-08 8:43 UTC (permalink / raw)
To: ruby-core; +Cc: shyouhei (Shyouhei Urabe)
Issue #20878 has been updated by shyouhei (Shyouhei Urabe).
byroot (Jean Boussier) wrote in #note-7:
> > There is no reason for us to believe that the const char *ptr was allocated by malloc.
>
> The proposed function documentation state that the pointer MUST have been allocated with `ruby_xmalloc`.
If that's okay that's okay. For instance a return value of asprintf cannot be "adopt"ed then because obviously, that's not allocated by ruby_xmalloc.
----------------------------------------
Feature #20878: A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)`
https://bugs.ruby-lang.org/issues/20878#change-110523
* Author: byroot (Jean Boussier)
* Status: Open
----------------------------------------
### Context
A common use case when writing C extensions is to generate text or bytes into a buffer, and to return it back
wrapped into a Ruby String. Examples are `JSON.generate(obj) -> String`, and all other format serializers,
compression libraries such as `ZLib.deflate`, etc, but also methods such as `Time.strftime`,
### Current Solution
#### Work in a buffer and copy the result
The most often used solution is to work with a native buffer and to manage a native allocated buffer,
and once the generation is done, call `rb_str_new*` to copy the result inside memory managed by Ruby.
It works, but isn't very efficient because it cause an extra copy and an extra `free()`.
On `ruby/json` macro-benchmarks, this represent around 5% of the time spent in `JSON.generate`.
```c
static void fbuffer_free(FBuffer *fb)
{
if (fb->ptr && fb->type == FBUFFER_HEAP_ALLOCATED) {
ruby_xfree(fb->ptr);
}
}
static VALUE fbuffer_to_s(FBuffer *fb)
{
VALUE result = rb_utf8_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));
fbuffer_free(fb);
return result;
}
```
#### Work inside RString allocated memory
Another way this is currently done, is to allocate an `RString` using `rb_str_buf_new`,
and write into it with various functions such as `rb_str_catf`,
or writing past `RString.len` through `RSTRING_PTR` and then resize it with `rb_str_set_len`.
The downside with this approach is that it contains a lot of inefficiencies, as `rb_str_set_len` will perform
numerous safety checks, compute coderange, and write the string terminator on every invocation.
Another major inneficiency is that this API make it hard to be in control of the buffer
growth, so it can result in a lot more `realloc()` calls than manually managing the buffer.
This method is used by `Kernel#sprintf`, `Time#strftime` etc, and when I attempted to improve `Time#strftime`
performance, this problem showed up as the biggest bottleneck:
- https://github.com/ruby/ruby/pull/11547
- https://github.com/ruby/ruby/pull/11544
- https://github.com/ruby/ruby/pull/11542
### Proposed API
I think a more effcient way to do this would be to work with a native buffer, and then build a RString
that "adopt" the memory region.
Technically, you can currently do this by reaching directly into `RString` members, but I don't think it's clean,
and a dedicated API would be preferable:
```c
/**
* Similar to rb_str_new(), but it adopts the pointer instead of copying.
*
* @param[in] ptr A memory region of `capa` bytes length. MUST have been allocated with `ruby_xmalloc`
* @param[in] len Length of the string, in bytes, not including the
* terminating NUL character, not including extra capacity.
* @param[in] capa The usable length of `ptr`, in bytes, including the
* terminating NUL character.
* @param[in] enc Encoding of `ptr`.
* @exception rb_eArgError `len` is negative.
* @return An instance of ::rb_cString, of `len` bytes length, `capa - 1` bytes capacity,
* and of `enc` encoding.
* @pre At least `capa` bytes of continuous memory region shall be
* accessible via `ptr`.
* @pre `ptr` MUST have been allocated with `ruby_xmalloc`.
* @pre `ptr` MUST not be manually freed after `rb_enc_str_adopt` has been called.
* @note `enc` can be a null pointer. It can also be seen as a routine
* identical to rb_usascii_str_new() then.
*/
rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc);
```
An alternative to the `adopt` term, could be `move`.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 13+ messages in thread
* [ruby-core:119836] [Ruby master Feature#20878] A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)`
2024-11-07 10:31 [ruby-core:119801] [Ruby master Feature#20878] A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)` byroot (Jean Boussier) via ruby-core
` (7 preceding siblings ...)
2024-11-08 8:43 ` [ruby-core:119835] " shyouhei (Shyouhei Urabe) via ruby-core
@ 2024-11-08 8:56 ` rhenium (Kazuki Yamaguchi) via ruby-core
2024-11-08 10:08 ` [ruby-core:119840] " byroot (Jean Boussier) via ruby-core
` (2 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: rhenium (Kazuki Yamaguchi) via ruby-core @ 2024-11-08 8:56 UTC (permalink / raw)
To: ruby-core; +Cc: rhenium (Kazuki Yamaguchi)
Issue #20878 has been updated by rhenium (Kazuki Yamaguchi).
byroot (Jean Boussier) wrote:
> #### Work inside RString allocated memory
> [...]
> The downside with this approach is that it contains a lot of inefficiencies, as `rb_str_set_len` will perform
> numerous safety checks, compute coderange, and write the string terminator on every invocation.
I thought `rb_str_set_len()` was supposed to be the efficient alternative to `rb_str_resize()` meant for such a purpose.
I think an assert on the capacity or filling the terminator is cheap enough that it won't matter. That it computes coderange is news to me - I found it was since commit commit:6b66b5fdedb2c9a9ee48e290d57ca7f8d55e01a2 / [Bug #19902] in 2023. I think correcting coderange after directly modifying the RString-managed buffer is the caller's responsibility. Perhaps it could be reversed?
----------------------------------------
Feature #20878: A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)`
https://bugs.ruby-lang.org/issues/20878#change-110524
* Author: byroot (Jean Boussier)
* Status: Open
----------------------------------------
### Context
A common use case when writing C extensions is to generate text or bytes into a buffer, and to return it back
wrapped into a Ruby String. Examples are `JSON.generate(obj) -> String`, and all other format serializers,
compression libraries such as `ZLib.deflate`, etc, but also methods such as `Time.strftime`,
### Current Solution
#### Work in a buffer and copy the result
The most often used solution is to work with a native buffer and to manage a native allocated buffer,
and once the generation is done, call `rb_str_new*` to copy the result inside memory managed by Ruby.
It works, but isn't very efficient because it cause an extra copy and an extra `free()`.
On `ruby/json` macro-benchmarks, this represent around 5% of the time spent in `JSON.generate`.
```c
static void fbuffer_free(FBuffer *fb)
{
if (fb->ptr && fb->type == FBUFFER_HEAP_ALLOCATED) {
ruby_xfree(fb->ptr);
}
}
static VALUE fbuffer_to_s(FBuffer *fb)
{
VALUE result = rb_utf8_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));
fbuffer_free(fb);
return result;
}
```
#### Work inside RString allocated memory
Another way this is currently done, is to allocate an `RString` using `rb_str_buf_new`,
and write into it with various functions such as `rb_str_catf`,
or writing past `RString.len` through `RSTRING_PTR` and then resize it with `rb_str_set_len`.
The downside with this approach is that it contains a lot of inefficiencies, as `rb_str_set_len` will perform
numerous safety checks, compute coderange, and write the string terminator on every invocation.
Another major inneficiency is that this API make it hard to be in control of the buffer
growth, so it can result in a lot more `realloc()` calls than manually managing the buffer.
This method is used by `Kernel#sprintf`, `Time#strftime` etc, and when I attempted to improve `Time#strftime`
performance, this problem showed up as the biggest bottleneck:
- https://github.com/ruby/ruby/pull/11547
- https://github.com/ruby/ruby/pull/11544
- https://github.com/ruby/ruby/pull/11542
### Proposed API
I think a more effcient way to do this would be to work with a native buffer, and then build a RString
that "adopt" the memory region.
Technically, you can currently do this by reaching directly into `RString` members, but I don't think it's clean,
and a dedicated API would be preferable:
```c
/**
* Similar to rb_str_new(), but it adopts the pointer instead of copying.
*
* @param[in] ptr A memory region of `capa` bytes length. MUST have been allocated with `ruby_xmalloc`
* @param[in] len Length of the string, in bytes, not including the
* terminating NUL character, not including extra capacity.
* @param[in] capa The usable length of `ptr`, in bytes, including the
* terminating NUL character.
* @param[in] enc Encoding of `ptr`.
* @exception rb_eArgError `len` is negative.
* @return An instance of ::rb_cString, of `len` bytes length, `capa - 1` bytes capacity,
* and of `enc` encoding.
* @pre At least `capa` bytes of continuous memory region shall be
* accessible via `ptr`.
* @pre `ptr` MUST have been allocated with `ruby_xmalloc`.
* @pre `ptr` MUST not be manually freed after `rb_enc_str_adopt` has been called.
* @note `enc` can be a null pointer. It can also be seen as a routine
* identical to rb_usascii_str_new() then.
*/
rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc);
```
An alternative to the `adopt` term, could be `move`.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 13+ messages in thread
* [ruby-core:119840] [Ruby master Feature#20878] A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)`
2024-11-07 10:31 [ruby-core:119801] [Ruby master Feature#20878] A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)` byroot (Jean Boussier) via ruby-core
` (8 preceding siblings ...)
2024-11-08 8:56 ` [ruby-core:119836] " rhenium (Kazuki Yamaguchi) via ruby-core
@ 2024-11-08 10:08 ` byroot (Jean Boussier) via ruby-core
2024-11-08 15:47 ` [ruby-core:119847] " kddnewton (Kevin Newton) via ruby-core
2024-11-08 17:30 ` [ruby-core:119848] " mdalessio (Mike Dalessio) via ruby-core
11 siblings, 0 replies; 13+ messages in thread
From: byroot (Jean Boussier) via ruby-core @ 2024-11-08 10:08 UTC (permalink / raw)
To: ruby-core; +Cc: byroot (Jean Boussier)
Issue #20878 has been updated by byroot (Jean Boussier).
> If that's okay that's okay. For instance a return value of asprintf cannot be "adopt"ed then because obviously, that's not allocated by ruby_xmalloc.
Yes, that's why I'm wondering if this requirement should be relaxed to "MUST be freeable by `ruby_xfree`", which I believe would be true for `asprintf`.
> I think an assert on the capacity or filling the terminator is cheap enough that it won't matter.
It seemed to matter when I profiled. In some cases like `strftime` the string is written byte by byte, so it basically double the cost of appending a byte.
----------------------------------------
Feature #20878: A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)`
https://bugs.ruby-lang.org/issues/20878#change-110529
* Author: byroot (Jean Boussier)
* Status: Open
----------------------------------------
### Context
A common use case when writing C extensions is to generate text or bytes into a buffer, and to return it back
wrapped into a Ruby String. Examples are `JSON.generate(obj) -> String`, and all other format serializers,
compression libraries such as `ZLib.deflate`, etc, but also methods such as `Time.strftime`,
### Current Solution
#### Work in a buffer and copy the result
The most often used solution is to work with a native buffer and to manage a native allocated buffer,
and once the generation is done, call `rb_str_new*` to copy the result inside memory managed by Ruby.
It works, but isn't very efficient because it cause an extra copy and an extra `free()`.
On `ruby/json` macro-benchmarks, this represent around 5% of the time spent in `JSON.generate`.
```c
static void fbuffer_free(FBuffer *fb)
{
if (fb->ptr && fb->type == FBUFFER_HEAP_ALLOCATED) {
ruby_xfree(fb->ptr);
}
}
static VALUE fbuffer_to_s(FBuffer *fb)
{
VALUE result = rb_utf8_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));
fbuffer_free(fb);
return result;
}
```
#### Work inside RString allocated memory
Another way this is currently done, is to allocate an `RString` using `rb_str_buf_new`,
and write into it with various functions such as `rb_str_catf`,
or writing past `RString.len` through `RSTRING_PTR` and then resize it with `rb_str_set_len`.
The downside with this approach is that it contains a lot of inefficiencies, as `rb_str_set_len` will perform
numerous safety checks, compute coderange, and write the string terminator on every invocation.
Another major inneficiency is that this API make it hard to be in control of the buffer
growth, so it can result in a lot more `realloc()` calls than manually managing the buffer.
This method is used by `Kernel#sprintf`, `Time#strftime` etc, and when I attempted to improve `Time#strftime`
performance, this problem showed up as the biggest bottleneck:
- https://github.com/ruby/ruby/pull/11547
- https://github.com/ruby/ruby/pull/11544
- https://github.com/ruby/ruby/pull/11542
### Proposed API
I think a more effcient way to do this would be to work with a native buffer, and then build a RString
that "adopt" the memory region.
Technically, you can currently do this by reaching directly into `RString` members, but I don't think it's clean,
and a dedicated API would be preferable:
```c
/**
* Similar to rb_str_new(), but it adopts the pointer instead of copying.
*
* @param[in] ptr A memory region of `capa` bytes length. MUST have been allocated with `ruby_xmalloc`
* @param[in] len Length of the string, in bytes, not including the
* terminating NUL character, not including extra capacity.
* @param[in] capa The usable length of `ptr`, in bytes, including the
* terminating NUL character.
* @param[in] enc Encoding of `ptr`.
* @exception rb_eArgError `len` is negative.
* @return An instance of ::rb_cString, of `len` bytes length, `capa - 1` bytes capacity,
* and of `enc` encoding.
* @pre At least `capa` bytes of continuous memory region shall be
* accessible via `ptr`.
* @pre `ptr` MUST have been allocated with `ruby_xmalloc`.
* @pre `ptr` MUST not be manually freed after `rb_enc_str_adopt` has been called.
* @note `enc` can be a null pointer. It can also be seen as a routine
* identical to rb_usascii_str_new() then.
*/
rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc);
```
An alternative to the `adopt` term, could be `move`.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 13+ messages in thread
* [ruby-core:119847] [Ruby master Feature#20878] A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)`
2024-11-07 10:31 [ruby-core:119801] [Ruby master Feature#20878] A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)` byroot (Jean Boussier) via ruby-core
` (9 preceding siblings ...)
2024-11-08 10:08 ` [ruby-core:119840] " byroot (Jean Boussier) via ruby-core
@ 2024-11-08 15:47 ` kddnewton (Kevin Newton) via ruby-core
2024-11-08 17:30 ` [ruby-core:119848] " mdalessio (Mike Dalessio) via ruby-core
11 siblings, 0 replies; 13+ messages in thread
From: kddnewton (Kevin Newton) via ruby-core @ 2024-11-08 15:47 UTC (permalink / raw)
To: ruby-core; +Cc: kddnewton (Kevin Newton)
Issue #20878 has been updated by kddnewton (Kevin Newton).
I would use this in Prism as well. There are many cases where we allocate a string in the parser and then when we reify the Ruby AST we have to copy the string over. But the string content was allocated with ruby_xmalloc. So it would be nice to just hand over the string content without having to make a copy.
Personally I would prefer _move_ as a naming convention, just because it mirrors what I would expect from std::move.
----------------------------------------
Feature #20878: A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)`
https://bugs.ruby-lang.org/issues/20878#change-110538
* Author: byroot (Jean Boussier)
* Status: Open
----------------------------------------
### Context
A common use case when writing C extensions is to generate text or bytes into a buffer, and to return it back
wrapped into a Ruby String. Examples are `JSON.generate(obj) -> String`, and all other format serializers,
compression libraries such as `ZLib.deflate`, etc, but also methods such as `Time.strftime`,
### Current Solution
#### Work in a buffer and copy the result
The most often used solution is to work with a native buffer and to manage a native allocated buffer,
and once the generation is done, call `rb_str_new*` to copy the result inside memory managed by Ruby.
It works, but isn't very efficient because it cause an extra copy and an extra `free()`.
On `ruby/json` macro-benchmarks, this represent around 5% of the time spent in `JSON.generate`.
```c
static void fbuffer_free(FBuffer *fb)
{
if (fb->ptr && fb->type == FBUFFER_HEAP_ALLOCATED) {
ruby_xfree(fb->ptr);
}
}
static VALUE fbuffer_to_s(FBuffer *fb)
{
VALUE result = rb_utf8_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));
fbuffer_free(fb);
return result;
}
```
#### Work inside RString allocated memory
Another way this is currently done, is to allocate an `RString` using `rb_str_buf_new`,
and write into it with various functions such as `rb_str_catf`,
or writing past `RString.len` through `RSTRING_PTR` and then resize it with `rb_str_set_len`.
The downside with this approach is that it contains a lot of inefficiencies, as `rb_str_set_len` will perform
numerous safety checks, compute coderange, and write the string terminator on every invocation.
Another major inneficiency is that this API make it hard to be in control of the buffer
growth, so it can result in a lot more `realloc()` calls than manually managing the buffer.
This method is used by `Kernel#sprintf`, `Time#strftime` etc, and when I attempted to improve `Time#strftime`
performance, this problem showed up as the biggest bottleneck:
- https://github.com/ruby/ruby/pull/11547
- https://github.com/ruby/ruby/pull/11544
- https://github.com/ruby/ruby/pull/11542
### Proposed API
I think a more effcient way to do this would be to work with a native buffer, and then build a RString
that "adopt" the memory region.
Technically, you can currently do this by reaching directly into `RString` members, but I don't think it's clean,
and a dedicated API would be preferable:
```c
/**
* Similar to rb_str_new(), but it adopts the pointer instead of copying.
*
* @param[in] ptr A memory region of `capa` bytes length. MUST have been allocated with `ruby_xmalloc`
* @param[in] len Length of the string, in bytes, not including the
* terminating NUL character, not including extra capacity.
* @param[in] capa The usable length of `ptr`, in bytes, including the
* terminating NUL character.
* @param[in] enc Encoding of `ptr`.
* @exception rb_eArgError `len` is negative.
* @return An instance of ::rb_cString, of `len` bytes length, `capa - 1` bytes capacity,
* and of `enc` encoding.
* @pre At least `capa` bytes of continuous memory region shall be
* accessible via `ptr`.
* @pre `ptr` MUST have been allocated with `ruby_xmalloc`.
* @pre `ptr` MUST not be manually freed after `rb_enc_str_adopt` has been called.
* @note `enc` can be a null pointer. It can also be seen as a routine
* identical to rb_usascii_str_new() then.
*/
rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc);
```
An alternative to the `adopt` term, could be `move`.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 13+ messages in thread
* [ruby-core:119848] [Ruby master Feature#20878] A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)`
2024-11-07 10:31 [ruby-core:119801] [Ruby master Feature#20878] A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)` byroot (Jean Boussier) via ruby-core
` (10 preceding siblings ...)
2024-11-08 15:47 ` [ruby-core:119847] " kddnewton (Kevin Newton) via ruby-core
@ 2024-11-08 17:30 ` mdalessio (Mike Dalessio) via ruby-core
11 siblings, 0 replies; 13+ messages in thread
From: mdalessio (Mike Dalessio) via ruby-core @ 2024-11-08 17:30 UTC (permalink / raw)
To: ruby-core; +Cc: mdalessio (Mike Dalessio)
Issue #20878 has been updated by mdalessio (Mike Dalessio).
This would likely be useful in Nokogiri as well. The two key places I have in mind are
1. returning a large serialization string generated within libxml2 (which is configured to use `ruby_xmalloc` by default)
2. assembling an HTML5-compliant serialization within the extension (which currently uses `rb_enc_str_buf_cat`)
----------------------------------------
Feature #20878: A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)`
https://bugs.ruby-lang.org/issues/20878#change-110539
* Author: byroot (Jean Boussier)
* Status: Open
----------------------------------------
### Context
A common use case when writing C extensions is to generate text or bytes into a buffer, and to return it back
wrapped into a Ruby String. Examples are `JSON.generate(obj) -> String`, and all other format serializers,
compression libraries such as `ZLib.deflate`, etc, but also methods such as `Time.strftime`,
### Current Solution
#### Work in a buffer and copy the result
The most often used solution is to work with a native buffer and to manage a native allocated buffer,
and once the generation is done, call `rb_str_new*` to copy the result inside memory managed by Ruby.
It works, but isn't very efficient because it cause an extra copy and an extra `free()`.
On `ruby/json` macro-benchmarks, this represent around 5% of the time spent in `JSON.generate`.
```c
static void fbuffer_free(FBuffer *fb)
{
if (fb->ptr && fb->type == FBUFFER_HEAP_ALLOCATED) {
ruby_xfree(fb->ptr);
}
}
static VALUE fbuffer_to_s(FBuffer *fb)
{
VALUE result = rb_utf8_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));
fbuffer_free(fb);
return result;
}
```
#### Work inside RString allocated memory
Another way this is currently done, is to allocate an `RString` using `rb_str_buf_new`,
and write into it with various functions such as `rb_str_catf`,
or writing past `RString.len` through `RSTRING_PTR` and then resize it with `rb_str_set_len`.
The downside with this approach is that it contains a lot of inefficiencies, as `rb_str_set_len` will perform
numerous safety checks, compute coderange, and write the string terminator on every invocation.
Another major inneficiency is that this API make it hard to be in control of the buffer
growth, so it can result in a lot more `realloc()` calls than manually managing the buffer.
This method is used by `Kernel#sprintf`, `Time#strftime` etc, and when I attempted to improve `Time#strftime`
performance, this problem showed up as the biggest bottleneck:
- https://github.com/ruby/ruby/pull/11547
- https://github.com/ruby/ruby/pull/11544
- https://github.com/ruby/ruby/pull/11542
### Proposed API
I think a more effcient way to do this would be to work with a native buffer, and then build a RString
that "adopt" the memory region.
Technically, you can currently do this by reaching directly into `RString` members, but I don't think it's clean,
and a dedicated API would be preferable:
```c
/**
* Similar to rb_str_new(), but it adopts the pointer instead of copying.
*
* @param[in] ptr A memory region of `capa` bytes length. MUST have been allocated with `ruby_xmalloc`
* @param[in] len Length of the string, in bytes, not including the
* terminating NUL character, not including extra capacity.
* @param[in] capa The usable length of `ptr`, in bytes, including the
* terminating NUL character.
* @param[in] enc Encoding of `ptr`.
* @exception rb_eArgError `len` is negative.
* @return An instance of ::rb_cString, of `len` bytes length, `capa - 1` bytes capacity,
* and of `enc` encoding.
* @pre At least `capa` bytes of continuous memory region shall be
* accessible via `ptr`.
* @pre `ptr` MUST have been allocated with `ruby_xmalloc`.
* @pre `ptr` MUST not be manually freed after `rb_enc_str_adopt` has been called.
* @note `enc` can be a null pointer. It can also be seen as a routine
* identical to rb_usascii_str_new() then.
*/
rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc);
```
An alternative to the `adopt` term, could be `move`.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2024-11-08 17:31 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-11-07 10:31 [ruby-core:119801] [Ruby master Feature#20878] A new C API to create a String by adopting a pointer: `rb_enc_str_adopt(const char *ptr, long len, long capa, rb_encoding *enc)` byroot (Jean Boussier) via ruby-core
2024-11-07 14:54 ` [ruby-core:119813] " Eregon (Benoit Daloze) via ruby-core
2024-11-07 15:38 ` [ruby-core:119815] " byroot (Jean Boussier) via ruby-core
2024-11-07 16:40 ` [ruby-core:119816] " nobu (Nobuyoshi Nakada) via ruby-core
2024-11-07 17:14 ` [ruby-core:119819] " byroot (Jean Boussier) via ruby-core
2024-11-08 0:02 ` [ruby-core:119828] " shyouhei (Shyouhei Urabe) via ruby-core
2024-11-08 3:20 ` [ruby-core:119830] " nobu (Nobuyoshi Nakada) via ruby-core
2024-11-08 7:53 ` [ruby-core:119834] " byroot (Jean Boussier) via ruby-core
2024-11-08 8:43 ` [ruby-core:119835] " shyouhei (Shyouhei Urabe) via ruby-core
2024-11-08 8:56 ` [ruby-core:119836] " rhenium (Kazuki Yamaguchi) via ruby-core
2024-11-08 10:08 ` [ruby-core:119840] " byroot (Jean Boussier) via ruby-core
2024-11-08 15:47 ` [ruby-core:119847] " kddnewton (Kevin Newton) via ruby-core
2024-11-08 17:30 ` [ruby-core:119848] " mdalessio (Mike Dalessio) via ruby-core
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).