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

[prepared statements] fix for auto_encode_arrays, refactoring #59

Merged
merged 6 commits into from
Sep 4, 2024

Conversation

ermolaev
Copy link
Contributor

@ermolaev ermolaev commented Aug 21, 2024

  1. full testing for auto_encode_arrays (prepared, unprepared)
  2. refactoring prepared connection (simplify prepared method)
  3. performance improved
  4. more compatible with ActiveRecord

@ermolaev ermolaev force-pushed the fix-empty-array branch 3 times, most recently from 201d373 to c36178c Compare August 21, 2024 22:47
@ermolaev ermolaev force-pushed the fix-empty-array branch 2 times, most recently from 49dc09b to 06775fe Compare August 22, 2024 00:27
@ermolaev ermolaev changed the title [auto_encode_arrays, perf] fix empty array [prepared statements] fix for auto_encode_arrays, refactoring Aug 22, 2024
when true then "true"
when false then "false"
when nil then "NULL"
when EMPTY_ARRAY then array_encoder ? "'{}'" : "NULL"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arr = []
freeze_arr = [].freeze
Benchmark.ips do |x|
  x.report("new") {
    case arr
    when [] then true
    else false
    end
  }
  x.report("inline freeze") {
    case arr
    when [].freeze then true
    else false
    end
  }
  x.report("var freeze") {
    case arr
    when freeze_arr then true
    else false
    end
  }
  x.compare!
end;

# Comparison:
#           var freeze:  6412829.6 i/s
#                  new:  5822569.7 i/s - 1.10x  slower
#        inline freeze:  5379263.0 i/s - 1.19x  slower

@@ -30,8 +26,9 @@ def deserializer_cache

private def run(sql, params)
prepared_sql, binds, _bind_names = @param_binder.bind(sql, *params)
@prepared_cache ||= PreparedCache.new(unprepared)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lazy evaluate

@@ -28,6 +28,10 @@ def prepare_statement(sql)

private

def raw_connection
@connection.raw_connection
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for usage with ActiveRecord

@ermolaev
Copy link
Contributor Author

@SamSaffron PR ready to review

Comment on lines +25 to +42
class MiniSql::Postgres::TestAutoEncodeArraysPrepared < MiniSql::Postgres::TestPreparedConnection
include MiniSql::Postgres::ArrayTests

def setup
@unprepared_connection = pg_connection(auto_encode_arrays: true)
@connection = @unprepared_connection.prepared

setup_tables
end
end

class MiniSql::Postgres::TestAutoEncodeArraysUnprepared < MiniSql::Postgres::TestConnection
include MiniSql::Postgres::ArrayTests

def setup
@connection = pg_connection(auto_encode_arrays: true)
end
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

run all test for connection with auto_encode_arrays: true

@@ -61,12 +61,8 @@ def type_map
@type_map ||= self.class.type_map(raw_connection)
end

def prepared(condition = true)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in a real application the parameter condition was never used

@ermolaev
Copy link
Contributor Author

ermolaev commented Sep 3, 2024

Hi @SamSaffron, I remind you about this PR, because the master branch does not work correctly in some situations in mode auto_encode_arrays = true

@SamSaffron
Copy link
Member

no worries, thanks

the ruby-head failure is a bit of a nightmare, bigdecimal is a dependency on Ruby 3.4 and up.

@SamSaffron SamSaffron merged commit 3766913 into discourse:main Sep 4, 2024
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants