anchor.diff:  possibly_format_mfield_encoder(MsgName, #field{type={msg,SubMsg}}=FieldDef,
                                  AnRes) ->
       FnName = mk_field_encode_fn_name(MsgName, FieldDef),
  -    case is_msgsize_known_at_compiletime(SubMsg, AnRes) of
  +    case is_msgsize_known_at_generationtime(SubMsg, AnRes) of
           no ->
  -            [f("~p(Msg, Bin) ->~n", [FnName]),
  -             f("    SubBin = ~p(Msg, <<>>),~n", [mk_fn(e_msg_, SubMsg)]),
  -             f("    Bin2 = e_varint(byte_size(SubBin), Bin),~n"),
  -             f("    <<Bin2/binary, SubBin/binary>>.~n~n")];
  +            gpb_codegen:format_fn(
  +              FnName,
  +              fun(Msg, Bin) ->
  +                      SubBin = '<encode-msg>'(Msg, <<>>),
  +                      Bin2 = e_varint(byte_size(SubBin), Bin),
  +                      <<Bin2/binary, SubBin/binary>>
  +              end,
  +              [replace_term('<encode-msg>', mk_fn(e_msg_, SubMsg))]);
           {yes, MsgSize} when MsgSize > 0 ->
  -            MsgSizeTxt = varint_to_byte_text(MsgSize),
  -            [f("~p(Msg, Bin) ->~n", [FnName]),
  -             f("    Bin2 = <<Bin/binary, ~s>>,~n", [MsgSizeTxt]),
  -             f("    ~p(Msg, Bin2).~n~n", [mk_fn(e_msg_, SubMsg)])];
  +            MsgSizeBytes = varint_to_binary_fields(MsgSize),
  +            gpb_codegen:format_fn(
  +              FnName,
  +              fun(Msg, Bin) ->
  +                      Bin2 = <<Bin/binary, '<msg-size>'>>,
  +                      '<encode-msg>'(Msg, Bin2)
  +              end,
  +              [splice_trees('<msg-size>', MsgSizeBytes),
  +               replace_term('<encode-msg>', mk_fn(e_msg_, SubMsg))]);
           {yes, 0} ->
               %% special case, there will not be any e_msg_<MsgName>/2 function
               %% generated, so don't call it.
  -            [f("~p(_Msg, Bin) ->~n", [FnName]),
  -             f("    <<Bin/binary, 0>>.~n~n")]
  +            gpb_codegen:format_fn(
  +              FnName,
  +              fun(_Msg, Bin) -> <<Bin/binary, 0>> end)
       end;
   possibly_format_mfield_encoder(_MsgName, _FieldDef, _Defs) ->
       [].
   
  -is_msgsize_known_at_compiletime(MsgName, #anres{known_msg_size=MsgSizes}) ->
  +is_msgsize_known_at_generationtime(MsgName, #anres{known_msg_size=MsgSizes}) ->
       case dict:fetch(MsgName, MsgSizes) of
           MsgSize when is_integer(MsgSize) ->
               {yes, MsgSize};
  
positive.code:     FnName = mk_field_encode_fn_name(MsgName, FDef),
      ElemEncoderFn = mk_field_encode_fn_name(MsgName,
                                              FDef#field{occurrence=required}),
      KeyTxt = mk_key_txt(FNum, Type),
      [f("~p([Elem | Rest], Bin) ->~n", [FnName]),
       f("    Bin2 = <<Bin/binary, ~s>>,~n", [KeyTxt]),
       f("    Bin3 = ~p(Elem, Bin2),~n", [ElemEncoderFn]),
       f("    ~p(Rest, Bin3);~n", [FnName]),
       f("~p([], Bin) ->~n", [FnName]),
       f("    Bin.~n~n")].
  
  format_packed_field_encoder2(MsgName, #field{type=Type}=FDef) ->
      case packed_byte_size_can_be_computed(Type) of
          {yes, BitLen, BitType} ->
              format_knownsize_packed_field_encoder2(MsgName, FDef,
                                                    BitLen, BitType);
          no ->
              format_unknownsize_packed_field_encoder2(MsgName, FDef)
      end.
  
  packed_byte_size_can_be_computed(fixed32)  -> {yes, 32, 'little'};
  packed_byte_size_can_be_computed(sfixed32) -> {yes, 32, 'little-signed'};
  packed_byte_size_can_be_computed(float)    -> {yes, 32, 'little-float'};
  packed_byte_size_can_be_computed(fixed64)  -> {yes, 64, 'little'};
  packed_byte_size_can_be_computed(sfixed64) -> {yes, 64, 'little-signed'};
  packed_byte_size_can_be_computed(double)   -> {yes, 64, 'little-float'};
  packed_byte_size_can_be_computed(_)        -> no.
  
  format_knownsize_packed_field_encoder2(MsgName, #field{name=FName,
                                                         fnum=FNum}=FDef,
                                        BitLen, BitType) ->
      FnName = mk_field_encode_fn_name(MsgName, FDef),
      KeyTxt = mk_key_txt(FNum, bytes),
      PackedFnName = mk_fn(e_pfield_, MsgName, FName),
      [f("~p(Elems, Bin) when Elems =/= [] ->~n", [FnName]),
       f("    Bin2 = <<Bin/binary, ~s>>,~n", [KeyTxt]),
       f("    Bin3 = e_varint(length(Elems) * ~w, Bin2),~n", [BitLen div 8]),
       f("    ~p(Elems, Bin3);~n", [PackedFnName]),
       f("~p([], Bin) ->~n", [FnName]),
       f("    Bin.~n"),
       f("~n"),
       f("~p([Value | Rest], Bin) ->~n", [PackedFnName]),
       f("    Bin2 = <<Bin/binary, Value:~w/~s>>,~n", [BitLen, BitType]),
       f("    ~p(Rest, Bin2);~n", [PackedFnName]),
       f("~p([], Bin) ->~n", [PackedFnName]),
       f("    Bin.~n~n")].
  
  format_unknownsize_packed_field_encoder2(MsgName, #field{name=FName,
                                                           fnum=FNum}=FDef) ->
      FnName = mk_field_encode_fn_name(MsgName, FDef),
      ElemEncoderFn = mk_field_encode_fn_name(MsgName,
                                              FDef#field{occurrence=required}),
      KeyTxt = mk_key_txt(FNum, bytes),
      PackedFnName = mk_fn(e_pfield_, MsgName, FName),
      [f("~p(Elems, Bin) when Elems =/= [] ->~n", [FnName]),
       f("    SubBin = ~p(Elems, <<>>),~n", [PackedFnName]),
       f("    Bin2 = <<Bin/binary, ~s>>,~n", [KeyTxt]),
       f("    Bin3 = e_varint(byte_size(SubBin), Bin2),~n"),
       f("    <<Bin3/binary, SubBin/binary>>;~n"),
       f("~p([], Bin) ->~n", [FnName]),
       f("    Bin.~n"),
       f("~n"),
       f("~p([Value | Rest], Bin) ->~n", [PackedFnName]),
       f("    Bin2 = ~p(Value, Bin),~n", [ElemEncoderFn]),
       f("    ~p(Rest, Bin2);~n", [PackedFnName]),
       f("~p([], Bin) ->~n", [PackedFnName]),
       f("    Bin.~n~n")].
  
  format_type_encoders(AnRes) ->
      [format_varlength_field_encoders(AnRes),
  
negative.code:     Key = (FNum bsl 3) bor gpb:encode_wiretype(Type),
      varint_to_binary_fields(Key).
  
  mk_field_encode_fn_name(MsgName, #field{occurrence=repeated, name=FName}) ->
      mk_fn(e_field_, MsgName, FName);
  mk_field_encode_fn_name(MsgName, #field{type={msg,_Msg}, name=FName}) ->
  