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

Reduce the number of files loaded at require 'pg' #513

Merged
merged 2 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions ext/pg_text_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
#include <string.h>

VALUE rb_mPG_TextDecoder;
static ID s_id_decode;
static ID s_id_Rational;
static ID s_id_new;
static ID s_id_utc;
Expand Down Expand Up @@ -171,6 +170,19 @@ pg_text_dec_numeric(t_pg_coder *conv, const char *val, int len, int tuple, int f
return rb_funcall(rb_cObject, s_id_BigDecimal, 1, rb_str_new(val, len));
}

/* called per autoload when TextDecoder::Numeric is used */
static VALUE
init_pg_text_decoder_numeric(VALUE rb_mPG_TextDecoder)
{
rb_require("bigdecimal");
s_id_BigDecimal = rb_intern("BigDecimal");

/* dummy = rb_define_class_under( rb_mPG_TextDecoder, "Numeric", rb_cPG_SimpleDecoder ); */
pg_define_coder( "Numeric", pg_text_dec_numeric, rb_cPG_SimpleDecoder, rb_mPG_TextDecoder );

return Qnil;
}

/*
* Document-class: PG::TextDecoder::Float < PG::SimpleDecoder
*
Expand Down Expand Up @@ -922,8 +934,9 @@ pg_text_dec_inet(t_pg_coder *conv, const char *val, int len, int tuple, int fiel
return ip;
}

void
init_pg_text_decoder(void)
/* called per autoload when TextDecoder::Inet is used */
static VALUE
init_pg_text_decoder_inet(VALUE rb_mPG_TextDecoder)
{
rb_require("ipaddr");
s_IPAddr = rb_funcall(rb_cObject, rb_intern("const_get"), 1, rb_str_new2("IPAddr"));
Expand All @@ -942,14 +955,21 @@ init_pg_text_decoder(void)
s_vmasks6 = rb_eval_string("a = [0]*129; a[0] = 0; a[128] = 0xffffffffffffffffffffffffffffffff; 127.downto(1){|i| a[i] = a[i+1] - (1 << (127 - i))}; a.freeze");
rb_global_variable(&s_vmasks6);

s_id_decode = rb_intern("decode");
/* dummy = rb_define_class_under( rb_mPG_TextDecoder, "Inet", rb_cPG_SimpleDecoder ); */
pg_define_coder( "Inet", pg_text_dec_inet, rb_cPG_SimpleDecoder, rb_mPG_TextDecoder);

return Qnil;
}


void
init_pg_text_decoder(void)
{
s_id_Rational = rb_intern("Rational");
s_id_new = rb_intern("new");
s_id_utc = rb_intern("utc");
s_id_getlocal = rb_intern("getlocal");

rb_require("bigdecimal");
s_id_BigDecimal = rb_intern("BigDecimal");
s_nan = rb_eval_string("0.0/0.0");
rb_global_variable(&s_nan);
s_pos_inf = rb_eval_string("1.0/0.0");
Expand All @@ -959,6 +979,8 @@ init_pg_text_decoder(void)

/* This module encapsulates all decoder classes with text input format */
rb_mPG_TextDecoder = rb_define_module_under( rb_mPG, "TextDecoder" );
rb_define_private_method(rb_singleton_class(rb_mPG_TextDecoder), "init_inet", init_pg_text_decoder_inet, 0);
rb_define_private_method(rb_singleton_class(rb_mPG_TextDecoder), "init_numeric", init_pg_text_decoder_numeric, 0);

/* Make RDoc aware of the decoder classes... */
/* dummy = rb_define_class_under( rb_mPG_TextDecoder, "Boolean", rb_cPG_SimpleDecoder ); */
Expand All @@ -967,8 +989,6 @@ init_pg_text_decoder(void)
pg_define_coder( "Integer", pg_text_dec_integer, rb_cPG_SimpleDecoder, rb_mPG_TextDecoder );
/* dummy = rb_define_class_under( rb_mPG_TextDecoder, "Float", rb_cPG_SimpleDecoder ); */
pg_define_coder( "Float", pg_text_dec_float, rb_cPG_SimpleDecoder, rb_mPG_TextDecoder );
/* dummy = rb_define_class_under( rb_mPG_TextDecoder, "Numeric", rb_cPG_SimpleDecoder ); */
pg_define_coder( "Numeric", pg_text_dec_numeric, rb_cPG_SimpleDecoder, rb_mPG_TextDecoder );
/* dummy = rb_define_class_under( rb_mPG_TextDecoder, "String", rb_cPG_SimpleDecoder ); */
pg_define_coder( "String", pg_text_dec_string, rb_cPG_SimpleDecoder, rb_mPG_TextDecoder );
/* dummy = rb_define_class_under( rb_mPG_TextDecoder, "Bytea", rb_cPG_SimpleDecoder ); */
Expand All @@ -977,8 +997,6 @@ init_pg_text_decoder(void)
pg_define_coder( "Identifier", pg_text_dec_identifier, rb_cPG_SimpleDecoder, rb_mPG_TextDecoder );
/* dummy = rb_define_class_under( rb_mPG_TextDecoder, "Timestamp", rb_cPG_SimpleDecoder ); */
pg_define_coder( "Timestamp", pg_text_dec_timestamp, rb_cPG_SimpleDecoder, rb_mPG_TextDecoder);
/* dummy = rb_define_class_under( rb_mPG_TextDecoder, "Inet", rb_cPG_SimpleDecoder ); */
pg_define_coder( "Inet", pg_text_dec_inet, rb_cPG_SimpleDecoder, rb_mPG_TextDecoder);

/* dummy = rb_define_class_under( rb_mPG_TextDecoder, "Array", rb_cPG_CompositeDecoder ); */
pg_define_coder( "Array", pg_text_dec_array, rb_cPG_CompositeDecoder, rb_mPG_TextDecoder );
Expand Down
23 changes: 16 additions & 7 deletions ext/pg_text_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,21 @@ pg_text_enc_numeric(t_pg_coder *this, VALUE value, char *out, VALUE *intermediat
}
}

/* called per autoload when TextEncoder::Numeric is used */
static VALUE
init_pg_text_encoder_numeric(VALUE rb_mPG_TextDecoder)
{
s_str_F = rb_str_freeze(rb_str_new_cstr("F"));
rb_global_variable(&s_str_F);
rb_require("bigdecimal");
s_cBigDecimal = rb_const_get(rb_cObject, rb_intern("BigDecimal"));

/* dummy = rb_define_class_under( rb_mPG_TextEncoder, "Numeric", rb_cPG_SimpleEncoder ); */
pg_define_coder( "Numeric", pg_text_enc_numeric, rb_cPG_SimpleEncoder, rb_mPG_TextEncoder );

return Qnil;
}


static const char hextab[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
Expand Down Expand Up @@ -784,14 +799,10 @@ init_pg_text_encoder(void)
s_id_encode = rb_intern("encode");
s_id_to_i = rb_intern("to_i");
s_id_to_s = rb_intern("to_s");
s_str_F = rb_str_freeze(rb_str_new_cstr("F"));
rb_global_variable(&s_str_F);
rb_require("bigdecimal");
s_cBigDecimal = rb_const_get(rb_cObject, rb_intern("BigDecimal"));


/* This module encapsulates all encoder classes with text output format */
rb_mPG_TextEncoder = rb_define_module_under( rb_mPG, "TextEncoder" );
rb_define_private_method(rb_singleton_class(rb_mPG_TextEncoder), "init_numeric", init_pg_text_encoder_numeric, 0);

/* Make RDoc aware of the encoder classes... */
/* dummy = rb_define_class_under( rb_mPG_TextEncoder, "Boolean", rb_cPG_SimpleEncoder ); */
Expand All @@ -800,8 +811,6 @@ init_pg_text_encoder(void)
pg_define_coder( "Integer", pg_text_enc_integer, rb_cPG_SimpleEncoder, rb_mPG_TextEncoder );
/* dummy = rb_define_class_under( rb_mPG_TextEncoder, "Float", rb_cPG_SimpleEncoder ); */
pg_define_coder( "Float", pg_text_enc_float, rb_cPG_SimpleEncoder, rb_mPG_TextEncoder );
/* dummy = rb_define_class_under( rb_mPG_TextEncoder, "Numeric", rb_cPG_SimpleEncoder ); */
pg_define_coder( "Numeric", pg_text_enc_numeric, rb_cPG_SimpleEncoder, rb_mPG_TextEncoder );
/* dummy = rb_define_class_under( rb_mPG_TextEncoder, "String", rb_cPG_SimpleEncoder ); */
pg_define_coder( "String", pg_coder_enc_to_s, rb_cPG_SimpleEncoder, rb_mPG_TextEncoder );
/* dummy = rb_define_class_under( rb_mPG_TextEncoder, "Bytea", rb_cPG_SimpleEncoder ); */
Expand Down
44 changes: 34 additions & 10 deletions lib/pg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,45 @@ def self.connect( *args, &block )
end


module BinaryDecoder
%i[ TimestampUtc TimestampUtcToLocal TimestampLocal ].each do |klass|
autoload klass, 'pg/binary_decoder/timestamp'
end
end
module BinaryEncoder
%i[ TimestampUtc TimestampLocal ].each do |klass|
autoload klass, 'pg/binary_encoder/timestamp'
end
end
module TextDecoder
%i[ TimestampUtc TimestampUtcToLocal TimestampLocal TimestampWithoutTimeZone TimestampWithTimeZone ].each do |klass|
autoload klass, 'pg/text_decoder/timestamp'
end
autoload :Date, 'pg/text_decoder/date'
autoload :Inet, 'pg/text_decoder/inet'
autoload :JSON, 'pg/text_decoder/json'
autoload :Numeric, 'pg/text_decoder/numeric'
end
module TextEncoder
%i[ TimestampUtc TimestampWithoutTimeZone TimestampWithTimeZone ].each do |klass|
autoload klass, 'pg/text_encoder/timestamp'
end
autoload :Date, 'pg/text_encoder/date'
autoload :Inet, 'pg/text_encoder/inet'
autoload :JSON, 'pg/text_encoder/json'
autoload :Numeric, 'pg/text_encoder/numeric'
end

autoload :BasicTypeMapBasedOnResult, 'pg/basic_type_map_based_on_result'
autoload :BasicTypeMapForQueries, 'pg/basic_type_map_for_queries'
autoload :BasicTypeMapForResults, 'pg/basic_type_map_for_results'
autoload :BasicTypeRegistry, 'pg/basic_type_registry'
require 'pg/exceptions'
require 'pg/constants'
require 'pg/coder'
require 'pg/binary_encoder'
require 'pg/binary_decoder'
require 'pg/text_encoder'
require 'pg/text_decoder'
require 'pg/basic_type_registry'
require 'pg/basic_type_map_based_on_result'
require 'pg/basic_type_map_for_queries'
require 'pg/basic_type_map_for_results'
require 'pg/type_map_by_column'
require 'pg/connection'
require 'pg/result'
require 'pg/tuple'
require 'pg/version'
autoload :VERSION, 'pg/version'

end # module PG
File renamed without changes.
File renamed without changes.
7 changes: 3 additions & 4 deletions lib/pg/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
# frozen_string_literal: true

require 'pg' unless defined?( PG )
require 'uri'
require 'io/wait'
require 'io/wait' unless ::IO.public_instance_methods(false).include?(:wait_readable)
require 'socket'

# The PostgreSQL connection class. The interface for this class is based on
Expand Down Expand Up @@ -63,8 +62,8 @@ def self.parse_connect_args( *args )
iopts = {}

if args.length == 1
case args.first
when URI, /=/, /:\/\//
case args.first.to_s
when /=/, /:\/\//
# Option or URL string style
conn_string = args.first.to_s
iopts = PG::Connection.conninfo_parse(conn_string).each_with_object({}){|h, o| o[h[:keyword].to_sym] = h[:val] if h[:val] }
Expand Down
12 changes: 0 additions & 12 deletions lib/pg/constants.rb

This file was deleted.

18 changes: 18 additions & 0 deletions lib/pg/text_decoder/date.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- ruby -*-
# frozen_string_literal: true

require 'date'

module PG
module TextDecoder
class Date < SimpleDecoder
def decode(string, tuple=nil, field=nil)
if string =~ /\A(\d{4})-(\d\d)-(\d\d)\z/
::Date.new $1.to_i, $2.to_i, $3.to_i
else
string
end
end
end
end
end # module PG
9 changes: 9 additions & 0 deletions lib/pg/text_decoder/inet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# -*- ruby -*-
# frozen_string_literal: true

module PG
module TextDecoder
# Init C part of the decoder
init_inet
end
end # module PG
14 changes: 14 additions & 0 deletions lib/pg/text_decoder/json.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# -*- ruby -*-
# frozen_string_literal: true

require 'json'

module PG
module TextDecoder
class JSON < SimpleDecoder
def decode(string, tuple=nil, field=nil)
::JSON.parse(string, quirks_mode: true)
end
end
end
end # module PG
9 changes: 9 additions & 0 deletions lib/pg/text_decoder/numeric.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# -*- ruby -*-
# frozen_string_literal: true

module PG
module TextDecoder
# Init C part of the decoder
init_numeric
end
end # module PG
19 changes: 0 additions & 19 deletions lib/pg/text_decoder.rb → lib/pg/text_decoder/timestamp.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,8 @@
# -*- ruby -*-
# frozen_string_literal: true

require 'date'
require 'json'

module PG
module TextDecoder
class Date < SimpleDecoder
def decode(string, tuple=nil, field=nil)
if string =~ /\A(\d{4})-(\d\d)-(\d\d)\z/
::Date.new $1.to_i, $2.to_i, $3.to_i
else
string
end
end
end

class JSON < SimpleDecoder
def decode(string, tuple=nil, field=nil)
::JSON.parse(string, quirks_mode: true)
end
end

# Convenience classes for timezone options
class TimestampUtc < Timestamp
def initialize(**kwargs)
Expand Down
59 changes: 0 additions & 59 deletions lib/pg/text_encoder.rb

This file was deleted.

12 changes: 12 additions & 0 deletions lib/pg/text_encoder/date.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# -*- ruby -*-
# frozen_string_literal: true

module PG
module TextEncoder
class Date < SimpleEncoder
def encode(value)
value.respond_to?(:strftime) ? value.strftime("%Y-%m-%d") : value
end
end
end
end # module PG
Loading