Article View: comp.arch
Article #35Re: Rust (was: Optimization by programmers)
From: Anton Ertl
Date: Sun, 11 Oct 2026 09:27
Date: Sun, 11 Oct 2026 09:27
70 lines
3561 bytes
3561 bytes
Michael S <already5chosen@yahoo.com> writes:
>On Tue, 06 Oct 2026 08:29:19 GMT
>anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:
>> My goal was to implement search tree insertion as efficiently and
>> compactly as in Pascal,
>
>I suppose, you mean some extended Pascal dialect here, because I can not
>imagine how you code an example below in Standard Pascal.
>Then again, I learned Pascal ~40 years ago and didn't use it in
>practice since 1989 or 1990, so can easily miss something.
Page 75 and 76 of my copy of "Programming in Modula-2" (Second Edition)
contain this:
TreePtr = POINTER TO TreeNode;
TreeNode =
Record
key: CARDINAL;
data: ...
left, right: TreePtr;
END;
PROCEDURE search(VAR p: TreePtr; x: CARDINAL): TreePtr;
BEGIN
IF p # NIL THEN
IF p^.key < x THEN
RETURN search(p^.right,x)
ELSIF p^.key > x THEN
RETURN search(p^.left, x)
ELSE
RETURN p
END
ELSE (* not found, hence insert *)
NEW(p);
WITH p^ DO
key := x;
left := NIL;
right := NIL;
END;
RETURN p
END
END search
You would invoke this with
search(root,needle)^.data := ...;
in order to actually change the field "data", performing the insert
functionality.
One would not want to use this procedure for lookup, given that it
inserts a node with an uninitialized data field when the key is not
found. Wirth gives a loop implementation of lookup before the
PROCEDURE search.
Note that the "VAR" in front of the p makes p a reference parameter
rather than a value parameter (like x), which is key to making this
work for insertion. This language feature is also present in Pascal;
the Pascal syntax is different, but I don't think that prevents
implementing this approach.
Using recursion is necessary here because Modula-2 and Pascal support
references to variables or fields only for parameters. But the
recursive calls are tail-recursive (I don't think that Wirth's
Modula-2 or Parcal compilers optimized tail recursion, though).
- anton
--
'Anyone trying for "industrial quality" ISA should avoid undefined behavior.'
Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Technical Details
Message-ID:
<2026Oct11.112704@mips.complang.tuwien.ac.at>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<1177fdi$26ea4$1@dont-email.me> <119otm8$2c89q$2@dont-email.me> <86ik3jo1js.fsf@linuxsc.com> <2026Oct3.131214@mips.complang.tuwien.ac.at> <11a0662$gic$1@reader2.panix.com> <2026Oct6.102919@mips.complang.tuwien.ac.at> <20261010222854.0000591c@yahoo.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: anton@mips.complang.tuwien.ac.at (Anton Ertl) Newsgroups: comp.arch Subject: Re: Rust (was: Optimization by programmers) Date: Sun, 11 Oct 2026 09:27:04 GMT Organization: Institut fuer Computersprachen, Technische Universitaet Wien Lines: 70 Message-ID: <2026Oct11.112704@mips.complang.tuwien.ac.at> References: <1177fdi$26ea4$1@dont-email.me> <119otm8$2c89q$2@dont-email.me> <86ik3jo1js.fsf@linuxsc.com> <2026Oct3.131214@mips.complang.tuwien.ac.at> <11a0662$gic$1@reader2.panix.com> <2026Oct6.102919@mips.complang.tuwien.ac.at> <20261010222854.0000591c@yahoo.com> Injection-Date: Sun, 11 Oct 2026 09:55:48 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2657545"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18tgJMbHDYzmXd4d4lti6x8"; posting-host="048ebf825c3e0ead5be8fd534130ebc5" Cancel-Lock: sha1:WO+TEutNI6a6EycKOXCaJ7igzXc= sha256:gNg4fVGwKo+mPrvXCefqZ+OM06wkzaHpi54z2ztiFwU= sha1:Dr2YTFG3t9PZehuBH/G8WBHd2gY= sha256:rXCxcRkDO5MDbtFmkntvzSNylmpyDtwQJ2PzfXQzf34= X-newsreader: xrn 10.11