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

Pre-allocate error vector in TRY #9986

Closed

Conversation

mbasmanova
Copy link
Contributor

Summary:
TRY(CAST(...)) is up to 4x slower than TRY_CAST when many rows fail.
The profile reveals that significant percentage of cpu time goes to
EvalCtx::ensureErrorsVectorSize. For every row that fails, we call
EvalCtx::ensureErrorsVectorSize to resize the error vector to accommodate that
row. When many rows fail we end up resizing a lot: resize(1), resize(2), resize
(3),....resize(n). Fix this by pre-allocating error vector in TryExpr.

An earlier attempt at fixing this #9911 caused 2x memory regression in one of the streaming pipelines. The change
was reverted: #9971

The regression was due to TRY starting to allocate 'nulls' buffer in results
unconditionally. Even if there were no errors, TRY would still allocate 'nulls'
buffer. When result is a boolean vector, allocating unnecessary 'nulls' buffer
increases memory usage for 'result' by 2x. This fix makes sure not to do that
and adds a test.

Also, this change creates ErrorVector with only nulls buffer allocated.
The 'values' buffer that requires ~20 bytes per row is allocated only if an
error occurs.

Before:

============================================================================
[...]hmarks/ExpressionBenchmarkBuilder.cpp     relative  time/iter   iters/s
============================================================================
cast##try_cast_invalid_empty_input                          2.27ms    440.97
cast##tryexpr_cast_invalid_empty_input                      8.96ms    111.56
cast##try_cast_invalid_nan                                  5.49ms    182.26
cast##tryexpr_cast_invalid_nan                             12.96ms     77.17

After:

cast##try_cast_invalid_empty_input                          2.22ms    451.34
cast##tryexpr_cast_invalid_empty_input                      4.52ms    221.06
cast##try_cast_invalid_nan                                  5.79ms    172.69
cast##tryexpr_cast_invalid_nan                              8.16ms    122.48

Differential Revision: D57968341

Summary:
TRY(CAST(...)) is up to 4x slower than TRY_CAST when many rows fail.
The profile reveals that significant percentage of cpu time goes to
EvalCtx::ensureErrorsVectorSize. For every row that fails, we call
EvalCtx::ensureErrorsVectorSize to resize the error vector to accommodate that
row. When many rows fail we end up resizing a lot: resize(1), resize(2), resize
(3),....resize(n). Fix this by pre-allocating error vector in TryExpr.

An earlier attempt at fixing this facebookincubator#9911 caused 2x memory regression in one of the streaming pipelines. The change 
was reverted: facebookincubator#9971

The regression was due to TRY starting to allocate 'nulls' buffer in results
unconditionally. Even if there were no errors, TRY would still allocate 'nulls'
buffer. When result is a boolean vector, allocating unnecessary 'nulls' buffer
increases memory usage for 'result' by 2x. This fix makes sure not to do that
and adds a test. 

Also, this change creates ErrorVector with only nulls buffer allocated.
The 'values' buffer that requires ~20 bytes per row is allocated only if an
error occurs.

Before:

```
============================================================================
[...]hmarks/ExpressionBenchmarkBuilder.cpp     relative  time/iter   iters/s
============================================================================
cast##try_cast_invalid_empty_input                          2.27ms    440.97
cast##tryexpr_cast_invalid_empty_input                      8.96ms    111.56
cast##try_cast_invalid_nan                                  5.49ms    182.26
cast##tryexpr_cast_invalid_nan                             12.96ms     77.17
```

After:

```
cast##try_cast_invalid_empty_input                          2.22ms    451.34
cast##tryexpr_cast_invalid_empty_input                      4.52ms    221.06
cast##try_cast_invalid_nan                                  5.79ms    172.69
cast##tryexpr_cast_invalid_nan                              8.16ms    122.48
```

Differential Revision: D57968341
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label May 30, 2024
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D57968341

Copy link

netlify bot commented May 30, 2024

Deploy Preview for meta-velox canceled.

Name Link
🔨 Latest commit 09f0d4a
🔍 Latest deploy log https://app.netlify.com/sites/meta-velox/deploys/6659071ced04cf00084aa986

Copy link
Contributor

@xiaoxmeng xiaoxmeng left a comment

Choose a reason for hiding this comment

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

@mbasmanova thanks for the improvement!

@facebook-github-bot
Copy link
Contributor

This pull request has been merged in 76424c0.

Copy link

Conbench analyzed the 1 benchmark run on commit 76424c0d.

There were no benchmark performance regressions. 🎉

The full Conbench report has more details.

Joe-Abraham pushed a commit to Joe-Abraham/velox that referenced this pull request Jun 7, 2024
Summary:
Pull Request resolved: facebookincubator#9986

TRY(CAST(...)) is up to 4x slower than TRY_CAST when many rows fail.
The profile reveals that significant percentage of cpu time goes to
EvalCtx::ensureErrorsVectorSize. For every row that fails, we call
EvalCtx::ensureErrorsVectorSize to resize the error vector to accommodate that
row. When many rows fail we end up resizing a lot: resize(1), resize(2), resize
(3),....resize(n). Fix this by pre-allocating error vector in TryExpr.

An earlier attempt at fixing this facebookincubator#9911 caused 2x memory regression in one of the streaming pipelines. The change
was reverted: facebookincubator#9971

The regression was due to TRY starting to allocate 'nulls' buffer in results
unconditionally. Even if there were no errors, TRY would still allocate 'nulls'
buffer. When result is a boolean vector, allocating unnecessary 'nulls' buffer
increases memory usage for 'result' by 2x. This fix makes sure not to do that
and adds a test.

Also, this change creates ErrorVector with only nulls buffer allocated.
The 'values' buffer that requires ~20 bytes per row is allocated only if an
error occurs.

Before:

```
============================================================================
[...]hmarks/ExpressionBenchmarkBuilder.cpp     relative  time/iter   iters/s
============================================================================
cast##try_cast_invalid_empty_input                          2.27ms    440.97
cast##tryexpr_cast_invalid_empty_input                      8.96ms    111.56
cast##try_cast_invalid_nan                                  5.49ms    182.26
cast##tryexpr_cast_invalid_nan                             12.96ms     77.17
```

After:

```
cast##try_cast_invalid_empty_input                          2.22ms    451.34
cast##tryexpr_cast_invalid_empty_input                      4.52ms    221.06
cast##try_cast_invalid_nan                                  5.79ms    172.69
cast##tryexpr_cast_invalid_nan                              8.16ms    122.48
```

Reviewed By: xiaoxmeng, bikramSingh91

Differential Revision: D57968341

fbshipit-source-id: d9f44aeda56596d9efb035ff9fada5eae22bea1d
Joe-Abraham pushed a commit to Joe-Abraham/velox that referenced this pull request Jun 7, 2024
Summary:
Pull Request resolved: facebookincubator#9986

TRY(CAST(...)) is up to 4x slower than TRY_CAST when many rows fail.
The profile reveals that significant percentage of cpu time goes to
EvalCtx::ensureErrorsVectorSize. For every row that fails, we call
EvalCtx::ensureErrorsVectorSize to resize the error vector to accommodate that
row. When many rows fail we end up resizing a lot: resize(1), resize(2), resize
(3),....resize(n). Fix this by pre-allocating error vector in TryExpr.

An earlier attempt at fixing this facebookincubator#9911 caused 2x memory regression in one of the streaming pipelines. The change
was reverted: facebookincubator#9971

The regression was due to TRY starting to allocate 'nulls' buffer in results
unconditionally. Even if there were no errors, TRY would still allocate 'nulls'
buffer. When result is a boolean vector, allocating unnecessary 'nulls' buffer
increases memory usage for 'result' by 2x. This fix makes sure not to do that
and adds a test.

Also, this change creates ErrorVector with only nulls buffer allocated.
The 'values' buffer that requires ~20 bytes per row is allocated only if an
error occurs.

Before:

```
============================================================================
[...]hmarks/ExpressionBenchmarkBuilder.cpp     relative  time/iter   iters/s
============================================================================
cast##try_cast_invalid_empty_input                          2.27ms    440.97
cast##tryexpr_cast_invalid_empty_input                      8.96ms    111.56
cast##try_cast_invalid_nan                                  5.49ms    182.26
cast##tryexpr_cast_invalid_nan                             12.96ms     77.17
```

After:

```
cast##try_cast_invalid_empty_input                          2.22ms    451.34
cast##tryexpr_cast_invalid_empty_input                      4.52ms    221.06
cast##try_cast_invalid_nan                                  5.79ms    172.69
cast##tryexpr_cast_invalid_nan                              8.16ms    122.48
```

Reviewed By: xiaoxmeng, bikramSingh91

Differential Revision: D57968341

fbshipit-source-id: d9f44aeda56596d9efb035ff9fada5eae22bea1d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported Merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants