[M3devel] efficient "safe" Modula-3?

Mika Nystrom mika at async.caltech.edu
Wed Jan 2 13:47:24 CET 2008


Well it's hard (i.e., impossible) to do a safe sprintf in Modula-3
that doesn't require heap allocations.  Fast idiomatic Modula-3
would involve using procedures to append to an ARRAY OF CHAR, and
then passing it a SUBARRAY as you go down the string.

If the format string is constant, as it is in most cases, why don't
you write a printf compiler? :)  Something that can write to a 
Wr.T, maybe.

By the way, from the point of view of performance, the worst piece
of the Modula-3 system that I have found is Scan.LongReal.  It's
horrid!  (At least with PM3, but I think CM3 still has problems.)
The libraries I have that need to read lots of numbers from disk
files use the C routines strtod and strtol for that reason.  The
performance loss here, in my experience, dwarfs any memory
allocator/garbage collector performance issues from the code you
attached...

    Mika


Henning Thielemann writes:
>
>On Tue, 1 Jan 2008, Jay wrote:
>
>> How can I write this in idiomatic Modula-3:
>
>>     date : Date.T    Fmt.FN(                                 (* heap alloc *)        "%04s-%02s-%02s %02s:%02s:%02s",        ARRAY OF TEXT{                      (* heap alloc? easily avoided, but just over t
>he limit *)            Fmt.Int(date.year),             (* heap alloc *)            Fmt.Int(ORD(date.month) + 1),   (* heap alloc *)            Fmt.Int(date.day),              (* heap alloc *)            Fmt.In
>t(date.hour),             (* heap alloc *)            Fmt.Int(date.minute),           (* heap alloc *)            Fmt.Int(date.second)            (* heap alloc *)            })));
>
> I think that this solution is the most elegant one can do. It's still not
>entirely safe, because the number of arguments may not match the number of
>placeholders in the format string. GCC checks this for printf and friends,
>but I'm afraid this is restricted to the standard format strings.
> Even more safe, but maybe less readable and less efficient is
>  Fmt.Int(date.year) & "-" & Fmt.Int(date.month+1) & "-" ...
>
> People have even invented dependently typed languages in order to get
>functions like printf typed in a statically safe manner:
>  http://en.wikipedia.org/wiki/Cayenne_(programming_language)



More information about the M3devel mailing list