-
Notifications
You must be signed in to change notification settings - Fork 372
perf: reuse zstd compression contexts across shuffle blocks #5565
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
Open
dwsmith1983
wants to merge
33
commits into
apache:main
Choose a base branch
from
dwsmith1983:perf/shuffle-compressor-reuse
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
8da6e6b
perf: reuse zstd compression contexts across shuffle blocks
dwsmith1983 abf5807
fix: bound retained zstd context memory outside encode bursts
dwsmith1983 9afe4eb
Merge branch 'main' into perf/shuffle-compressor-reuse
dwsmith1983 7c907a8
fix: scope decode context to the scan operator and pin reuse in tests
dwsmith1983 16198d9
Merge branch 'main' into perf/shuffle-compressor-reuse
dwsmith1983 386bcd0
bench: add decoder-reuse microbenchmark for shuffle frames
dwsmith1983 6c8bae4
Merge branch 'main' into perf/shuffle-compressor-reuse
dwsmith1983 d8fa161
Merge branch main into perf/shuffle-compressor-reuse
dwsmith1983 dc8bb14
Merge branch 'main' into perf/shuffle-compressor-reuse
dwsmith1983 7a120d1
Merge branch 'main' into perf/shuffle-compressor-reuse
dwsmith1983 1372d78
Merge remote-tracking branch 'origin/main' into perf/shuffle-compress…
dwsmith1983 3fb42e1
Merge branch 'main' into perf/shuffle-compressor-reuse
dwsmith1983 d21b24e
perf: release the shuffle decoder at EOF and fold it into the scan in…
dwsmith1983 f2df122
Merge branch 'main' into perf/shuffle-compressor-reuse
dwsmith1983 89aed72
Merge branch 'main' into perf/shuffle-compressor-reuse
dwsmith1983 37b697f
Merge branch 'main' into perf/shuffle-compressor-reuse
dwsmith1983 495cead
Merge branch 'main' into perf/shuffle-compressor-reuse
dwsmith1983 a23d1ed
Merge branch 'main' into perf/shuffle-compressor-reuse
dwsmith1983 74b57b5
Merge branch 'main' into perf/shuffle-compressor-reuse
dwsmith1983 27c2033
perf: keep zstd context reuse to the shuffle write path
dwsmith1983 81828df
Merge remote-tracking branch 'origin/main' into perf/shuffle-compress…
dwsmith1983 5a1ead6
Merge remote-tracking branch 'origin/main' into perf/shuffle-compress…
dwsmith1983 d4fec19
Merge branch 'main' into perf/shuffle-compressor-reuse
dwsmith1983 9bbb153
Merge branch 'main' into perf/shuffle-compressor-reuse
dwsmith1983 9704320
Merge branch 'main' into perf/shuffle-compressor-reuse
dwsmith1983 3298b72
Merge remote-tracking branch 'origin/main' into perf/shuffle-compress…
dwsmith1983 792c3aa
Merge remote-tracking branch 'origin/main' into perf/shuffle-compress…
dwsmith1983 c314c32
Merge branch 'main' into perf/shuffle-compressor-reuse
dwsmith1983 b185104
Merge branch 'main' into perf/shuffle-compressor-reuse
dwsmith1983 16e3034
Merge branch 'main' into perf/shuffle-compressor-reuse
dwsmith1983 abb59d5
Merge branch 'main' into perf/shuffle-compressor-reuse
dwsmith1983 bf8300f
Merge remote-tracking branch 'origin/main' into perf/shuffle-compress…
dwsmith1983 cceb74c
Merge remote-tracking branch 'origin/main' into perf/shuffle-compress…
dwsmith1983 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| use arrow::ipc::writer::IpcWriteContext; | ||
| use std::io; | ||
| use zstd::zstd_safe::{CCtx, CParameter, ResetDirective}; | ||
|
|
||
| /// Largest zstd workspace worth caching between blocks. Covers the commonly configured | ||
| /// levels; higher levels (tens to hundreds of MiB of window) fall back to a fresh context | ||
| /// per block, which is what per-block encoding paid anyway. | ||
| /// | ||
| /// Workspace sizes measured against zstd-sys 2.0.16+zstd.1.5.7 (`sizeof()` after one | ||
| /// streaming frame, no pledged source size). Levels 7/8 sit ~3% under the cap, so a zstd | ||
| /// upgrade can silently flip them to release-per-block; re-measure on any dependency bump. | ||
| /// | ||
| /// | level | CCtx after encode | | ||
| /// |-------|-------------------| | ||
| /// | 1 | 1,369,617 | | ||
| /// | 2 | 2,090,513 | | ||
| /// | 3 | 3,663,377 | | ||
| /// | 4 | 4,974,097 | | ||
| /// | 5 | 5,498,385 | | ||
| /// | 6 | 5,498,385 | | ||
| /// | 7 | 8,119,825 | | ||
| /// | 8 | 8,119,825 | | ||
| /// | 9 | 15,459,857 (over) | | ||
| /// | 19 | 93,848,207 (over) | | ||
| const MAX_RETAINED_ZSTD_CONTEXT_BYTES: usize = 8 * 1024 * 1024; | ||
|
|
||
| /// Reusable compression state for encoding shuffle blocks. | ||
| /// | ||
| /// A zstd context costs about a megabyte and real setup time, so a task shares one across all | ||
| /// the blocks it encodes instead of paying per block. Keep ownership task-scoped, never | ||
| /// per-output-partition -- a shuffle can have thousands of partitions. Local shuffle reuses | ||
| /// the zstd context between blocks but bounds what it retains | ||
| /// ([`Self::release_zstd_if_oversized`]) and drops it at spill/finish boundaries; the remote | ||
| /// (RSS) path frees it after each admitted encode via [`Self::release_zstd`], since its | ||
| /// memory accounting only reserves the workspace per invocation. | ||
| #[derive(Default)] | ||
| pub struct ShuffleCodecContext { | ||
| /// Arrow's per-message IPC compression scratch, reused across encodes. | ||
| pub(crate) arrow_ipc: IpcWriteContext, | ||
| /// Lazily created, reused across blocks. | ||
| zstd: Option<CCtx<'static>>, | ||
| /// How many zstd contexts this value has created, so tests can assert that N blocks | ||
| /// cost fewer than N creations instead of only observing retained/released state. | ||
| #[cfg(test)] | ||
| zstd_creations: u32, | ||
| } | ||
|
|
||
| impl ShuffleCodecContext { | ||
| /// The shared zstd context primed for one frame at `level`, plus the Arrow IPC scratch | ||
| /// (returned together because the encoder borrows the context for the whole frame). | ||
| /// | ||
| /// The session reset and level re-apply happen on every call: writers with different | ||
| /// levels can share one context, and a failed encode must not leave state behind. | ||
| pub(crate) fn zstd_cctx( | ||
| &mut self, | ||
| level: i32, | ||
| ) -> io::Result<(&mut CCtx<'static>, &mut IpcWriteContext)> { | ||
| let cctx = match &mut self.zstd { | ||
| Some(cctx) => cctx, | ||
| none => { | ||
| #[cfg(test)] | ||
| { | ||
| self.zstd_creations += 1; | ||
| } | ||
| none.insert(CCtx::try_create().ok_or_else(|| { | ||
| io::Error::other("failed to allocate zstd compression context") | ||
| })?) | ||
| } | ||
| }; | ||
| cctx.reset(ResetDirective::SessionOnly) | ||
| .map_err(map_zstd_error)?; | ||
| cctx.set_parameter(CParameter::CompressionLevel(level)) | ||
| .map_err(map_zstd_error)?; | ||
| Ok((cctx, &mut self.arrow_ipc)) | ||
| } | ||
|
|
||
| /// Drops the cached zstd context, freeing its native workspace. The remote encode path | ||
| /// calls this after every admitted encode so the memory lives and dies inside that | ||
| /// invocation's reservation; the next zstd encode re-creates it lazily. | ||
| pub(crate) fn release_zstd(&mut self) { | ||
| self.zstd = None; | ||
| } | ||
|
|
||
| /// Drops the cached zstd context when its workspace outgrew | ||
| /// [`MAX_RETAINED_ZSTD_CONTEXT_BYTES`] (a session reset keeps the allocation); the next | ||
| /// encode re-creates it lazily. | ||
| pub(crate) fn release_zstd_if_oversized(&mut self) { | ||
| if self | ||
| .zstd | ||
| .as_ref() | ||
| .is_some_and(|cctx| cctx.sizeof() > MAX_RETAINED_ZSTD_CONTEXT_BYTES) | ||
| { | ||
| self.zstd = None; | ||
| } | ||
| } | ||
|
|
||
| /// Test hook for the release-vs-retain contract of the two encode paths. | ||
| #[cfg(test)] | ||
| pub(crate) fn holds_zstd_cctx(&self) -> bool { | ||
| self.zstd.is_some() | ||
| } | ||
|
|
||
| /// Test hook: zstd contexts created so far, for pinning reuse across blocks. | ||
| #[cfg(test)] | ||
| pub(crate) fn creation_count(&self) -> u32 { | ||
| self.zstd_creations | ||
| } | ||
| } | ||
|
|
||
| fn map_zstd_error(code: usize) -> io::Error { | ||
| io::Error::other(zstd::zstd_safe::get_error_name(code)) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I measured
CCtx::sizeof()against the pinned zstd 1.5.7 build after one streaming frame and the cap is closer to the edge than the comment suggests. Levels 7 and 8 come in at 8,119,825 bytes against a cap of 8,388,608, so about 3% of headroom. Levels 1 through 6 are 1.31 to 5.24 MiB and level 9 jumps to 14.74 MiB, so anything at 9 or above never reuses at all. On the decode side a level 19 frame leaves theDCtxat 8.47 MiB, which also misses the cap.Two things that would help. Could the measured level to size table go in the comment next to the constant, so the choice of 8 MiB is traceable and someone bumping
zstd-syscan see what they are moving? And could a test pin where the boundary actually falls, say level 6 retains and level 9 does not? As it stands a routine dependency bump could push levels 7 and 8 over the line and silently disable the optimization for those users with every test still passing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Measured the full table against the pinned build and got the same numbers you did — it is in the comment next to the constant now, with the zstd-sys version. Boundary tests pin levels 6 and 8 retained (8 being the ~3% edge, so a bump that crosses it fails loudly) and level 9 recreated per block; the decode side pins level 1 retained and level 19 released.