comp.lang.ada
 help / color / mirror / Atom feed
* Function result = controlled type
@ 1998-11-18  0:00 adam
  1998-11-19  0:00 ` Dmitriy Anisimkov
  1998-11-19  0:00 ` Tucker Taft
  0 siblings, 2 replies; 5+ messages in thread
From: adam @ 1998-11-18  0:00 UTC (permalink / raw)




Suppose I want to write a function that strings together an array of strings:

    use Ada.Strings.Unbounded;
    function String_Together (Arr : String_Array) return Unbounded_String is
	Result : Unbounded_String;
    begin
        Result := Null_Unbounded_String;
	for I in Arr'range loop
	    if I /= Arr'first then
		Append (Result, ", ");
	    end if;
	    Append (Result, Arr (I).all);
	end loop;
	return Result;
    end String_Together;

Simple enough?  Now, suppose Unbounded_String is implemented as suggested in
the Rationale, i.e. derived from Ada.Finalization.Controlled.  Its
implementation is an access to a string, and the Adjust procedure calls
new String' on the contents of the object's string, to make a new copy of
the string.  Finalize deallocates the data pointed to by the access, using
Unchecked_Deallocation.

As I read the RM, the statement "return Result" causes Adjust to be called
when Result is assigned into an anonymous object (6.5(21)).  Also, when
String_Together is exited, Result must be finalized, which means the string
created for Result is deallocated.  This means that after creating the string,
the "return" statement copies this string to a new location and then
deallocates the old one, a waste of time.  Now, I realize that someone who
cares about this wouldn't have implemented the String_Together function with
a bunch of Appends, each of which deallocates a string and reallocates a new
one.  (I would have gone through Arr first to compute the length of the
result.)  Still, I think the idea that "return" causes probably needless
adjusting and finalization operations to take place is a cause for some
concern.

Questions:

(1) Is there a way, in String_Together, to write code so that Result can be
    returned without Adjust and Finalize being called?

(2) Assuming there is a way, would it be desirable to do so (given that
    Unbounded_String is a defined as a private type and that the code for
    String_Together shouldn't rely on any assumptions about how this private
    type is implemented)?

(3) Is the compiler permitted to delete the Adjust and Finalize calls in this
 case?	It doesn't look like 7.6(18ff) applies here, since the assignment  is
not part of an assignment_statement, and because the finalization  referred
to by this part of the RM is one of the intermediate finalizations  that
takes place in an assignment statement, not the finalization of an  object
that goes out of scope.

(4) Should the Rationale have suggested using reference counters?  [1/2 :-)
    But then would someone have complained about the needless incrementing and
    decrementing of the counter?]

				-- thanks, Adam

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




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

* Re: Function result = controlled type
  1998-11-18  0:00 Function result = controlled type adam
  1998-11-19  0:00 ` Dmitriy Anisimkov
@ 1998-11-19  0:00 ` Tucker Taft
  1 sibling, 0 replies; 5+ messages in thread
From: Tucker Taft @ 1998-11-19  0:00 UTC (permalink / raw)


adam@irvine.com wrote:

: ...
: As I read the RM, the statement "return Result" causes Adjust to be called
: when Result is assigned into an anonymous object (6.5(21)).  Also, when
: String_Together is exited, Result must be finalized, which means the string
: created for Result is deallocated.  This means that after creating the string,
: the "return" statement copies this string to a new location and then
: deallocates the old one, a waste of time.  Now, I realize that someone who
: cares about this wouldn't have implemented the String_Together function with
: a bunch of Appends, each of which deallocates a string and reallocates a new
: one.  (I would have gone through Arr first to compute the length of the
: result.)  Still, I think the idea that "return" causes probably needless
: adjusting and finalization operations to take place is a cause for some
: concern.

: Questions:

: ...
: (3) Is the compiler permitted to delete the Adjust and Finalize calls in this
:  case?	It doesn't look like 7.6(18ff) applies here, since the assignment  is
: not part of an assignment_statement, and because the finalization  referred
: to by this part of the RM is one of the intermediate finalizations  that
: takes place in an assignment statement, not the finalization of an  object
: that goes out of scope.

The intent was always that such optimizations were permitted, but
the wording in 7.6 was a bit unclear.  Hence, there is 
a new "AI" (Ada Interpretation) which explicitly permits more aggressive 
optimization.  The basic rules are that the compiler may remove unnecessary 
temps and the corresponding pairs of adjusts and finalizes.  The rules
have been tightened a bit about whether the compiler may "move"
an object without adjusting it in its new location, and finalizing
it in its old location.  It used to be disallowed only if the object
contained aliased subcomponents.  Now it is also disallowed if
there might be pointers to the object as a whole which need to
be adjusted.  A compiler might "know" additional things about
Unbounded_String which would permit further optimizations.

: ...
: 				-- thanks, Adam

--
-Tucker Taft   stt@inmet.com   http://www.inmet.com/~stt/
Intermetrics, Inc.  Burlington, MA  USA
An AverStar Company




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

* Re: Function result = controlled type
  1998-11-18  0:00 Function result = controlled type adam
@ 1998-11-19  0:00 ` Dmitriy Anisimkov
  1998-11-19  0:00   ` Ed Falis
  1998-11-19  0:00   ` Tom Moran
  1998-11-19  0:00 ` Tucker Taft
  1 sibling, 2 replies; 5+ messages in thread
From: Dmitriy Anisimkov @ 1998-11-19  0:00 UTC (permalink / raw)


[-- Attachment #1: Type: text/plain, Size: 219 bytes --]

I have maked experiment example which placed to the attachment. Each
finalization causes text output to console. GNAT 3-10 does more
finalizations than Aonix ObjectAda 7.1. ObjectAda not finalizes Result
local variable.

[-- Attachment #2: contr4test.adb --]
[-- Type: application/x-unknown-content-type-adb_auto_file, Size: 163 bytes --]

[-- Attachment #3: contr4test.ads --]
[-- Type: application/x-unknown-content-type-ads_auto_file, Size: 247 bytes --]

[-- Attachment #4: tstfin.adb --]
[-- Type: application/x-unknown-content-type-adb_auto_file, Size: 338 bytes --]

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

* Re: Function result = controlled type
  1998-11-19  0:00 ` Dmitriy Anisimkov
@ 1998-11-19  0:00   ` Ed Falis
  1998-11-19  0:00   ` Tom Moran
  1 sibling, 0 replies; 5+ messages in thread
From: Ed Falis @ 1998-11-19  0:00 UTC (permalink / raw)


This is what I get with OA 7.1.2 on windows:

Enter
Finalize u
Finalize r
Leave
Finalize r


The value of U is finalized as part of the assignment on exiting 
the function, then R (the tempo function result), then R which is 
the last value of U.

Current versions and all that...

- Ed Falis
Aonix




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

* Re: Function result = controlled type
  1998-11-19  0:00 ` Dmitriy Anisimkov
  1998-11-19  0:00   ` Ed Falis
@ 1998-11-19  0:00   ` Tom Moran
  1 sibling, 0 replies; 5+ messages in thread
From: Tom Moran @ 1998-11-19  0:00 UTC (permalink / raw)


In testing CLAW we found a bug in OA 7.1 about over agressive
adjust/finalization optimization, as I recall.  It is fixed in OA
7.1.2




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

end of thread, other threads:[~1998-11-19  0:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-11-18  0:00 Function result = controlled type adam
1998-11-19  0:00 ` Dmitriy Anisimkov
1998-11-19  0:00   ` Ed Falis
1998-11-19  0:00   ` Tom Moran
1998-11-19  0:00 ` Tucker Taft

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