Skip to content

Commit

Permalink
[#508] Optimize read method in C++ Array wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
mikir committed Sep 20, 2023
1 parent efc8750 commit 02424c3
Show file tree
Hide file tree
Showing 29 changed files with 235 additions and 235 deletions.
2 changes: 1 addition & 1 deletion compiler/extensions/cpp/freemarker/Choice.cpp.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
<@user_includes cppUserIncludes, false/>
<@namespace_begin package.path/>

<@inner_classes_definition name, fieldList/>
<#if withWriterCode>
<#macro empty_constructor_field_initialization>
m_objectChoice(allocator)
Expand Down Expand Up @@ -536,6 +535,7 @@ void ${name}::write(${name}::ZserioPackingContext&<#if uses_packing_context(fiel
</#if>
<#if fieldList?has_content>

<@inner_classes_definition name, fieldList/>
<#macro choice_read_member member packed indent>
<#local I>${""?left_pad(indent * 4)}</#local>
<#if member.compoundField??>
Expand Down
42 changes: 23 additions & 19 deletions compiler/extensions/cpp/freemarker/CompoundField.inc.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -503,13 +503,15 @@ void ${compoundName}::<@array_expressions_name field.name/>::initializeElement(<
public:
using OwnerType = ${compoundName};

static ${field.array.elementTypeInfo.typeFullName} create(<#if !withWriterCode>const </#if>${compoundName}& owner,
::zserio::BitStreamReader& in, const ${types.allocator.default}& allocator, size_t index);
static void create(<#if !withWriterCode>const </#if>${compoundName}& owner,
<@vector_type_name field.array.elementTypeInfo.typeFullName/>& array,
::zserio::BitStreamReader& in, size_t index);
<#if field.isPackable>

static ${field.array.elementTypeInfo.typeFullName} create(<#if !withWriterCode>const </#if>${compoundName}& owner,
${field.array.elementTypeInfo.typeFullName}::ZserioPackingContext& context, ::zserio::BitStreamReader& in,
const ${types.allocator.default}& allocator, size_t index);
static void create(<#if !withWriterCode>const </#if>${compoundName}& owner,
<@vector_type_name field.array.elementTypeInfo.typeFullName/>& array,
${field.array.elementTypeInfo.typeFullName}::ZserioPackingContext& context,
::zserio::BitStreamReader& in, size_t index);
</#if>
};

Expand All @@ -521,33 +523,35 @@ void ${compoundName}::<@array_expressions_name field.name/>::initializeElement(<
<@compound_field_compound_ctor_params field.array.elementCompound, true/><#t>
</#if>
</#local>
${field.array.elementTypeInfo.typeFullName} ${compoundName}::<@element_factory_name field.name/>::create(<#rt>
void ${compoundName}::<@element_factory_name field.name/>::create(<#rt>
<#if !withWriterCode>const </#if>${compoundName}&<#t>
<#lt><#if needs_field_initialization_owner(field.array.elementCompound)> owner</#if>,
::zserio::BitStreamReader& in, const ${types.allocator.default}& allocator, size_t<#rt>
<#if needs_field_initialization_owner(field.array.elementCompound)> owner</#if>,
<@vector_type_name field.array.elementTypeInfo.typeFullName/>& array,
::zserio::BitStreamReader& in, size_t<#rt>
<#lt><#if needs_field_initialization_index(field.array.elementCompound)> index</#if>)
{
return ${field.array.elementTypeInfo.typeFullName}(in<#rt>
array.emplace_back(in<#rt>
<#if extraConstructorArguments?has_content>
, ${extraConstructorArguments}<#t>
</#if>
<#lt>, allocator);
<#lt>, array.get_allocator());
}
<#if field.isPackable>

${field.array.elementTypeInfo.typeFullName} ${compoundName}::<@element_factory_name field.name/>::create(<#rt>
<#if field.isPackable>
void ${compoundName}::<@element_factory_name field.name/>::create(<#rt>
<#if !withWriterCode>const </#if>${compoundName}&<#t>
<#lt><#if needs_field_initialization_owner(field.array.elementCompound)> owner</#if>,
<#if needs_field_initialization_owner(field.array.elementCompound)> owner</#if>,
<@vector_type_name field.array.elementTypeInfo.typeFullName/>& array,
${field.array.elementTypeInfo.typeFullName}::ZserioPackingContext& context, ::zserio::BitStreamReader& in,
const ${types.allocator.default}& allocator, size_t<#rt>
<#lt><#if needs_field_initialization_index(field.array.elementCompound)> index</#if>)
size_t<#if needs_field_initialization_index(field.array.elementCompound)> index</#if>)
{
return ${field.array.elementTypeInfo.typeFullName}(context, in<#rt>
<#if extraConstructorArguments?has_content>
array.emplace_back(context, in<#rt>
<#if extraConstructorArguments?has_content>
, ${extraConstructorArguments}<#t>
</#if>
<#lt>, allocator);
</#if>
<#lt>, array.get_allocator());
}

</#if>
</#macro>

Expand Down
6 changes: 4 additions & 2 deletions compiler/extensions/cpp/freemarker/Structure.cpp.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
</#if>
</#if>
</#macro>
<@inner_classes_definition name, fieldList/>
<#macro empty_constructor_field_initialization>
<#list fieldList as field>
<@field_member_name field/>(<@field_default_constructor_arguments field/>)<#if field?has_next>,</#if>
Expand Down Expand Up @@ -483,8 +482,11 @@ void ${name}::write(${name}::ZserioPackingContext&<#if uses_packing_context(fiel
}
</#if>
</#if>
<#list fieldList as field>
<#if fieldList?has_content>

<@inner_classes_definition name, fieldList/>
</#if>
<#list fieldList as field>
<@field_member_type_name field, name/> ${name}::${field.readerName}(::zserio::BitStreamReader& in<#rt>
<#if field.needsAllocator || field.holderNeedsAllocator>
<#lt>,
Expand Down
2 changes: 1 addition & 1 deletion compiler/extensions/cpp/freemarker/Union.cpp.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
<#assign choiceTagArrayTraits="::zserio::VarSizeArrayTraits">
<@namespace_begin package.path/>

<@inner_classes_definition name, fieldList/>
<#macro empty_constructor_field_initialization>
m_choiceTag(UNDEFINED_CHOICE)<#rt>
<#if fieldList?has_content>
Expand Down Expand Up @@ -561,6 +560,7 @@ void ${name}::write(${name}::ZserioPackingContext& context, ::zserio::BitStreamW
</#if>
<#if fieldList?has_content>

<@inner_classes_definition name, fieldList/>
${name}::ChoiceTag ${name}::readChoiceTag(::zserio::BitStreamReader& in)
{
return static_cast<${name}::ChoiceTag>(static_cast<int32_t>(in.readVarSize()));
Expand Down
18 changes: 9 additions & 9 deletions compiler/extensions/cpp/runtime/src/zserio/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,31 +216,31 @@ template <typename ARRAY_TRAITS, typename OWNER_TYPE, typename RAW_ARRAY,
!has_allocator<ARRAY_TRAITS>::value, int>::type = 0>
void arrayTraitsRead(const OWNER_TYPE& owner, RAW_ARRAY& rawArray, BitStreamReader& in, size_t index)
{
return rawArray.push_back(ARRAY_TRAITS::read(owner, in, index));
rawArray.push_back(ARRAY_TRAITS::read(owner, in, index));
}

template <typename ARRAY_TRAITS, typename OWNER_TYPE, typename RAW_ARRAY,
typename std::enable_if<has_owner_type<ARRAY_TRAITS>::value &&
has_allocator<ARRAY_TRAITS>::value, int>::type = 0>
void arrayTraitsRead(OWNER_TYPE& owner, RAW_ARRAY& rawArray, BitStreamReader& in, size_t index)
{
return rawArray.push_back(ARRAY_TRAITS::read(owner, in, rawArray.get_allocator(), index));
ARRAY_TRAITS::read(owner, rawArray, in, index);
}

template <typename ARRAY_TRAITS, typename OWNER_TYPE, typename RAW_ARRAY,
typename std::enable_if<!has_owner_type<ARRAY_TRAITS>::value &&
!has_allocator<ARRAY_TRAITS>::value, int>::type = 0>
void arrayTraitsRead(const OWNER_TYPE&, RAW_ARRAY& rawArray, BitStreamReader& in, size_t index)
{
return rawArray.push_back(ARRAY_TRAITS::read(in, index));
rawArray.push_back(ARRAY_TRAITS::read(in, index));
}

template <typename ARRAY_TRAITS, typename OWNER_TYPE, typename RAW_ARRAY,
typename std::enable_if<!has_owner_type<ARRAY_TRAITS>::value &&
has_allocator<ARRAY_TRAITS>::value, int>::type = 0>
void arrayTraitsRead(const OWNER_TYPE&, RAW_ARRAY& rawArray, BitStreamReader& in, size_t index)
{
return rawArray.push_back(ARRAY_TRAITS::read(in, rawArray.get_allocator(), index));
ARRAY_TRAITS::read(rawArray, in, index);
}

// calls the read method properly on packed array traits
Expand All @@ -250,7 +250,7 @@ template <typename PACKED_ARRAY_TRAITS, typename OWNER_TYPE, typename RAW_ARRAY,
void packedArrayTraitsRead(OWNER_TYPE& owner, RAW_ARRAY& rawArray, PACKING_CONTEXT& context,
BitStreamReader& in, size_t index)
{
rawArray.push_back(PACKED_ARRAY_TRAITS::read(owner, context, in, rawArray.get_allocator(), index));
PACKED_ARRAY_TRAITS::read(owner, rawArray, context, in, index);
}

template <typename PACKED_ARRAY_TRAITS, typename OWNER_TYPE, typename RAW_ARRAY, typename PACKING_CONTEXT,
Expand Down Expand Up @@ -280,15 +280,15 @@ template <typename ARRAY_TRAITS, typename OWNER_TYPE,
void arrayTraitsWrite(const OWNER_TYPE& owner,
BitStreamWriter& out, const typename ARRAY_TRAITS::ElementType& element)
{
return ARRAY_TRAITS::write(owner, out, element);
ARRAY_TRAITS::write(owner, out, element);
}

template <typename ARRAY_TRAITS, typename OWNER_TYPE,
typename std::enable_if<!has_owner_type<ARRAY_TRAITS>::value, int>::type = 0>
void arrayTraitsWrite(const OWNER_TYPE&,
BitStreamWriter& out, const typename ARRAY_TRAITS::ElementType& element)
{
return ARRAY_TRAITS::write(out, element);
ARRAY_TRAITS::write(out, element);
}

// call the write method properly on packed array traits
Expand All @@ -297,15 +297,15 @@ template <typename PACKED_ARRAY_TRAITS, typename OWNER_TYPE, typename PACKING_CO
void packedArrayTraitsWrite(const OWNER_TYPE& owner, PACKING_CONTEXT& context,
BitStreamWriter& out, const typename PACKED_ARRAY_TRAITS::ElementType& element)
{
return PACKED_ARRAY_TRAITS::write(owner, context, out, element);
PACKED_ARRAY_TRAITS::write(owner, context, out, element);
}

template <typename PACKED_ARRAY_TRAITS, typename OWNER_TYPE, typename PACKING_CONTEXT,
typename std::enable_if<!has_owner_type<PACKED_ARRAY_TRAITS>::value, int>::type = 0>
void packedArrayTraitsWrite(const OWNER_TYPE&, PACKING_CONTEXT& context,
BitStreamWriter& out, const typename PACKED_ARRAY_TRAITS::ElementType& element)
{
return PACKED_ARRAY_TRAITS::write(context, out, element);
PACKED_ARRAY_TRAITS::write(context, out, element);
}

} // namespace detail
Expand Down
43 changes: 24 additions & 19 deletions compiler/extensions/cpp/runtime/src/zserio/ArrayTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -1444,13 +1444,13 @@ struct BasicBytesArrayTraits
/**
* Reads the single array element.
*
* \param rawArray Raw array to use.
* \param in Bit stream reader.
*
* \return Read element.
*/
static ElementType read(BitStreamReader& in, const allocator_type& allocator, size_t = 0)
template <typename RAW_ARRAY>
static void read(RAW_ARRAY& rawArray, BitStreamReader& in, size_t = 0)
{
return in.readBytes(allocator);
rawArray.emplace_back(in.readBytes(rawArray.get_allocator()));
}

/**
Expand Down Expand Up @@ -1510,13 +1510,13 @@ struct BasicStringArrayTraits
/**
* Reads the single array element.
*
* \param rawArray Raw array to use.
* \param in Bit stream reader.
*
* \return Read element.
*/
static ElementType read(BitStreamReader& in, const allocator_type& allocator, size_t = 0)
template <typename RAW_ARRAY>
static void read(RAW_ARRAY& rawArray, BitStreamReader& in, size_t = 0)
{
return in.readString(allocator);
rawArray.emplace_back(in.readString(rawArray.get_allocator()));
}

/**
Expand Down Expand Up @@ -1576,11 +1576,13 @@ struct BasicBitBufferArrayTraits
/**
* Reads the single array element.
*
* \param rawArray Raw array to use.
* \param in Bit stream reader.
*/
static ElementType read(BitStreamReader& in, const allocator_type& allocator, size_t = 0)
template <typename RAW_ARRAY>
static void read(RAW_ARRAY& rawArray, BitStreamReader& in, size_t = 0)
{
return in.readBitBuffer(allocator);
rawArray.emplace_back(in.readBitBuffer(rawArray.get_allocator()));
}

/**
Expand Down Expand Up @@ -1769,14 +1771,16 @@ class ObjectArrayTraits
/**
* Reads the single array element.
*
* \param elementFactory Factory which knows how to create a single array element.
* \param owner Owner of the array.
* \param rawArray Raw array to use.
* \param in Bit stream reader.
* \param index Index need in case of parameterized type which depends on the current index.
*/
static ElementType read(OwnerType& owner, BitStreamReader& in, const allocator_type& allocator,
size_t index)
template <typename RAW_ARRAY>
static void read(OwnerType& owner, RAW_ARRAY& rawArray,
BitStreamReader& in, size_t index)
{
return ELEMENT_FACTORY::create(owner, in, allocator, index);
ELEMENT_FACTORY::create(owner, rawArray, in, index);
}

/**
Expand Down Expand Up @@ -2219,17 +2223,18 @@ class PackedArrayTraits<ObjectArrayTraits<T, ELEMENT_FACTORY>,
/**
* Reads an element from the bit stream.
*
* \param owner Owner of the current array.
* \param owner Owner of the array.
* \param rawArray Raw array to use.
* \param packingContext Packing context node which keeps the appropriate subtree of contexts.
* \param in Bit stream reader.
* \param allocator Allocator to use.
* \param index Index of the current element.
*/
template <typename PACKING_CONTEXT>
static ElementType read(typename ArrayTraits::OwnerType& owner,
PACKING_CONTEXT& packingContext, BitStreamReader& in, const allocator_type& allocator, size_t index)
template <typename RAW_ARRAY, typename PACKING_CONTEXT>
static void read(typename ArrayTraits::OwnerType& owner, RAW_ARRAY& rawArray,
PACKING_CONTEXT& packingContext, BitStreamReader& in, size_t index)
{
return ELEMENT_FACTORY::create(owner, packingContext, in, allocator, index);
ELEMENT_FACTORY::create(owner, rawArray, packingContext, in, index);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -791,25 +791,21 @@ uint32_t CreatorNested::readValue(CreatorNested::ZserioPackingContext& context,
{
return context.getValue().read<::zserio::StdIntArrayTraits<uint32_t>>(in);
}

::zserio::pmr::string CreatorNested::readText(::zserio::BitStreamReader& in,
const allocator_type& allocator)
{
return static_cast<::zserio::pmr::string>(in.readString(allocator));
}

::zserio::pmr::BitBuffer CreatorNested::readExternData(::zserio::BitStreamReader& in,
const allocator_type& allocator)
{
return static_cast<::zserio::pmr::BitBuffer>(in.readBitBuffer(allocator));
}

::zserio::pmr::vector<uint8_t> CreatorNested::readBytesData(::zserio::BitStreamReader& in,
const allocator_type& allocator)
{
return static_cast<::zserio::pmr::vector<uint8_t>>(in.readBytes(allocator));
}

::test_object::polymorphic_allocator::CreatorEnum CreatorNested::readCreatorEnum(::zserio::BitStreamReader& in)
{
return ::zserio::read<::test_object::polymorphic_allocator::CreatorEnum>(in);
Expand All @@ -819,7 +815,6 @@ ::test_object::polymorphic_allocator::CreatorEnum CreatorNested::readCreatorEnum
{
return ::zserio::read<::test_object::polymorphic_allocator::CreatorEnum>(context.getCreatorEnum(), in);
}

::test_object::polymorphic_allocator::CreatorBitmask CreatorNested::readCreatorBitmask(::zserio::BitStreamReader& in)
{
return ::test_object::polymorphic_allocator::CreatorBitmask(in);
Expand Down
Loading

0 comments on commit 02424c3

Please sign in to comment.