-
-
Notifications
You must be signed in to change notification settings - Fork 1
Decompressor/Compressor pool #34
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
adam-fowler
wants to merge
7
commits into
main
Choose a base branch
from
compressor-pool
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
7 commits
Select commit
Hold shift + click to select a range
495c2d4
Add compressor pool
adam-fowler 5e02f20
Return compressor pool based off HTTP header
adam-fowler da7c945
Generic PoolAllocator
adam-fowler 6c9138b
Add allocation pool for decompressors
adam-fowler 78ea027
Add missing function
adam-fowler cd7a5bb
Add AllocatedValue to manage lifecycle of object created by ZlibAlloc…
adam-fowler 6ccee8c
Merge branch 'main' into compressor-pool
adam-fowler 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Hummingbird server framework project | ||
| // | ||
| // Copyright (c) 2025 the Hummingbird authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| import NIOConcurrencyHelpers | ||
|
|
||
| protocol ZlibAllocator<Value> { | ||
| associatedtype Value | ||
| func allocate() throws -> Value | ||
| func free(_ compressor: inout Value?) | ||
| } | ||
|
|
||
| /// Wrapper for value that uses allocator to manage its lifecycle | ||
| class AllocatedValue<Allocator: ZlibAllocator> { | ||
| let value: Allocator.Value | ||
| let allocator: Allocator | ||
|
|
||
| init(allocator: Allocator) throws { | ||
| self.allocator = allocator | ||
| self.value = try allocator.allocate() | ||
| } | ||
|
|
||
| deinit { | ||
| var optionalValue: Allocator.Value? = self.value | ||
| self.allocator.free(&optionalValue) | ||
| } | ||
| } | ||
|
|
||
| /// Type that can be used with the PoolAllocator | ||
| protocol PoolReusable { | ||
| func reset() throws | ||
| } | ||
|
|
||
| /// Allocator that keeps a pool of values around to be re-used. | ||
| /// | ||
| /// It will use a value from the pool if it isnt empty. Otherwise it will | ||
| /// allocate a new value. When values are freed they are passed back to the pool | ||
| /// up until the point where the pool grows to its maximum size. | ||
| struct PoolAllocator<BaseAllocator: ZlibAllocator>: ZlibAllocator where BaseAllocator.Value: PoolReusable { | ||
| typealias Value = BaseAllocator.Value | ||
| @usableFromInline | ||
| let base: BaseAllocator | ||
| @usableFromInline | ||
| let poolSize: Int | ||
| @usableFromInline | ||
| let values: NIOLockedValueBox<[Value]> | ||
|
|
||
| @inlinable | ||
| init(size: Int, base: BaseAllocator) { | ||
| self.base = base | ||
| self.poolSize = size | ||
| self.values = .init([]) | ||
| } | ||
|
|
||
| @inlinable | ||
| func allocate() throws -> Value { | ||
| let value = self.values.withLockedValue { | ||
| $0.popLast() | ||
| } | ||
| if let value { | ||
| try value.reset() | ||
| return value | ||
| } | ||
| return try base.allocate() | ||
| } | ||
|
|
||
| @inlinable | ||
| func free(_ value: inout Value?) { | ||
| guard let nonOptionalValue = value else { preconditionFailure("Cannot ball free twice on a compressor") } | ||
| self.values.withLockedValue { | ||
| if $0.count < poolSize { | ||
| $0.append(nonOptionalValue) | ||
| } | ||
| } | ||
| base.free(&value) | ||
| } | ||
| } |
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.
If this
writefails, the allocation isn't freed. Can we use a non-copyable with a deinit to handle this instead? Or a class or something similar.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.
Using AllocatedValue class to hold these.