[m-rev.] for review: implement io__read_file_as_string on il backend

Fergus Henderson fjh at cs.mu.OZ.AU
Wed Dec 4 11:35:09 AEDT 2002


Actually, on second thoughts, there is one issue:

On 03-Dec-2002, Peter Ross <pro at missioncriticalit.com> wrote:
> +io__read_into_buffer(Stream, buffer(Array0), Pos0, Size, buffer(Array), Pos) -->
> +	( { Pos0 >= Size } ->
> +		{ Array = Array0 },
> +		{ Pos = Pos0 }
> +	;
> +		io__read_char(Stream, CharResult),
> +		( { CharResult = ok(Char) } ->
> +			{ array__set(Array0, Pos0, Char, Array1) },
> +			io__read_into_buffer(Stream, buffer(Array1), Pos0 + 1,
> +					Size, buffer(Array), Pos)
> +		;
> +			{ Array = Array0 },
> +			{ Pos = Pos0}
> +		)
> +	).

With the current implementation, which for .NET does not optimize no-tag types,
it would be more efficient to avoid constructing buffer/1 values at each
iteration of the loop.

Also, you could use array__unsafe_set here
(which on .NET is in fact range-checked anyway).

Hence...

io__read_into_buffer(Stream, buffer(Array0), Pos0, Size, buffer(Array), Pos) -->
	io__read_into_array(Stream, Array0, Pos0, Size, Array, Pos).

:- pred io__read_into_array(stream::in, array(char)::di, int::in, int::in,
	array(char)::uo, int::out, io__state::di, io__state::uo) is det.

io__read_into_array(Stream, Array0, Pos0, Size, Array, Pos) -->
	( { Pos0 >= Size } ->
		{ Array = Array0 },
		{ Pos = Pos0 }
	;
		io__read_char(Stream, CharResult),
		( { CharResult = ok(Char) } ->
			{ array__unsafe_set(Array0, Pos0, Char, Array1) },
			io__read_into_array(Stream, Array1, Pos0 + 1,
					Size, Array, Pos)
		;
			{ Array = Array0 },
			{ Pos = Pos0}
		)
	).

Also, it would be more efficient to use a char_array type, which could be
defined in C# as char[], rather than array(char).  For Java or .NET-style
implementations of array, this would be faster, since indexing into
an array whose element type is known statically avoids some overhead
that is required when the element type isn't determined statically.
(For C-style implementations of array, char_array could use a lot less
memory than array(char).)  The current implementation is OK for now,
but it would be good to have an XXX comment about this.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list