eProsima / Fast-DDS-Gen

Fast-DDS IDL code generator tool. Looking for commercial support? Contact info@eprosima.com
Apache License 2.0
82 stars 62 forks source link

Why don't 1-byte data types call cdr_alignment for byte alignment? #360

Open Jackie2chen opened 5 months ago

Jackie2chen commented 5 months ago

Byte Alignment Rule and Zero-Copy Support

Due to this byte alignment rule, case 2 does not support zero-copy, but case 1 does.

Byte Alignment Code

switch (getKind()) {
    case com.eprosima.idl.parser.typecode.Kind.KIND_LONG:
    case com.eprosima.idl.parser.typecode.Kind.KIND_ULONG:
    case com.eprosima.idl.parser.typecode.Kind.KIND_FLOAT:
        current_alignment += 4 + TypeCode.cdr_alignment(current_alignment, 4);
        break;
    case com.eprosima.idl.parser.typecode.Kind.KIND_SHORT:
    case com.eprosima.idl.parser.typecode.Kind.KIND_USHORT:
        current_alignment += 2 + TypeCode.cdr_alignment(current_alignment, 2);
        break;
    case com.eprosima.idl.parser.typecode.Kind.KIND_BOOLEAN:
    case com.eprosima.idl.parser.typecode.Kind.KIND_CHAR:
    case com.eprosima.idl.parser.typecode.Kind.KIND_OCTET:
    case com.eprosima.idl.parser.typecode.Kind.KIND_INT8:
    case com.eprosima.idl.parser.typecode.Kind.KIND_UINT8:
        current_alignment += 1;
        break;
}

Case 1: Supports Zero-Copy

module DDS {
#define MAX_SIZE (1920 * 1200 * 3)
    struct Header {
        unsigned long long eightbytes0;
        long fourbytes1;
        long fourbytes2;
        long fourbytes3;
        char onebyte1;
    };
    struct LoanableHelloWorld {
        long fourbytes1;
        long fourbytes2;
        long fourbytes3;
        Header header;
        unsigned long fourbytes4;
        unsigned long fourbytes5;
        long fourbytes6;
    };
};

Case 1 Screenshot

Case 2: Does Not Support Zero-Copy

module DDS {
#define MAX_SIZE (1920 * 1200 * 3)
    struct Header {
        unsigned long long eightbytes0;
        long fourbytes1;
        long fourbytes2;
        long fourbytes3;
        char onebyte;
    };
    struct LoanableHelloWorld {
        long fourbytes1;
        long fourbytes2;
        long fourbytes3;
        Header header;
        char onebyte;
        unsigned long fourbytes4;
        unsigned long fourbytes5;
        long fourbytes6;
    };
};

屏幕截图 2024-06-21 161430

is that a bug?

MiguelCompany commented 5 months ago

@Jackie2chen Nested structures don't usually play well with Zero-Copy. See https://github.com/eProsima/Fast-DDS-Gen/issues/193 for a nice discussion on this

Jackie2chen commented 5 months ago

Thank you for your reply. But why is it necessary to consider the CDR specification when using zero-copy? What is the significance of this operation? Is it because OMG requires it to be implemented this way?

Is it possible to determine whether a struct in the IDL supports zero-copy through a script or other means without compiling the generated code?

I look forward to your professional response.