real-logic / simple-binary-encoding

Simple Binary Encoding (SBE) - High Performance Message Codec
Apache License 2.0
3.12k stars 523 forks source link

about sbe:message Car's blocklength value. #949

Closed kuangtu closed 1 year ago

kuangtu commented 1 year ago

I compile the project and run ExampleUsingGeneratedStub successfully. In generated-src CarEncoder.java:

    public static final int BLOCK_LENGTH = 45;
    public static final int TEMPLATE_ID = 1;
    public static final int SCHEMA_ID = 1;
    public static final int SCHEMA_VERSION = 0;

The BLOCK_LENGTH is 45.from the "Simple_Binary_Encoding_V1.0_with_Errata_November_2020.pdf", it shows the "blocklength" is total space reserved for the root level of the message not counting any repeating groups or variable-length fields.

the message schema.xml is:

<sbe:message name="Car" id="1" description="Description of a basic Car">
    <field name="serialNumber" id="1" type="uint64"/>
    <field name="modelYear" id="2" type="ModelYear"/>
    <field name="available" id="3" type="BooleanType"/>
    <field name="code" id="4" type="Model"/>
    <field name="someNumbers" id="5" type="someNumbers"/>
    <field name="vehicleCode" id="6" type="VehicleCode"/>
    <field name="extras" id="7" type="OptionalExtras"/>
    <field name="discountedModel" id="8" type="Model" presence="constant" valueRef="Model.C"/>
    <field name="engine" id="9" type="Engine"/>
    <group name="fuelFigures" id="10" dimensionType="groupSizeEncoding">
        <field name="speed" id="11" type="uint16"/>
        <field name="mpg" id="12" type="float"/>
        <data name="usageDescription" id="200" type="varAsciiEncoding"/>
    </group>
    <group name="performanceFigures" id="13" dimensionType="groupSizeEncoding">
        <field name="octaneRating" id="14" type="Ron"/>
        <group name="acceleration" id="15" dimensionType="groupSizeEncoding">
            <field name="mph" id="16" type="uint16"/>
            <field name="seconds" id="17" type="float"/>
        </group>
    </group>
    <data name="manufacturer" id="18" type="varStringEncoding"/>
    <data name="model" id="19" type="varStringEncoding"/>
    <data name="activationCode" id="20" type="varAsciiEncoding"/>
</sbe:message>

I caculate the blocklength is:

     <field name="serialNumber" id="1" type="uint64"/> 8 
    <field name="modelYear" id="2" type="ModelYear"/>2 uint16
    <field name="available" id="3" type="BooleanType"/>1 uint8
    <field name="code" id="4" type="Model"/>1 char
    <field name="someNumbers" id="5" type="someNumbers"/>4 uint32
    <field name="vehicleCode" id="6" type="VehicleCode"/>6 char" length="6
    <field name="extras" id="7" type="OptionalExtras"/>1 uint8
    <field name="discountedModel" id="8" type="Model" presence="constant" valueRef="Model.C"/> need not be transimitted
    <field name="engine" id="9" type="Engine"/>10

    <composite name="Engine">
        <type name="capacity" primitiveType="uint16"/>2
        <type name="numCylinders" primitiveType="uint8"/>1
        <type name="maxRpm" primitiveType="uint16" presence="constant">9000</type>  need not be transimitted
        <type name="manufacturerCode" primitiveType="char" length="3"/>3
        <type name="fuel" primitiveType="char" presence="constant">Petrol</type>  need not be transimitted
        <ref name="efficiency" type="Percentage"/>1
        <ref name="boosterEnabled" type="BooleanType"/>1
        <ref name="booster" type="Booster"/>2
    </composite> 

    the sum field is:33.

Ref the specification "The block size must be at least the sum of lengths of all fields at the root level of the message, and that is its default value. However, it may be set larger to reserve more space to effect alignment of blocks. This is specified by setting the blockLength attribute in a message schema." if the blocklength is larger than reserve more space, settting by "blockLength attribute in a message schema." Is there any wrong with my calclucation? Thanks!

ksergey commented 1 year ago
        <field name="serialNumber" id="1" type="uint64"/> <!-- 8 -->
        <field name="modelYear" id="2" type="ModelYear"/> <!-- 2 -->
        <field name="available" id="3" type="BooleanType"/> <!-- 1 -->
        <field name="code" id="4" type="Model"/> <!-- 1 -->
        <field name="someNumbers" id="5" type="someNumbers"/> <!-- 4 * 4 = 16 -->
        <field name="vehicleCode" id="6" type="VehicleCode"/> <!-- 6 -->
        <field name="extras" id="7" type="OptionalExtras"/> <!-- 1 -->
        <field name="discountedModel" id="8" type="Model" presence="constant" valueRef="Model.C"/> <!-- 0 -->
        <field name="engine" id="9" type="Engine"/> <!-- 10 -->

total = 45

looks like your calculation not includes actual size of someNumbers which is 4*4=16 (not 4)

kuangtu commented 1 year ago
        <field name="serialNumber" id="1" type="uint64"/> <!-- 8 -->
        <field name="modelYear" id="2" type="ModelYear"/> <!-- 2 -->
        <field name="available" id="3" type="BooleanType"/> <!-- 1 -->
        <field name="code" id="4" type="Model"/> <!-- 1 -->
        <field name="someNumbers" id="5" type="someNumbers"/> <!-- 4 * 4 = 16 -->
        <field name="vehicleCode" id="6" type="VehicleCode"/> <!-- 6 -->
        <field name="extras" id="7" type="OptionalExtras"/> <!-- 1 -->
        <field name="discountedModel" id="8" type="Model" presence="constant" valueRef="Model.C"/> <!-- 0 -->
        <field name="engine" id="9" type="Engine"/> <!-- 10 -->

total = 45

looks like your calculation not includes actual size of someNumbers which is 4*4=16 (not 4)

Yes!the type is:

primitiveType is uint32, 4 bytes, and length is 4, sum is 4*4 = 16. Thanks!