Issue #14819 has been updated by sam.saffron (Sam Saffron).


I like the idea of a building block that converts to Integer or (Integer pair) if it is acceptable, you can already init a Time cheaply with a number, so we could have a similar method for IPAddr and MacAddr etc. and get away with shipping the common building block without pulling these things into core. 

It is not a trivial win though so I get the reluctance here.  

----------------------------------------
Feature #14819: Efficient cstring to RVALUE typecasting for c extension gems
https://bugs.ruby-lang.org/issues/14819#change-72367

* Author: sam.saffron (Sam Saffron)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
A general pattern I notice in the PG / MySQL and other gems is a general need for a C string to RVALUE type casting.

A lot of the problem is that the libpq likes returning strings and you do not want to allocate an extra RVALUE string for an IP address or Date and so on when returning data for the data set.

The result is that many c extension gems carry around very similar code for casting C strings to dates/ip addresses and so on. 

An example of such a function is: https://github.com/ged/ruby-pg/blob/master/ext/pg_text_decoder.c#L551-L715

Ruby already provides quite a few helpers such as `rb_cstr2inum` I wonder if there are some gaps we can fill like

`rb_cstr2ipaddr` `rb_cstr2time` `rb_cstr2date` `rb_cstr2macaddr` 

Having these implementation could help reduce code carried in gems and offer a faster method for adoption of faster patterns. For the interim gems could copy and paste MRI implementation and longer term just add an #if to lean on MRI. 

An alternative "wild" idea here would be to allow Ruby code to operate in an "unsafe" way on strings that are not RVALUES yet, .NET has a similar mechanism with `unsafe`. Eg: 

```
unsafe do
  non_rvalue_string = api.getcstr
  # operate on string with a limited set of operators that only allocate on stack
  api.free non_rvalue_string
end
``` 

If the wild idea here is interesting perhaps lets open a new topic, for this purpose let's discuss `rb_cstr2ipaddr` `rb_cstr2time` `rb_cstr2date` `rb_cstr2macaddr` 


 



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>