News for hackers. Stuff that matters.
Re:Removing garbage collection from the Rust language by copx on Monday June 03, 2013 @06:06AM (#5812345)
By the way, why does Rust need a C compiler anyway?

I admit I am ignorant here because I never cared. I know that many academic/niche programming languages bundle MinGW/depend on a MinGW or even Cygwin installation.. I always assumed that was to cut some corners. I mean, I have used multiple programming languages on Windows which compile to native code and have no dependency on a C compiler.

Re:Removing garbage collection from the Rust language by steveklabnik on Monday June 03, 2013 @06:23AM (#5812393)
> By the way, why does Rust need a C compiler anyway?

I'm not 100% sure, but I do know that zero.rs requires 'the following libc functions: malloc, free, abort, memcpy, and memcmp'. So I'd imagine it's that.

Re:Removing garbage collection from the Rust language by pjmlp on Monday June 03, 2013 @07:42AM (#5812623)
Those functions could be easily done via syscalls to the underlying OS or simple Assembly routines, as they are quite simple to implement.

I have not yet looked at Rust's code, but I guess it is required for some bootstrapping code.

Re:Removing garbage collection from the Rust language by colanderman on Monday June 03, 2013 @10:09AM (#5813344)
Those functions could be easily done via syscalls to the underlying OS or simple Assembly routines, as they are quite simple to implement.

None of those four functions use syscalls, nor are any of them simple to implement efficiently in assembly.

Consider that memcpy, under GCC,

* generates different code based on any known alignment of the source or destination

* generates simpler code if the size is known at compile time

* generates only register accesses if one or both of the arguments live in registers

* is elided entirely if GCC determines that it may do so safely

The other three have similar complex behavior. The Rust developers didn't use them just because they were too dumb to know how to write a for loop in assembly.

Re:Removing garbage collection from the Rust language by pjmlp on Monday June 03, 2013 @10:33AM (#5813479)
Simple is not the same as easy.

Anyone with compiler development experience can implement them without much trouble, hence simple.

Sadly that is a skill many seem to lack nowadays.

Re:Removing garbage collection from the Rust language by Confusion on Wednesday June 05, 2013 @03:58AM (#5824253)
Yeah, back in the old days everyone had that kind of compiler development experience, didn't they?
Re:Removing garbage collection from the Rust language by pjmlp on Wednesday June 05, 2013 @02:06PM (#5827401)
Compared to what kids seem to know nowadays, yes.

They can grok HTML5 page full of JavaScript stuff, but then mix language with implementation, think that strong typing is only possible with VM based languages and faith at the look of Assembly code.

Re:Removing garbage collection from the Rust language by njs12345 on Monday June 03, 2013 @09:38AM (#5813163)
I think malloc/free at least are certainly far from simple to implement..
Re:Removing garbage collection from the Rust language by pjmlp on Monday June 03, 2013 @10:31AM (#5813472)
Simple does not mean easy.
Re:Removing garbage collection from the Rust language by zxcdw on Monday June 03, 2013 @12:29PM (#5814161)
What's the definition of "hard to implement" if not complexity?
Re:Removing garbage collection from the Rust language by pjmlp on Monday June 03, 2013 @12:45PM (#5814263)
If you are targeting an existing OS, malloc/free can be simple wrappers to OS syscalls for memory allocation. Not very efficient, but pretty straightforward to implement.

If targeting direct hardware, any CS student should have learned how to implement a memory allocator on their compiler design, data structures or OS classes.

Back when I graduated, this would be a home work exercise.

If you want to implement a high performance memory allocator that works under pressure in a multi-core context, then yes it is complex to implement.

Now something that allocates/releases memory with a general purpose algorithm? That is quite simple and many compiler design books even provide such examples.

Re:Removing garbage collection from the Rust language by ahomescu1 on Monday June 03, 2013 @06:45PM (#5816576)
> If you are targeting an existing OS, malloc/free can be simple wrappers to OS syscalls for memory allocation.

Not really, there's no such thing as malloc/free syscalls. On Linux, you have mmap/munmap (at page granularity) and brk/sbrk (which only extend the current heap). You could implement malloc as either a mmap() of one or more pages, or by using sbrk() to extend the heap by exactly the memory you need, but then freeing that memory becomes much harder.

Re:Removing garbage collection from the Rust language by srgpqt on Monday June 03, 2013 @08:27PM (#5816973)
To take the example of Windows, wouldn't this qualify as a malloc/free syscall equivalent?

http://msdn.microsoft.com/en-us/library/windows/desktop/aa36...

Re:Removing garbage collection from the Rust language by marshray on Tuesday June 04, 2013 @04:39AM (#5818296)
In fact, I believe modern versions of msvcrt.dll just call HeapAlloc(GetProcessHeap(), ...) in the usual case.
Re:Removing garbage collection from the Rust language by pjmlp on Tuesday June 04, 2013 @01:10AM (#5817840)
That is what I meant by OS syscalls.

Should I write a full tutorial on an HN post how to write memory allocators when anyone can easily search for one?

Re:Removing garbage collection from the Rust language by ahomescu1 on Tuesday June 04, 2013 @04:31PM (#5821944)
> Should I write a full tutorial on an HN post how to write memory allocators when anyone can easily search for one?

No, but a memory allocator is more that "simple wrappers". My point was that OS kernels (Linux/BSDs at least) don't actually provide malloc/free as syscalls, and that you need to implement an allocator in userspace if you want these functions (even if it's a trivial allocator that wastes memory).

Re:Removing garbage collection from the Rust language by pjmlp on Wednesday June 05, 2013 @03:17AM (#5824153)
Of course they don't, why should they?!

malloc/free are part of the C language runtime, not OS syscalls.

A compiler writer can build the memory allocator on top of what the OS provides as syscalls or take a shortcut and use the C runtime library instead while adding a dependency to it.

I never mentioned malloc/free were syscalls.

Re:Removing garbage collection from the Rust language by marshray on Tuesday June 04, 2013 @04:36AM (#5818287)
Those functions should be available in %systemroot%\system32\msvcrt.dll.
Re:Removing garbage collection from the Rust language by pjmlp on Tuesday June 04, 2013 @05:57AM (#5818499)
msvcrt.dll is not an OS API library, Kernel32.dll is.
Re:Removing garbage collection from the Rust language by marshray on Tuesday June 04, 2013 @12:21PM (#5820372)
http://msdn.microsoft.com/en-us/library/abx4dbyh(v=vs.80).as... The msvcrt.dll is now a "known DLL," meaning that it is a system component owned and built by Windows. It is intended for future use only by system-level components.

Msvcrt.dll began shipping as part of the OS around the Win2K/XP timeframe, IIRC.

Re:Removing garbage collection from the Rust language by pjmlp on Tuesday June 04, 2013 @01:01PM (#5820645)
It still does not make it an OS API, it is the C runtime library which is quite different even if bundled with the OS.
Re:Removing garbage collection from the Rust language by marshray on Tuesday June 04, 2013 @01:15PM (#5820734)
I know some folks who think of the NT syscall interface as the 'true' OS API and see kernel32.dll as an add-on. I'll ask, but I don't think these folks make a distinction between user32.dll and the msvcrt.dll that ships with the OS.
Re:Removing garbage collection from the Rust language by pjmlp on Tuesday June 04, 2013 @04:22PM (#5821898)
> I know some folks who think of the NT syscall interface as the 'true' OS API and see kernel32.dll as an add-on.

This is 100% correct, many of the real syscalls are located in Ntoskrnl, while kernel32.dll is actually part of the Win32 subsystem, given the personalities feature from Windows.

So you could be using another subsystem.

However, given that the POSIX and OS/2 subsystems are no longer supported, the only available subsystem is Win32.

Metro is another matter as it uses another architecture not based on the subsystems mechanism.

But in the end these are all OS APIs, not language runtimes.

Re:Removing garbage collection from the Rust language by marshray on Tuesday June 04, 2013 @04:52PM (#5822046)
OK, asking around, the word I get is that the OS team owns the msvcrt.dll in %systemroot%\system32. Although unofficially it might be the case that x86 apps built with VC6 depend on this dll to implement a stable interface, it's not officially recommended to take dependencies on it.
Re:Removing garbage collection from the Rust language by Ygg2 on Monday June 03, 2013 @06:17AM (#5812374)
So it (Firefox with Servo) would work on Windows as close to native as possible? It would be funny to start Firefox and have a console windows poping in/out of existence or some other visual quirk.
Re:Removing garbage collection from the Rust language by tmzt on Monday June 03, 2013 @01:14PM (#5814456)
I believe the reason in the past was due to C++ implemented NPAPI plugins like Flash. Only MSVC builds of Mozilla/Firefox were officially supported.
Re:Removing garbage collection from the Rust language by pjmlp on Monday June 03, 2013 @07:41AM (#5812616)
> By the way, why does Rust need a C compiler anyway?

Bootstrapping.

Re:Removing garbage collection from the Rust language by eridius on Monday June 03, 2013 @01:37PM (#5814583)
Rust bootstraps by downloading an existing snapshot of the rust compiler, which it then uses to build the stage0 rust compiler, which then builds the stage1 rust compiler, which then builds the stage2 rust compiler.