Skip to content
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

Ensure fixed-opaque types (arrays) implement EncodeTo() by pointer #68

Merged
merged 2 commits into from
Nov 10, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions lib/xdrgen/generators/go.rb
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,16 @@ def render_typedef_encode_to_interface(out, typedef)
name = name(typedef)
type = typedef.declaration.type
out.puts "// EncodeTo encodes this value using the Encoder."
out.puts "func (s #{name}) EncodeTo(e *xdr.Encoder) error {"

if type.is_a?(AST::Typespecs::Opaque) and type.fixed?
# Implement EncodeTo by pointer in fixed opaque types (arrays)
# otherwise (if called by value), Go will make a heap allocation
Comment on lines +394 to +395
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Suggested rewording of this explanation:

Suggested change
# Implement EncodeTo by pointer in fixed opaque types (arrays)
# otherwise (if called by value), Go will make a heap allocation
# Make the receiver of EncodeTo a pointer in fixed opaque types (arrays)
# otherwise (if called by value), Go will make a heap allocation

# for every by-value call since the copy required by the call
# tends to escape the stack due to the large array sizes.
out.puts "func (s *#{name}) EncodeTo(e *xdr.Encoder) error {"
else
out.puts "func (s #{name}) EncodeTo(e *xdr.Encoder) error {"
end
out.puts " var err error"
render_encode_to_body(out, "s", type, self_encode: true)
out.puts " return nil"
Expand Down Expand Up @@ -461,7 +470,17 @@ def render_encode_to_body(out, var, type, self_encode:)
out.puts " if #{var} != nil {"
var = "(*#{var})"
end
var = "#{name type}(#{var})" if self_encode
if self_encode
newvar = "#{name type}(#{var})"
if type.resolved_type.is_a?(AST::Definitions::Typedef)
declared_type = type.resolved_type.declaration.type
# Fixed opaque types implement EncodeTo by pointer
if declared_type.is_a?(AST::Typespecs::Opaque) and declared_type.fixed?
newvar = "(*#{name type})(&#{var})"
end
end
var = newvar
end
out.puts " err = #{var}.EncodeTo(e)"
if optional_within
out.puts " }"
Expand Down