Ada 95 Quality and Style Guide Chapter 10

Chapter 10: Improving Performance - TOC - 10.5 ALGORITHMS

10.5.8 Subprogram Dispatching

guideline

  • Use static subprogram dispatching when measured performance indicates.
  • example

    The term "static dispatching" in this example refers to the use of if/elsif sequences to explicitly determine which subprograms to call based on certain conditions:

        -- (1) Dispatching where tag is not known at compile time
        --     (See ACES V2.0 test "a9_ob_class_wide_dynamic_01")
        -- Object_Type is a tagged type
        -- The_Pointer designates Object_Type'Class;
        -- Subclass1_Pointer designates Subclass1 (derived from Object_Type)
        -- Subclass2_Pointer designates Subclass2 (derived from Subclass1)
        -- Subclass3_Pointer designates Subclass3 (derived from Subclass2)
        Random_Value := Simple_Random; -- Call to a random number generator
        if Random_Value < 1.0/3.0 then
           The_Pointer := Subclass1_Pointer;
        elsif Random_Value > 2.0/3.0 then
           The_Pointer := Subclass2_Pointer;
        else
           The_Pointer := Subclass3_Pointer;
        end if;
        Process (The_Pointer.all);  -- Tag is unknown
        -- (2) Tag is determinable at compile time (static dispatching)
        --     (See ACES V2.0, test "a9_ob_class_wide_static_01")
        -- Object_Type is a tagged type
        -- The_Pointer designates Object_Type'Class;
        -- Subclass1_Pointer designates Subclass1 (derived from Object_Type)
        -- Subclass2_Pointer designates Subclass2 (derived from Subclass1)
        -- Subclass3_Pointer designates Subclass3 (derived from Subclass2)
        Random_Value := Simple_Random; -- Call to a random number generator
        if Random_Value < 1.0/3.0 then
           Process (Subclass1_Pointer.all);
        elsif Random_Value > 2.0/3.0 then
           Process (Subclass2_Pointer.all);
        else
           Process (Subclass3_Pointer.all);
        end if;
        -- (3) No tagged types are involved (no dispatching)
        --     (See ACES V2.0, test "ap_ob_class_wide_01")
        -- Object_type is a discriminated type with variants; possible
        -- discriminant values are Subclass1, Subclass2, and Subclass3
        -- All the pointers designate values of Object_Type
        -- Subclass1_Pointer := new Object_Type (Subclass1);
        -- Subclass2_Pointer := new Object_Type (Subclass2);
        -- Subclass3_Pointer := new Object_Type (Subclass3);
        -- There is only one "Process" procedure (operating on Object_Type)
        Random_Value := Simple_Random; -- Call to a random number generator
        if Random_Value < 1.0/3.0 then
           Process (Subclass1_Pointer.all);
        elsif Random_Value > 2.0/3.0 then
           Process (Subclass2_Pointer.all);
        else
           Process (Subclass3_Pointer.all);
        end if;
    

    rationale

    Determine the impact of dynamic and static subprogram dispatching. The compiler may generate much more efficient code for one form of dispatching than the other.

    notes

    Dynamic dispatching will almost certainly be more efficient than an explicit if . . . elsif sequence. However, you should be aware of any optimizing decisions made by a compiler that might affect this situation.


    < Previous Page Search Contents Index Next Page >
    1 2 3 4 5 6 7 8 9 10 11
    TOC TOC TOC TOC TOC TOC TOC TOC TOC TOC TOC
    Appendix References Bibliography