9 ms·
Bflat – a single ahead-of-time crosscompiler and runtime for C#
- chrismsimpson 4y agoWhat would be great is a system like this, but with reference counting instead. If that existed, I’d immediately jump ship.
- divs1210 4y agoInteresting. Why do you say so?
- chrismsimpson 4y agoPredictable finalisation. To build say, an operating system, you need to know exactly when memory is freed. If you don’t know this at a fundamental level, how can you schedule work? This IMO is fundamentally why no one has built a modern OS in C# alone.
- viraptor 4y agoZero stdlib comes with no GC and seems to be viable for bare metal, so pretty much what you're describing. Although you don't need to remove GC for everything. IDisposable already covers explicit destruction and the memory mapping can be handled with native functions where necessary. (I don't know if that's possible in bflat, I'm talking in general - but pinvoke should be enough)
- pjmlp 4y agoHence why system programming languages with GC also have other ways to manage resources (including memory), just because a GC is available doesn't mean it has to be used for everything. See the whole linage of Mesa/Cedar, Modula-2+, Modula-3, Oberon, Midori,... OSes. C#, like those languages, allows for manual memory management, unsafe code, stack allocation. MSIL was designed to also support languages like C++, and since C# 7, many of those capabilities are no longer only available to C++/CLI, rather being incremently exposed to C# as well. Besides being a GC algorithm anyway, RC has performance issues, which can only be solved by using multiple optimization techniques from tracing GC, like background threads for cascading deletion of complex graphs, deferred counter manipulation, lock free data structures,... Project Oberon source code, http://www.projectoberon.com/ http://www.projectoberon.com/ Active Oberon source code (the latest language OS from the Oberon tree still being actively developed), https://gitlab.inf.ethz.ch/felixf/oberon https://gitlab.inf.ethz.ch/felixf/oberon It would be great if Microsoft some day releases Midori source code.
- still_grokking 4y ago> It would be great if Microsoft some day releases Midori source code. Waiting for this since years. But it won't happen, I guess, as it didn't happen until now.
- candiodari 4y agoAdding features to languages by anyone but the language designer doesn't work. It's unsupported, it affects too many other parts of the stack and leads to incredibly hard to spot bugs. You, or any project, can't do this. Also: doing this means as soon as you start using predictable finalisation to close DB connections, or anything, your code stops being C# code. It can no longer be used except in a highly nontrivial environment. It depends, in an undeclared manner, on a nonstandard runtime. So for open source code, or anything you're hoping to reuse: please don't do this. If you want this, switch to C++, Swift or Rust, or some other language that has it built in.
- pjmlp 4y agoThat is what using declarations and IDisposable are for.
- superjan 4y agoExactly. Idiomatic C# code does not depend on finalizers to release resources. Finalizers are typically only used as a last resort to prevent handle leaks, it is only memory deallocation that is nondeterministic.
- pjmlp 4y agoAnd even then, SafeHandle is a much better option. Memory deallocation can be done as deterministic as malloc/free via the native heap APIs.
- superjan 4y agoThere is a PoC OS in C#. https://en.wikipedia.org/wiki/Singularity_(operating_system) https://en.wikipedia.org/wiki/Singularity_(operating_system)
- pjmlp 4y agoThere are more than one, https://www.gocosmos.org/ https://www.gocosmos.org/ https://www.wildernesslabs.co/ https://www.wildernesslabs.co/ http://joeduffyblog.com/2015/11/03/blogging-about-midori/ http://joeduffyblog.com/2015/11/03/blogging-about-midori/
- olvy0 4y agoAlso for hard real time systems (which do not necessarily include operating systems). You need to have a deterministic computation time each cycle and never go over the time budget allocated. With a GC that isn't reference-counted it's nondeterministic. It could also be nondeterministic with reference counting if run e.g. into a loop that allocates a lot of objects and then they are freed, but you can measure and control and fix this behavior more easily than when trying to work against a non-resource-counting GC. Like others said here, it could certainly be done in C#, but you have to fight the language on some level. Events/delegates, strings and almost all collection classes are heap based and therefore can trigger the GC. It's difficult to do useful stuff without them. But you can do what you'd do in typical hard real-time applications anyway anyway, that is, preallocate everything you think you'll need. The APIs are there. The most problematic data type is the string, it's almost impossible to do anything in C# without it, and it's in the heap, therefore can trigger the GC. Also it's immutable, so almost all useful operations on it produce another string(s). See Joe Duffy's old blog post here [0]. However Using the new Span<char> or ReadOnlySpan<char> which are stack based can alleviate that. Almost all string actions can be done on them. There's also a library called ZString which can be useful [1]. Another problem is the jit, which in "regular" dotnet works lazily, only on the first call to a function or even to string literals, so again you don't get deterministic execution. It is very quick but calling a new function chain in succession can add up. However using an AOT compilation like bflat does eliminates that problem. Even without using AOT, you can approximate it by using reflection and calling RuntimeHelpers.PrepareMethod() on all methods in all referenced assemblies (except the system ones which should already be precompiled). [0] https://github.com/joeduffy/joeduffy.github.io/blob/master/_posts/2012-10-30-beware-the-string.md https://github.com/joeduffy/joeduffy.github.io/blob/master/_... [1] https://github.com/Cysharp/ZString https://github.com/Cysharp/ZString
- pjmlp 4y agoDepends on the effort, https://www.ptc.com/en/products/developer-tools/perc https://www.ptc.com/en/products/developer-tools/perc https://www.aicas.com/wp/products-services/jamaicavm/ https://www.aicas.com/wp/products-services/jamaicavm/ It just happens that .NET never went down this path.
- weitzj 4y agoMaybe nim lang might help?
- Dwedit 4y agoThe license is confusing. Some source files say "GNU Affero General Public License (AGPL)", but the homepage says "MIT license". Does this mean that if you build a project with this compiler, your code is distributed under the terms of the GNU Affero General Public License (AGPL)?
- lukevp 4y agoIs there still a point to this now that .NET 7 has NativeAOT and self contained EXEs? does this just give native trimming? Lack of macOS support seems like a non starter for a lot of cross platform cli tooling. Regular .NET can’t do AOT for asp.net core. Can bflat? If so, that would be really helpful for example for lambdas. The cold start time is quite poor without AOT.
- pjmlp 4y agoNativeAOT doesn't do bare metal. As for the cold start you can use tiered JIT and Ready2Run, until full AOT becomes available.
- Deukhoofd 4y agoNativeAOT definetely can do bare metal, here's an example of a very basic EFI boot application: https://github.com/MichalStrehovsky/zerosharp/tree/master/efi-no-runtime https://github.com/MichalStrehovsky/zerosharp/tree/master/ef...
- pjmlp 4y agoExcept that isn't NativeAOT as it ships on the box using dotnet publish workflow, rather a curated workflow which requires special steps as shown on the efi-runtime example. My remark was regarding the support you get with dotnet publish for NativeAOT, which by the way still doesn't support the GUI/EF scenarios from .NET Native.
- MStrehovsky 4y agoZeroSharp is done by the same person as bflat (me). These ZeroSharp samples get broken all the time because they bend the MSBuild targets that ship with .NET to an extreme. Bflat is a more supportable route and has a couple extra customizations to the compiler specifically for these scenarios.
- MStrehovsky 4y agobflat author here - happy to answer questions. I think at this point https://flattened.net/ https://flattened.net/ is a better introduction to bflat than the repo. The repo also links to it, but wanted to highlight it.
- throwaway_au_1 4y agoAwesome project! C# is my preferred language, but I love the less-heavyweight tooling in other langs, so bflat fits nicely. What inspired you to start this / what was your use case? Where did existing tooling fall short for you? I had a brief look at the repo but didnt see a 'Why' or similar section.
- MStrehovsky 4y agoI also like the less heavyweight approach where one can compose tools to do things. The existing .NET tooling is so entwined with MSBuild that one can't even (easily) invoke the C# compiler without an MSBuild project file. Don't get me wrong, I like MSBuild and even bflat is built with MSBuild (wrote a custom target to invoke bflat from MSBuild), but sometimes I really just want to write a Makefile and be done with it instead of fiddling with MSBuild targets. bflat integrates nicely with Make or other build systems - pass `-c` to generate object files (link it later with the commands bflat would use - use `-x` to see the commands), or use `bflat build-il` to create .NET assemblies and pass those as a reference (`-r`) to `bflat build` later.
- adamredwoods 4y agoAny thoughts on OSX support? I'm naive here, but DotNet 6 seems to work just fine (I haven't tried building bflat yet), and p/invoke seems straightforward enough: https://learn.microsoft.com/en-us/dotnet/standard/native-interop/pinvoke https://learn.microsoft.com/en-us/dotnet/standard/native-int...
- metaltyphoon 4y agoI think that support will come when dotnet 8 adds aot support for osx.
- timsneath 4y agoI'm a pedant, I know, but I can't deal with this being called B♭ (the enharmonic equivalent for A♯), instead of D♭ (the enharmonic equivalent of C♯). Is there another layer of the joke that I'm missing?
- mindover 4y agoWas my first thought as well. I bet there is already something called D flat, so they took this name instead. Edit: Db language https://www.codeproject.com/Articles/13639/Db-The-Future-Is-Coming https://www.codeproject.com/Articles/13639/Db-The-Future-Is-...
- MStrehovsky 4y agodflat could imply it has something do to with dlang. bflat triggers a lot of engagement on Twitter and elsewhere because everyone wants to correct it. Couldn't have chosen a better name.
- jeffparsons 4y ago"Double sharps" exist; they could have gone with "B double sharp". (Not saying they _should_ have -- just pointing out the option.)
- piotrkaminski 4y agoWouldn't B double sharp just be C? I guess they'd need to call it "B triple sharp", which is just getting silly.
- menage 4y agoThere's no sharp/flat note between B and C - "B sharp" would be C.
- piotrkaminski 4y agoIndeed -- curse you, irregular standard note structure!
- dang 4y agoWe changed the URL from https://github.com/bflattened/bflat https://github.com/bflattened/bflat to the project page. Both are nice, but the project page is more unique. Yes I know you can't be "more unique".
- Yuioup 4y agoMore distinctive?
- yjftsjthsd-h 4y ago> Yes I know you can't be "more unique". Higher entropy? We talk about number of bits of identifying information in privacy issues; same idea seems applicable here.
- dang 4y agoRelated: Bflat: C# as you know it but with Go-like tooling - https://news.ycombinator.com/item?id=27634456 https://news.ycombinator.com/item?id=27634456 - June 2021 (185 comments) Show HN: C# compiler with Go-like tooling (build small/standalone/native apps) - https://news.ycombinator.com/item?id=27628817 https://news.ycombinator.com/item?id=27628817 - June 2021 (1 comment)
- viraptor 4y agoI've noticed the flag "--deterministic" but couldn't find an explanation. What are the reason someone wouldn't want a deterministic build from this?
- MStrehovsky 4y agoSome executable file formats have time stamps in them. bflat follows the platform convention that respects this and writes current date/time in the file header by default. It is also a platform convention to offer options to set these to something stable (e.g. `/Brepro` on lld-link, or `--deterministic` for bflat) if one wants stable output.
- oso2k 4y agoMore than 20 years ago, there was another C-like compiler for Flat Real Mode[0] called BFlat[1][2]. I want to say there are other tools called BFlat or some near variant. [0] https://en.wikipedia.org/wiki/Unreal_mode https://en.wikipedia.org/wiki/Unreal_mode [1] http://www.lanet.lv/simtel.net/msdos/misclang-pre.html http://www.lanet.lv/simtel.net/msdos/misclang-pre.html [2] http://www.lanet.lv/ftp/simtelnet/msdos/misclang/bflat050.zip http://www.lanet.lv/ftp/simtelnet/msdos/misclang/bflat050.zi...
- bob1029 4y agoThe no operating system part is super compelling to me... How would one go about accessing something like basic network sockets using the zero lib? I assume there's a lot missing there.
- PreInternet01 4y agoWell, this looks like fun! `Bflat build` doesn't seem to support .csproj files, so testing this on existing code wasn't a huge success for me right away (and of course the trivial stuff always works), but definitely worth looking into later. Quite interesting that this is literally the first time I've come across this project, despite it being in a space I'm interested in and even having been discussed here previously...
- RHSman2 4y agoShould have called it Dflat
- krylon 4y agoThere is a programming language called D (https://dlang.org/ https://dlang.org/), this might have caused confusion for some people. (Technically, there used to be a language called B, too, but I don't think it has been in use since before I was born.)
- RHSman2 4y agoThe flat part. Developers are binary
- solarkraft 4y ago> bflat is to dotnet as VS Code is to VS. That's wonderful! I think C# is pretty decent, but totally hated working with the tooling. Thanks for making this and thanks to MS for producing some open source tooling enabling it. This puts .NET back into the "stuff you can use" territory, which is nice. Though, to be honest, the rest of the ecosystem (language, patterns, libraries) could still use some polish too. But it's a great step!
- QuiCasseRien 4y agoCrazy work ! i will test
- Kukumber 4y agoSome people at Microsoft were ACTIVELY against CoreRT, even some very high profiles, and yet they still work for that company, i don't get it Long live to the people who worked on CoreRT/NativeAOT and the people who pushed for it Great tech that will empower great projects An example: https://icculus.org/finger/flibitijibibo https://icculus.org/finger/flibitijibibo
- pjmlp 4y agoIt is common for some Microsoft groups to sabotage the work of others. See Longhorn, with its design ideas being redone in COM from Vista onwards. XNA being dropped on the floor, replaced by DirectXTK. C++/CX being replaced by C++/WinRT with complete disregard for the developer experience, and when they got tired of working on it, they left it on its C++17 state without VS tooling, and started hacking away on Rust/WinRT, which still can't even support COM authoring. XAML Islands being replaced by WinUI 3/WinAppSDK without the same level of tooling and feature set as UWP and WinUI 2.x. Which might help them achieve whatever KPIs they need, but burn bridges in the developer community, and then they are surprised that even the most hardcore supporters no longer jump into adopting their tech solutions as in the "Developers, Developers, Developers" days.
- actionfromafar 4y agoAnd many more examples like that.
- naasking 4y agoLooks interesting, will review, but just a note: the grey text is unreadable in bright conditions. Sitting here having breakfast in direct sunlight and I can't really see the text.
- Dwedit 4y agoIt says in "Zero" that there is no garbage collector, does that mean you can't use any reference-type objects at all? Or does that mean that reference-type objects stay allocated forever?
- olvy0 4y agoI think it means that reference type objects stay allocated forever, but I'm not sure. From [0]: If an API allocates as part of its operation and doesn't return that as result, it's not good. We don't have a GC. This is all memory leaks. [0] https://github.com/bflattened/bflat/tree/master/src/zerolib https://github.com/bflattened/bflat/tree/master/src/zerolib
- MarkSweep 4y agoIt uses malloc in place of the GC’s allocator, so objects stay allocated forever: https://github.com/bflattened/bflat/blob/master/src/zerolib/Internal/Stubs.cs#L134 https://github.com/bflattened/bflat/blob/master/src/zerolib/...
- rolenthedeep 4y agoOh wow, I've been absolutely pining for a way to write C# without associating with dotnet. Well done.
- moloch-hai 4y agoThere was mono. Can you build Minecraft with this thing? Can you still build Minecraft at all?
- saghm 4y agoThey said they wanted a way to write C#, not compile existing C#
- noveltyaccount 4y agoI'm having a hard time understanding how memory management works with the no-GC Zero stdlib. I see this but I don't get it. Anyone see a better explanation? https://github.com/bflattened/bflat/tree/master/src/zerolib https://github.com/bflattened/bflat/tree/master/src/zerolib
- MarkSweep 4y agoIt uses malloc in place of the GC’s allocator, so objects stay allocated forever: https://github.com/bflattened/bflat/blob/master/src/zerolib/Internal/Stubs.cs#L134 https://github.com/bflattened/bflat/blob/master/src/zerolib/...
- zamalek 4y agoRelated: https://www.mosa-project.org/ https://www.mosa-project.org/ They use CIL as opposed to C#.