pganalyze / pg_query

Ruby extension to parse, deparse and normalize SQL queries using the PostgreSQL query parser
BSD 3-Clause "New" or "Revised" License
790 stars 85 forks source link

Treewalker: Improve performance by avoiding memory allocations #309

Closed lfittl closed 10 months ago

lfittl commented 10 months ago

Previously the treewalker would unnecessary call "to_h" on each Protobuf message in the parsetree, in order to get the field names to walk. This caused unnecessary copies of the message, increasing memory usage and slowing down the tree walk. Instead, use the Protobuf descriptor and its field descriptors to walk the message.

Additionally this also optimizes the case where a block with 1 argument is used for the tree walk, since we don't need to handle the location, avoiding unnecessary copies of the field name string.

Together these changes result in about a 5x speed up in some use cases.