Repository: go-swagger/go-swagger
Pull request #3063 (2024-01-20 07:37:05)

perf(codegen): reduced allocated memory
* contributes to #2649

This PR alleviates the need to duplicate specs across operations: we only need to save the state of the (mutating) spec once.

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/go-swagger/go-swagger/pull/3063?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-swagger) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`4aff943`)](https://app.codecov.io/gh/go-swagger/go-swagger/commit/4aff943ac9c3e805a75ea886cced5503c1dd25b3?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-swagger) 82.64% compared to head [(`6dfb009`)](https://app.codecov.io/gh/go-swagger/go-swagger/pull/3063?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-swagger) 82.79%.


<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master    #3063      +/-   ##
==========================================
+ Coverage   82.64%   82.79%   +0.15%     
==========================================
  Files          62       61       -1     
  Lines       12856    12759      -97     
==========================================
- Hits        10625    10564      -61     
+ Misses       1692     1667      -25     
+ Partials      539      528      -11     
```

| [Flag](https://app.codecov.io/gh/go-swagger/go-swagger/pull/3063/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-swagger) | Coverage Δ | |
|---|---|---|
| [codegen-oldstable-canary-fixtures](https://app.codecov.io/gh/go-swagger/go-swagger/pull/3063/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-swagger) | `?` | |
| [codegen-stable-canary-fixtures](https://app.codecov.io/gh/go-swagger/go-swagger/pull/3063/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-swagger) | `?` | |
| [unit-oldstable](https://app.codecov.io/gh/go-swagger/go-swagger/pull/3063/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-swagger) | `82.79% <100.00%> (ø)` | |
| [unit-stable](https://app.codecov.io/gh/go-swagger/go-swagger/pull/3063/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-swagger) | `82.79% <100.00%> (ø)` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-swagger#carryforward-flags-in-the-pull-request-comment) to find out more.


</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/go-swagger/go-swagger/pull/3063?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-swagger).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-swagger).

fredbi: The impact of this single change is visible on the codegen_test CI run, with timings for the whole codegen test suite down from about 20min to 15.

Patch:
diff --git a/generator/operation.go b/generator/operation.go
index 9b74b262..4be2f873 100644
--- a/generator/operation.go
+++ b/generator/operation.go
@@ -165,6 +165,7 @@ func (o *operationGenerator) Generate() error {
 		Imports:             imports,
 		DefaultScheme:       o.DefaultScheme,
 		Doc:                 o.Doc,
+		PristineDefs:        o.Doc.Pristine(),
 		Analyzed:            o.Analyzed,
 		BasePath:            o.BasePath,
 		GenOpts:             o.GenOpts,
@@ -224,7 +225,7 @@ type codeGenOpBuilder struct {
 	Target              string
 	Operation           spec.Operation
 	Doc                 *loads.Document
-	PristineDoc         *loads.Document
+	PristineDefs        *loads.Document
 	Analyzed            *analysis.Spec
 	DefaultImports      map[string]string
 	Imports             map[string]string
@@ -1035,10 +1036,7 @@ func (b *codeGenOpBuilder) cloneSchema(schema *spec.Schema) *spec.Schema {
 // This uses a deep clone the spec document to construct a type resolver which knows about definitions when the making of this operation started,
 // and only these definitions. We are not interested in the "original spec", but in the already transformed spec.
 func (b *codeGenOpBuilder) saveResolveContext(resolver *typeResolver, schema *spec.Schema) (*typeResolver, *spec.Schema) {
-	if b.PristineDoc == nil {
-		b.PristineDoc = b.Doc.Pristine()
-	}
-	rslv := newTypeResolver(b.GenOpts.LanguageOpts.ManglePackageName(resolver.ModelsPackage, defaultModelsTarget), b.DefaultImports[b.ModelsPackage], b.PristineDoc)
+	rslv := newTypeResolver(b.GenOpts.LanguageOpts.ManglePackageName(resolver.ModelsPackage, defaultModelsTarget), b.DefaultImports[b.ModelsPackage], b.PristineDefs)
 
 	return rslv, b.cloneSchema(schema)
 }
diff --git a/generator/support.go b/generator/support.go
index d1c8c889..3794ee1a 100644
--- a/generator/support.go
+++ b/generator/support.go
@@ -313,6 +313,7 @@ func (a *appGenerator) makeCodegenApp() (GenApp, error) {
 	genOps := make(GenOperations, 0, len(a.Operations))
 	consumesIndex := make(map[string][]string)
 	producesIndex := make(map[string][]string)
+	pristineDoc := a.SpecDoc.Pristine()
 
 	for operationName, opp := range a.Operations {
 		o := opp.Op
@@ -326,6 +327,7 @@ func (a *appGenerator) makeCodegenApp() (GenApp, error) {
 			Imports:          imports,
 			DefaultScheme:    a.DefaultScheme,
 			Doc:              a.SpecDoc,
+			PristineDefs:     pristineDoc,
 			Analyzed:         a.Analyzed,
 			BasePath:         a.SpecDoc.BasePath(),
 			GenOpts:          a.GenOpts,


Test patch:
diff --git a/generator/discriminators_test.go b/generator/discriminators_test.go
index 44f8eed3..9f394c97 100644
--- a/generator/discriminators_test.go
+++ b/generator/discriminators_test.go
@@ -190,6 +190,7 @@ func TestGenerateClient_OKResponseWithDiscriminator(t *testing.T) {
 		Principal:     "",
 		Target:        ".",
 		Doc:           specDoc,
+		PristineDefs:  specDoc.Pristine(),
 		Analyzed:      analysis.New(specDoc.Spec()),
 		Operation:     *op,
 		Authed:        false,
diff --git a/generator/operation_test.go b/generator/operation_test.go
index f03d0cdf..c71831aa 100644
--- a/generator/operation_test.go
+++ b/generator/operation_test.go
@@ -298,6 +298,7 @@ func methodPathOpBuilder(method, path, fname string) (codeGenOpBuilder, error) {
 		Target:        ".",
 		Operation:     *op,
 		Doc:           specDoc,
+		PristineDefs:  specDoc.Pristine(),
 		Analyzed:      analyzed,
 		Authed:        false,
 		ExtraSchemas:  make(map[string]GenSchema),
@@ -372,6 +373,7 @@ func opBuilderWithOpts(name, fname string, o *GenOpts) (codeGenOpBuilder, error)
 		Target:        ".",
 		Operation:     *op,
 		Doc:           specDoc,
+		PristineDefs:  specDoc.Pristine(),
 		Analyzed:      analyzed,
 		Authed:        false,
 		ExtraSchemas:  make(map[string]GenSchema),