unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Po Lu via "Emacs development discussions." <emacs-devel@gnu.org>
To: Troy Hinckley <comms@dabrev.com>
Cc: emacs-devel@gnu.org
Subject: Re: Consideration for Rust contributions in Emacs
Date: Sun, 22 Jan 2023 15:44:19 +0800	[thread overview]
Message-ID: <871qnn6mjw.fsf@yahoo.com> (raw)
In-Reply-To: <b972c046-8c29-42eb-92c9-0d5b5fe03551@Spark> (Troy Hinckley's message of "Sat, 21 Jan 2023 15:48:28 -0700")

Troy Hinckley <comms@dabrev.com> writes:

> Let assume for the sake of this discussion that there was a some Rust
> code that someone wanted to contribute and the maintainers wanted the
> functionality it provided. What would be the consideration/objections?

It is hard to say for certain, because you have not said what code you
have in mind.

> 1 The Rust tool-chain is Apache licensed and so is LLVM. There is work
> on a GCC backend, but it is not production ready yet. Would Emacs
> allow the current Rust tool-chain?

No.  Emacs code for a platform where GCC is available must be written so
that it can be compiled with GCC.  We already have this policy in place,
and it prevents us from using Objective-C features that are only
supported by Clang in the Nextstep.

> 2 LLVM (and hence Rust) support fewer targets than GCC. Are there
> certain target that LLVM doesn’t support that are important to Emacs?

For example: MS-DOS, via DJGPP.

Or, Windows 9X.  I use Emacs to code-convert files on a Windows 98
machine used to run government software.

And also the various Unix systems that currently do not support LLVM:
HP/UX, and AIX.

> 3 Many Rust libraries (crates) are MIT and/or Apache licensed. Do all
> Libraries used by GNU Emacs need to be GPL or is it sufficient to have
> a GPL compatible license?

I would be more concerned about how Rust libraries are distributed.
Isn't the Rust package manager one of those which download libraries
directly from a source code repository?

> 4 How sizable of a contribution would be needed for the maintainers to
> accept Rust in Emacs core? Would auxiliary functionality be considered
> (such as Rust in the Linux Kernel) or would it need to have major
> impact.

It will probably not be accepted.

> 5 Concerns over having more than one core language in GNU Emacs.

Yes.

> 6 Concerns over using such a new language. Rust still changes at a
> fast pace relative to C and it’s future is less certain then a more
> established language.

Yes.  Rust is not widely available at all, while C is available for
every platform, from 8 bit microcontrollers, to 16 and 24-bit digital
signal processors, and 32-bit and 64-bit consumer computers, and will
remain that way for the foreseeable future.

Emacs is a portable program written in C.  Thus, any code that is not
strictly a port to some other platform should also be written in
standard C99.

In the past, people wanted to rewrite Emacs in Scheme.  Then, it was
C++.  Then, it was Java.  Now, it is Rust.

Part of the reason Emacs has existed for so long is that it has not
given in to those demands, and remains a highly portable program,
written in a standardized language, that has remained more or less
constant since it Emacs was written.  Rust, on the other hand,
frequently releases breaking changes with new versions of the
programming language, and is not standardized in any way.  This shows in
that even an operating system supposedly written in Rust provides a C
toolchain and C runtime library.

\f

Now, judging by recent internet chatter, you probably think Rust will
magically allow Emacs Lisp to run on different threads.

Some people seem to have this idea that because the Rust compiler will
try to prevent two threads from having access to a variable at the same
time, writing Emacs in Rust will, as if by magic, allow multiple Lisp
threads to run at once.

That is not true.  The Rust compiler does not try to avoid concurrency
pitfalls aside from the simple data race: for example, locking a
non-recursive mutex twice is not only a programming error, it is
undefined behavior!

In addition, locking (and not locking) needs to be carefully thought
out, or it will slow down single threaded execution.  For example, a
common pattern found inside Emacs code is:

  for (; CONSP (tem); tem = XCDR (tem))
    /* Do something with XCAR (tem) */;

XCAR and XCDR work fine unchanged on most machines without needing any
kind of locking, as Lisp_Cons is always aligned to Lisp_Object.

However, it does not work on two machines, which either need explicit
memory barrier instructions (or in the case of vectors, mutexes):

On the (64-bit) Alpha, memory ordering across CPUs is extremely lax, and
without the appropriate barrier instructions, even aligned reads and
writes of 64 bit words are not atomic.

On x86 (and possibly other platforms) with --with-wide-int, reads and
writes of Lisp_Object require separate moves and stores to and from two
32 bit registers, which is obviously not atomic.

Then, if XCDR (tem) reads from a cons whose cdr cell is in the process
of being written to, then it may dereference a nonsense pointer.

And contrasting that, this code is perfectly safe in C, on x86.  The
assert will never trigger:

static unsigned int foo;

thread_1 ()
{
  foo++;
}

thread_2 ()
{
  assert (foo <= 1);
}

main ()
{
  /* start thread_1 and thread_2 at the same time.  */
}

I believe the Rust compiler will force you to add some code in that
case.  Imagine how much overhead it would add if Emacs had to lock a
Lisp_Cons before reading or writing to it.

But the Lisp interpreter is the easy part, especially since we already
have much of the necessary interpreter state moved off into struct
thread_state.  A lot of other code which is algorithmically non
reentrant will have to be made reentrant, and papering mutexes and
atomics over them to satisfy compile-time checks will not do that.  For
example, how do you propose to rewrite process.c to allow two threads to
enter wait_reading_process_output at the same time?

Who gets SIGCHLD? Who gets to read process output? In which thread do
process filters run?

In the end, you will have to do the same work you would need to in C,
with the added trouble of adding code in a new language, making everyone
else learn the new language, while throwing portability down the drain.
That doesn't sound like a good tradeoff to me.



  reply	other threads:[~2023-01-22  7:44 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <f2ee2f1d-332f-4707-bd9e-23444c34749f@Spark>
2023-01-21 22:48 ` Consideration for Rust contributions in Emacs Troy Hinckley
2023-01-22  7:44   ` Po Lu via Emacs development discussions. [this message]
2023-01-22 11:05   ` Daniel Martín
2023-01-22 14:04     ` Po Lu
2023-01-22 23:16       ` Troy Hinckley
2023-01-23  5:55         ` Po Lu
2023-01-24  3:49         ` Richard Stallman
2023-01-24  3:52     ` Richard Stallman
2023-01-24  6:52       ` Dr. Arne Babenhauserheide
2023-01-23  2:00   ` Sean Allred
2023-01-23  3:37     ` Troy Hinckley
2023-01-23 12:25       ` Po Lu
2023-01-24  2:24         ` Lynn Winebarger
2023-01-24  2:47           ` Etienne Prud'homme
2023-01-24  2:49           ` Po Lu
2023-04-11 12:39         ` Po Lu via Emacs development discussions.
2023-04-11 18:23           ` Dr. Arne Babenhauserheide
2023-04-15  3:36             ` Richard Stallman
2023-04-15  3:40               ` Po Lu
2023-04-15  7:03                 ` Eli Zaretskii
2023-04-12  0:36           ` Dmitry Gutov
2023-04-12  4:59             ` tomas
2023-04-12 11:26           ` Richard Stallman
2023-04-13  1:02           ` Richard Stallman
2023-04-13  5:09             ` Eli Zaretskii
2023-04-13  8:23               ` Po Lu
2023-04-15 23:27             ` Dmitry Gutov
2023-04-16  0:11               ` Po Lu
2023-04-17  2:55                 ` Richard Stallman
2023-01-23 13:21       ` Dr. Arne Babenhauserheide
2023-01-23 16:51         ` John Yates
2023-01-23 17:06           ` Eli Zaretskii
2023-01-23 18:22             ` John Yates
2023-01-23 19:04               ` Eli Zaretskii
2023-01-23 19:44                 ` Bob Rogers
2023-01-23 19:56                   ` Eli Zaretskii
2023-01-23 20:08                     ` Bob Rogers
2023-01-23 19:22               ` Dr. Arne Babenhauserheide
2023-01-23 23:52               ` Po Lu
2023-01-24  0:45         ` Po Lu
2023-01-23  7:32     ` Robert Pluim

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=871qnn6mjw.fsf@yahoo.com \
    --to=emacs-devel@gnu.org \
    --cc=comms@dabrev.com \
    --cc=luangruo@yahoo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).