Skip to content

perf: Optimize array_to_string to avoid a copy#20639

Open
neilconway wants to merge 2 commits intoapache:mainfrom
neilconway:neilc/optimize-array-to-string-builder
Open

perf: Optimize array_to_string to avoid a copy#20639
neilconway wants to merge 2 commits intoapache:mainfrom
neilconway:neilc/optimize-array-to-string-builder

Conversation

@neilconway
Copy link
Contributor

Which issue does this PR close?

N/A

Rationale for this change

array_to_string used a temporary buffer to convert array elements to a string, before then copying that buffer into a StringBuilder via append_value. We can skip this copy by arranging to write directly into the StringBuilder's buffer because it implements fmt::Write. This is a 12-18% performance improvement on the array_to_string` benchmarks.

What changes are included in this PR?

  • Optimization
  • Change the signature of the append closure to return Result, which avoids a lot of unwrap clutter

Are these changes tested?

Yes -- covered by existing tests and benchmarks.

Are there any user-facing changes?

No.

@github-actions github-actions bot added the functions Changes to functions implementation label Mar 2, 2026
@neilconway
Copy link
Contributor Author

Benchmarks:

  ┌──────────────────┬─────────────┐
  │    Benchmark     │ Improvement │
  ├──────────────────┼─────────────┤
  │ int64/5          │ -16%        │
  ├──────────────────┼─────────────┤
  │ int64/20         │ -13%        │
  ├──────────────────┼─────────────┤
  │ int64/100        │ -12%        │
  ├──────────────────┼─────────────┤
  │ float64/5        │ -14%        │
  ├──────────────────┼─────────────┤
  │ float64/20       │ -13%        │
  ├──────────────────┼─────────────┤
  │ float64/100      │ -12%        │
  ├──────────────────┼─────────────┤
  │ string/5         │ -18%        │
  ├──────────────────┼─────────────┤
  │ string/20        │ -15%        │
  ├──────────────────┼─────────────┤
  │ string/100       │ -14%        │
  ├──────────────────┼─────────────┤
  │ nested_int64/5   │ -13%        │
  ├──────────────────┼─────────────┤
  │ nested_int64/20  │ -13%        │
  ├──────────────────┼─────────────┤
  │ nested_int64/100 │ -12%        │
  └──────────────────┴─────────────┘

@neilconway
Copy link
Contributor Author

cc @goldmedal , if you're interested in reviewing another small tweak to array_to_string 😊

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

Labels

functions Changes to functions implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants