Enable Single Print when Generating Based on Swifttemplate #1308
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.
Brief
Enables a single point of output into STDOUT during codegen from swifttemplate
Context
This PR changes the way
swifttemplate
files are processed, and makes it possible to generate code inswifttemplate
using async/await or other concurrency.Technical Details
For a use case when there are a lot of types to be processed in a for-loop, it is important to allow use of concurrency upon processing each type. Currently, Sourcery only allows code generation using a single-threaded model, where the contents of
swifttemplate
are processed andprint
statements are inserted in every single point whereSourcery
would generate output after template parsing.Now, it would be possible to use
async/await
withinswifttemplate
for processing types and other facilities of processed swift code (AST), and only output the contents once in the very end.Example
let's consider the following code in
swifttemplate
:here, line
1
would iterate over all types, that is, it might be a collection of 100000 types, and sequentially process each one of them.But what if we do not need this sequential processing, and instead could leverage all of the M*-SoC CPUs, to process types in the codebase?
What if sourcery is run on a Linux machine with 128 CPUs? No possibility to leverage all those CPUs at once.
Now, with this PR, the following is possible, but let's first consider how the given example emits Swift code exactly:
6
is printed as// sourcery:inline:auto:TypeName.name.AutoCodable
as a comment in the generated source file10
is printed as// sourcery:end
as a comment in the generated source fileThis is happening using
print
statement emitted into the intermittent source file that SwiftTemplate binary uses to process the input.Now, using
print
statement is inefficient on its own:swifttemplate
file might include dozens of these statements, all put throughtout the generated intermittent fileprint
is expensive in terms of File I/O compared to a in-memory buffer which is printed just onceThis PR introduces that buffer named
sourceryBuffer
, and the following code is now possible: