Issue #14819 has been updated by jeremyevans0 (Jeremy Evans).


sam.saffron (Sam Saffron) wrote:
> btw did you see, PG gem now does native cstr2time I wonder if its time to just merge in functionality of sequel_pg into the pg gem in light of the type casters and so on. More winners here having one fast implementation of the building block.

As I mentioned, sequel_pg's implementation is dependent on Sequel's configuration (Date/Time, UTC/local database/application timezone, 8 separate cases, with a fallback to a pure-ruby version for non-UTC/local timezones), a generic implementation could definitely not handle things correctly.  I suspect that the sequel_pg case is not the only case where this is true.

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

* 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>