Skip to content

Commit

Permalink
Ensure fixed-opaque types (arrays) implement EncodeTo() by pointer (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio authored Nov 10, 2021
1 parent 92a28e1 commit 4677de9
Showing 1 changed file with 21 additions and 2 deletions.
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) && type.fixed?
# Implement EncodeTo by 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) && 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

0 comments on commit 4677de9

Please sign in to comment.