Skip to content

Conversation

@jeremy
Copy link
Contributor

@jeremy jeremy commented Dec 10, 2025

A series of profiling-guided optimizations to reduce object allocation and speed up hot paths in result processing, inspired by Trilogy's approach:

Allocation reductions:

  • Skip field name allocation in as: :array mode (names aren't used)
  • Eliminate unnecessary Hash allocation when no query options passed
  • Lazy-load uncommonly accessed server flags
  • Cache query options to avoid per-row lookups

Fast-path optimizations:

  • Integer parsing fast path (Trilogy-style)
  • cast: false fast path
  • Encoding cache to avoid repeated rb_enc_find_index lookups
  • force_encoding: "utf-8" option to skip charset→encoding handling when db and app encodings match (53% faster on string-heavy queries)

Partial casting:

  • New approach: eagerly cast cheap types (integers, floats) while deferring expensive ones (datetime, decimal) for lazy access—best of both worlds

Benchmarks: 17-53% faster depending on query shape and options.

When using as: :array mode, field names are allocated but never used.
Skip rb_mysql_result_fetch_field() -> profit.
In Array mode, field names are not used during iteration. Lazy-generate,
eliminating 1 Array + N field name Strings per query.
* Eliminate repeated rb_enc_find_index lookups by caching db charset ->
  Ruby encoding mappings.
* ~4.6% faster on benchmark query with 1000 string fields.
When db and app are all UTF-8 (for example) skip costly charset ->
encoding handling with `force_encoding: "utf-8"`.

Benchmark query with 1000 string fields
* `cast: true`: 53% faster
* `cast: false`: 17% faster
Optimistically 20+% faster due to casting fields likely to be accessed,
like integer, while leaving datetime, decimal, etc for lazy attrs.
@jeremy jeremy marked this pull request as draft December 10, 2025 20:00
@jeremy
Copy link
Contributor Author

jeremy commented Dec 10, 2025

This is an omnibus branch will all perf tunings. I'll split these out into distinct PRs for individual consideration.

@sodabrew
Copy link
Collaborator

Looking good!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants