Implement Copy methods on schema structs & lang.References #40
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.
Why
Certain structs need to be copied regularly within the language server.
Currently this is done via
copystruct
which is easy (from implementation/readability perspective) and it works, but it also comes at a cost of higher CPU usage. Copying a few structs every once in while this way would probably work just fine (from CPU usage perspective), but in LS this operation is pretty common operation and so such small detail causing small difference does add up.This patch therefore aims to help reduce CPU consumption downstream by avoiding reflection and help address hashicorp/terraform-ls#509
Implementation Notes
I briefly considered making shallow copies within all
Copy()
functions and then overwriting the "problematic" fields, such as maps or slices - which would be better from maintenance & readability (no need to list out every field).For example
However this would also mean, that we could (unintentionally) end up making a shallow copy a new map/slice/pointer.
The current implementation therefore won me over due to side-effects being "safer" or more obvious. i.e. if someone in the future forgets to add a field I would prefer to it not being copied at all rather than silently making a shallow copy.