Skip to content

Commit

Permalink
Avoid materializing Result multiple times in type map
Browse files Browse the repository at this point in the history
Testing methodology:

- 189k rows returned from pg_type query in `build_coder_maps`.
- Timing calling `PG::BasicTypeMapForQueries.new` only, figures are
  average of 16 iterations.

                 runtime        objects        allocations
Baseline :  2,027±350 ms          909 k            609 MiB
Optimized:    874± 70 ms (-56%)   113 k (-88%)     113 MiB (-79%)

Unfortunately performing the actual SQL query cannot easily be removed,
so there is a fair bit of variance in the runtimes, however, results
still show substantial improvement even when considering the worse case.
If I had more time I would time the query and remove it, but that
requires a bit more hacking than I would prefer, and, as said, the
improvement stands either way.
  • Loading branch information
amarshall committed Apr 7, 2021
1 parent ed95202 commit 4f38d28
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/pg/basic_type_mapping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def coder_by_oid(oid)
private

def build_coder_maps(connection)
result = connection.exec <<-SQL
result = connection.exec(<<-SQL).to_a
SELECT t.oid, t.typname, t.typelem, t.typdelim, ti.proname AS typinput
FROM pg_type as t
JOIN pg_proc as ti ON ti.oid = t.typinput
Expand Down

0 comments on commit 4f38d28

Please sign in to comment.