D Programming Language

Created: by Pradeep Gowda Updated: Apr 13, 2020 Tagged: programming-language

Why D?

Show off D

Semantics

Garbage Collection

Example code showing GCStats:

import std.stdio;

extern (C)
{
    void gc_stats(GCStats* stats) nothrow;
}

 GCStats
{
    size_t poolsize;        // total size of pool
    size_t usedsize;        // bytes allocated
    size_t freeblocks;      // number of blocks marked FREE
    size_t freelistsize;    // total of memory on free lists
    size_t pageblocks;      // number of blocks marked PAGE
}

void main()
{
    GCStats stats;
    gc_stats(&stats);
    writeln(stats);
    auto p = new byte[200];
    gc_stats(&stats);
    writeln(stats);
    p = new byte[1000];
    gc_stats(&stats);
    writeln(stats);
    p = new byte[10000];
    gc_stats(&stats);
    writeln(stats);
}

To turn on garbage collection profiling

$ max_column_sum_by_key_v1 --DRT-gcopt=profile:1 ngrams.tsv 1 2
max_key: 2006 sum: 22569013
        Number of collections:  132
        Grand total GC time:  246 milliseconds
GC summary:   35 MB,  132 GC  246 ms

Summary of GC discussion

See June 2016 discussion on the Garbage collector

  • Precise GC is in the works. (todo: summarise progress)
  • “Only a few language features forces you to use the GC.” (which ones?)
  • “For most uses you are not forced to use the GC in any form especially with the help of std.experimental.allocator.” (How?)
  • Current GC is based on a forked version of Boehm GC. So, not easy to upgrade to current version without deep understanding of how D does Thread Local Storage etc.,
  • “we can disable the GC so it won’t go ahead and scan upon allocation (only time it does)” (how?)

Memory allocations are /always/ expensive. Higher level languages like Java have the benefit of using pools and optimizing for this usage pattern, D does and will never have this. Keep in mind an allocation = usage of malloc + write to returned pointer. – rikki c.

The point is, D doesn’t have to have a GC. Not using it is way easier than in most other languages because all the tools to help you profile it and avoid it are provided by the compiler. Go without a good GC is a dead language. D without a good GC is just not as good as it could be. And btw we’re generally faster than Go ;-) The point is: while a better GC is a work in progress we’ll never have a GC that can fit all needs, but it’s not as critical as it is in Go or in C# because we provide other ways to manage memory when limitations arise. – cym13

“Has anyone thought about taking GC from .NET and reusing it in D?”

One significant point has been already mentioned: cost of write barriers. I’d like to mention another factor: .NET GC is a copying one, it moves data around. One good feature of current D is it never moves data, so you can very easily call C and C++ code and pass pointers to your buffers and stuff and C/C++ code just takes these pointers and works with them as usual. No pinning, no marshaling, zero overhead. If you take a moving GC like .NET’s, you immediately make all C/C++ interaction much harder, now you need to worry about pinning stuff or copying “managed” data to “unmanaged” memory and back. This is all costly both in terms of CPU cycles and of programmer cycles. You’ll need “FFI”, what most other GC-ed languages have to have, and D doesn’t. –thedeemon

Precise Garbage Collector

GSoC project

It is essentially in a similar state that it was when Rainer presented it at DConf2013. The version in the pull request is not different from the one Rainer presented at all. It’s actually the same one, just updated with what has been changed in druntime recently and my attempts at making the marking phase faster.

Meta programming3

Debugging

Building

  • To build projects using ldc2 instead of dmd2 with dub, use the --compiler switch, thus: dub build --compiler=ldc2
  • To build the release version of the software, use the -b switch, thus: dub build -b release

Cross Compiling

Serialization

  • Orange library for serialization.

Concurrency

Range based programming

  • poita.org has couple of articles on range based programming.

Profiling / performance

D run time

Testing

C/C++ binding

Lexing and parsing

Another option is to use a lexer/state machine generator. Ragel supports D and Rust (as of the not-quite-released v7) as target languages. HN

Memory safety

Documenting D code

  • harbored-mod A documentation generator for D with support for both DDoc and Markdown in documentation comments.

Higher level programming

Resources

Introduction

Intermediary

Articles

Tutorials

Presentations

Editor

  • IntelliJ plugin for D language – look at the screenshots. It is a fairly complete IDE with syntax highlighting, auto completion, compile checking, code linting (via Dscanner), navigate to classes etc.,

  • DDT – the Eclipse based D IDE works as advertised. Use the 3.7(Indigo) version. [In Oct 2012, DDT did not work with the latest version of Eclipse: Juno].

  • Setting up TextAdept to autocomplete D code using DCD

Blogs

Talks

Read code

Discussions

Contributing to D development

Software written in D

Developing and distributing D software

Misc

User experience

Libraries

For some reason, many D programmers have this “collection of D libraries”, which is interesting in the sense that more than one developer has found it just easy to write their own library to do X…

In addition to those of CWEB:

  • Markdown based – very easy to read and write Literate source.
  • Generates readable and commented code in the target language (the generated code is usable by others)
  • Reports syntax errors back from the compiler to the right line in the literate source
  • Supports any language including syntax highlighting and pretty printing in HTML
  • Compatible with Vim (literate.vim)
  • Highly customizable (you can add your own HTML or CSS)
  • Runs fast – wc.lit compiled for me in 7ms for both code and HTML output
  • Supports TeX equations with $ notation.

Inspired from CWEB:

Interop

The power of reflection | Átila on Code

import autowrap;
mixin(
    wrapDlang!(
        LibraryName("mylib"),
        Modules(
            Module("mymodule"),
            Module("myothermodule"),
        )
    )
);

The code above, when compiled, will generate a Python extension (shared library) that exposes every D function marked as “export” in the modules “mymodule” and “myothermodule” as Python functions. It’ll even convert their names from camelCase to snake_case. Any D exceptions thrown will become Python exceptions. D structs and classes become Python classses. If the original D functions take a D string, you’ll be able to pass Python strings to them in user code. Modulo bugs, this… works! The code shown above is the only code that needs to be written. Setting up the build system takes more work!