comp.lang.ada
 help / color / mirror / Atom feed
* Re: Problem with inefficiency and records with variant parts
@ 1992-10-06 15:58 Robert I. Eachus
  0 siblings, 0 replies; 4+ messages in thread
From: Robert I. Eachus @ 1992-10-06 15:58 UTC (permalink / raw)


In article <SIDOU.92Oct5232754@litsun.epfl.ch> sidou@litsun.epfl.ch (Dominique 
Sidou) writes:

   We have a big problem with records variant an efficiency...
   we now need to do the following...

   v.s := 2;
   fill(v.c2);

   with: procedure fill (c : in out BigStruct2);

   We want to avoid casting v in the following way before
   the call to fill:

   declare
     bigstruct2var : BigStruct2;
   begin
     v := (2, bigstruct2var); -- very inefficient and useless in our case
     fill(v.c2);
   end;


   Any Comments, Ideas...???

   The problem is one of different levels of abstraction.  Procedure
fill expects objects of type BigStruct2, and you want to operate on on
objects of type t.  Two possible solution approaches:

   First approach is to wrap fill to return a value of type t:

   function Fill return T is 
     Temp: t(2);
   begin
     fill(Temp.c2)
     return Temp;
   end Fill;
   ...
   v := Fill;

   (You could make it a procedure if you like, in that case you have
to depend on your optimizer to be more sophisticated than in this case.)

   Second approach is to redefine fill to return a value of type
BigStruct2, to be a procedure with an in out parameter of type, or to
match the template above.  Any of these should work without depending
on the optimizer.

   You may have simplified things in this example to eliminate things
like bounds on the component types which are in fact unconstrained
array types.  In that case it is probably easier on the reader of the
code to have function calls with explicit size parameters, but poor
optimizers will work better with in out parameters.


--

					Robert I. Eachus

with STANDARD_DISCLAIMER;
use  STANDARD_DISCLAIMER;
function MESSAGE (TEXT: in CLEVER_IDEAS) return BETTER_IDEAS is...

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Problem with inefficiency and records with variant parts
@ 1992-10-07 12:03 news.univie.ac.at!chx400!sicsun!disuns2!disuns2.epfl.ch!sidou
  0 siblings, 0 replies; 4+ messages in thread
From: news.univie.ac.at!chx400!sicsun!disuns2!disuns2.epfl.ch!sidou @ 1992-10-07 12:03 UTC (permalink / raw)


First, I want to reformulate the problem, I think it
was not enough precise in my first post:

Suppose:

PACKAGE qwe IS 

  subtype t1 is Integer;
  type t2 is array(1..1000) of Integer;
  type t3 is array (1..1000) of Boolean;

  subtype st is integer range 1..3;

  type t (s : st := 1) is record
    case s is
      when 1 => c1 : t1;
      when 2 => c2 : t2;
      when 3 => c3 : t3;
    end case;
  end record;

  procedure fillt1(v1 : in out t1);
  procedure fillt2(v2 : in out t2);
  procedure fillt3(v3 : in out t3);
  procedure fillt(v : in out t; s : in st);

END qwe;

package body qwe is

  procedure fillt1(v1 : in out t1) is
  begin
    v1 := 1;
  end fiilt1;

  procedure fillt2(v2 : in out t2) is
  begin
    for i in v2'range loop
      v2(i) := 2;
    end loop;
  end fillt2

  procedure fillt3(v3 : in out t3) is
  begin
    for i in v2'range loop
      v3(i) := true;
    end loop;
  end fillt3;

  procedure fillt(v : in out t; s : in st) is
  begin
      -- ?? How can I Do ??
      -- to cast v to v(s) and then call
      -- the corresponding filling procedure
      -- without any copy of a structure of
      -- type t2 or t3.
    case s is
      when 1 =>
	v := (s => 1, c1 => 0);
        fillt1(v.c1);
      when 2 =>
        v := (s => 2, c2 => (others => 0)); -- useless copy to avoid !!
        fillt1(v.c2);
      when 3 =>
	v := (s => 3, c3 => (others => false)); -- useless copy to avoid !!
        fillt1(v.c3);
    end case;
  end fillt;

end qwe;

I think now the problem is well formulated.

Precisions:
1: I know that if my components were
references it would be easy. But i have to deal
with static structures.

2: Working with Verdix Ada 6.0.3 on Sun4/SunOs 4.1.1

Thanks in advance for any ideas,

/DS.

Sidou Dominique, Ecole Polytechnique Federale de Lausanne,
DI-LIT-INN138, Ecublens, CH-1015 Lausanne, Suisse
tel: 693.26.48. e-mail: sidou@litsun.epfl.ch
--

Sidou Dominique, Ecole Polytechnique Federale de Lausanne,
DI-LIT-INN138, Ecublens, CH-1015 Lausanne, Suisse
tel: 693.26.48. e-mail: sidou@litsun.epfl.ch

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Problem with inefficiency and records with variant parts
@ 1992-10-07 13:34 deccrl!caen!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!news.sei.cmu.edu
  0 siblings, 0 replies; 4+ messages in thread
From: deccrl!caen!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!news.sei.cmu.edu @ 1992-10-07 13:34 UTC (permalink / raw)


In article <SIDOU.92Oct7130337@litsun.epfl.ch> sidou@litsun.epfl.ch (Dominique 
Sidou) writes:

>  procedure fillt2(v2 : in out t2) is
>  begin
>    for i in v2'range loop
>      v2(i) := 2;
>    end loop;
>  end fillt2

Change this to 

	Function Fillt2 return t2 is
	begin
	    return t2'(others => 2);
	end;

>        v := (s => 2, c2 => (others => 0)); -- useless copy to avoid !!
>        fillt1(v.c2);

and change this to

	v := (s=>2, c2=>Fillt2);

Any reasonable optimising compiler should be able to reduce this to
a single copy into the correct component of the LHS, especially as
the base type of the array is unconstrained.

Hope that helps.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Problem with inefficiency and records with variant parts
@ 1992-10-07 16:15 Dave Collard x7468
  0 siblings, 0 replies; 4+ messages in thread
From: Dave Collard x7468 @ 1992-10-07 16:15 UTC (permalink / raw)


In <SIDOU.92Oct7130337@litsun.epfl.ch> sidou@litsun.epfl.ch (Dominique Sidou) w
rites:


>First, I want to reformulate the problem, I think it
>was not enough precise in my first post:

>Suppose:

>PACKAGE qwe IS 

>  subtype t1 is Integer;
>  type t2 is array(1..1000) of Integer;
>  type t3 is array (1..1000) of Boolean;

>  subtype st is integer range 1..3;

>  type t (s : st := 1) is record
>    case s is
>      when 1 => c1 : t1;
>      when 2 => c2 : t2;
>      when 3 => c3 : t3;
>    end case;
>  end record;

>  procedure fillt1(v1 : in out t1);
>  procedure fillt2(v2 : in out t2);
>  procedure fillt3(v3 : in out t3);
>  procedure fillt(v : in out t; s : in st);

>END qwe;

>package body qwe is

>  procedure fillt1(v1 : in out t1) is
>  begin
>    v1 := 1;
>  end fiilt1;

>  procedure fillt2(v2 : in out t2) is
>  begin
>    for i in v2'range loop
>      v2(i) := 2;
>    end loop;
>  end fillt2

>  procedure fillt3(v3 : in out t3) is
>  begin
>    for i in v2'range loop
>      v3(i) := true;
>    end loop;
>  end fillt3;

>  procedure fillt(v : in out t; s : in st) is
>  begin
>      -- ?? How can I Do ??
>      -- to cast v to v(s) and then call
>      -- the corresponding filling procedure
>      -- without any copy of a structure of
>      -- type t2 or t3.
>    case s is
>      when 1 =>
>	v := (s => 1, c1 => 0);
>        fillt1(v.c1);
>      when 2 =>
>        v := (s => 2, c2 => (others => 0)); -- useless copy to avoid !!
>        fillt1(v.c2);
>      when 3 =>
>	v := (s => 3, c3 => (others => false)); -- useless copy to avoid !!
>        fillt1(v.c3);
>    end case;
>  end fillt;
--This does not do a copy on my machine:

  procedure fillt(v : out t; s : in st) is
    ret : t(s);
  begin
    case s is
      when 1 =>
        fillt1(ret.c1);
      when 2 =>
        fillt2(ret.c2);
      when 3 =>
        fillt3(ret.c3);
    end case;
    v := ret;
  end fillt;

>end qwe;

>I think now the problem is well formulated.

>Precisions:
>1: I know that if my components were
>references it would be easy. But i have to deal
>with static structures.

>2: Working with Verdix Ada 6.0.3 on Sun4/SunOs 4.1.1

>Thanks in advance for any ideas,

>/DS.

>Sidou Dominique, Ecole Polytechnique Federale de Lausanne,
>DI-LIT-INN138, Ecublens, CH-1015 Lausanne, Suisse
>tel: 693.26.48. e-mail: sidou@litsun.epfl.ch
>--

>Sidou Dominique, Ecole Polytechnique Federale de Lausanne,
>DI-LIT-INN138, Ecublens, CH-1015 Lausanne, Suisse
>tel: 693.26.48. e-mail: sidou@litsun.epfl.ch

--Thor
dlc@ddsdx2.jhuapl.edu
collard@capsrv.jhuapl.edu

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1992-10-07 16:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1992-10-06 15:58 Problem with inefficiency and records with variant parts Robert I. Eachus
  -- strict thread matches above, loose matches on Subject: below --
1992-10-07 12:03 news.univie.ac.at!chx400!sicsun!disuns2!disuns2.epfl.ch!sidou
1992-10-07 13:34 deccrl!caen!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!news.sei.cmu.edu
1992-10-07 16:15 Dave Collard x7468

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox