Skip to content

Commit

Permalink
Store client encoding in data part of PG::Connection and PG::Result o…
Browse files Browse the repository at this point in the history
…bjects

The previous solution used MRI's internal bits to store the connection encoding.
This still works, but is only supported for String and some other types of VALUEs since ruby-2.6.
Non-encoding capable objects are no longer allowed to use these bits.

To save some bytes of struct size of PG::Result, we're using bit fields for enc_idx and autoclear.

This patch also removes the cache from pgconn_external_encoding().
It was useful in the past, when the encoding retrievel was slow.
In the meantime the cache did an improvement of factor 3 only.
Therefore as external_encoding is a very infrequently called method, there is no need for a cache any longer.

Fixes ged#280
  • Loading branch information
larskanis committed Oct 6, 2019
1 parent 6f7136d commit cfb90ef
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 101 deletions.
11 changes: 8 additions & 3 deletions ext/pg.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ typedef long suseconds_t;
#define RARRAY_AREF(a, i) (RARRAY_PTR(a)[i])
#endif

#define PG_ENC_IDX_BITS 30

/* The data behind each PG::Connection object */
typedef struct {
PGconn *pgconn;
Expand All @@ -94,12 +96,12 @@ typedef struct {
VALUE type_map_for_results;
/* IO object internally used for the trace stream */
VALUE trace_stream;
/* Cached Encoding object */
VALUE external_encoding;
/* Kind of PG::Coder object for casting ruby values to COPY rows */
VALUE encoder_for_put_copy_data;
/* Kind of PG::Coder object for casting COPY rows to ruby values */
VALUE decoder_for_get_copy_data;
/* Ruby encoding index of the client/internal encoding */
int enc_idx : PG_ENC_IDX_BITS;

#if defined(_WIN32)
/* File descriptor to be used for rb_w32_unwrap_io_handle() */
Expand All @@ -125,10 +127,13 @@ typedef struct {
*/
t_typemap *p_typemap;

/* Ruby encoding index of the client/internal encoding */
int enc_idx : PG_ENC_IDX_BITS;

/* 0 = PGresult is cleared by PG::Result#clear or by the GC
* 1 = PGresult is cleared internally by libpq
*/
int autoclear;
unsigned int autoclear : 1;

/* Number of fields in fnames[] .
* Set to -1 if fnames[] is not yet initialized.
Expand Down
Loading

0 comments on commit cfb90ef

Please sign in to comment.