Skip to content

Instantly share code, notes, and snippets.

View DmitryOlshansky's full-sized avatar
💭
Photon!

Dmitry Olshansky DmitryOlshansky

💭
Photon!
View GitHub Profile
@DmitryOlshansky
DmitryOlshansky / mem_fun.d
Created June 11, 2014 16:55
Play with Windows MM-functions for potential use in concurrent GC
import core.sys.windows.windows, core.stdc.stdlib,
std.stdio, std.datetime;
//pragma(mangle, "_QueryWorkingSet@12")
extern(Windows) BOOL QueryWorkingSet(HANDLE hProcess,PVOID pv,DWORD cb);
void main()
{
// map 512mb, obviously 32bit sucks, as I couldn't map more
uint SIZE = 2^^29;
auto mapping = CreateFileMappingA(INVALID_HANDLE_VALUE,
@DmitryOlshansky
DmitryOlshansky / bench_buffer
Created March 15, 2014 15:52
Tiny benchmark for scopebuffer
import scopebuf;
//import std.buffer.scopebuffer;
import std.datetime, std.conv, std.stdio, std.exception;
int do_build(int n)
{
char[80] store = void;
auto buf = scopeBuffer(store[]);
//scope(exit) buf.free();
for(int i=0; i<n;i++)
@DmitryOlshansky
DmitryOlshansky / new_regex
Created October 20, 2013 16:29
DDoc intro for new std.regex API
New API for std.regex was introduced. The old API that had its operation mode dependent on "g" (=global) flag was too confusing and error prone. Moreover in some cases it was already being overrided by a function such as by splitter with regex.
New version ties the operation to the function in question, thus being simpler to understand without extra context. Compare 2 samples.
Before:
---
void main()
{
import std.regex, std.algorithm, std.range, std.stdio, std.string;
auto m = "3.141592".match(`(\d+)\.(\d+)`);
@DmitryOlshansky
DmitryOlshansky / threaded.d
Created September 9, 2013 21:48
Threaded-code and tail-call optimizations. Every good compiler can do it...
/**
* Simple threaded-code interpreter
* Practical on the follwoing compiler(s)/options
* gdc -O2 and above (tested on venerable 4.6)
* ldc -O1 and above (tested with LLVM 3.3)
* Not usable on dmd - doesn't optimize tail-call to jump
*/
import std.typecons;
struct OpCode{
@DmitryOlshansky
DmitryOlshansky / relay_hosts.d
Created September 6, 2013 21:57
A simple benchmark where std.regex performs quite poorly.
import std.regex;
import core.stdc.stdio;
import core.stdc.string;
import std.string;
import std.stdio : writefln, writef;
/*
Parses mail logs like this line:
Nov 1 19:37:59 themailhost sendmail[10620]: [ID 801593 mail.info] pA20bx2u010620:
from=<[email protected]>, size=6988, class=0, nrcpts=2,
@DmitryOlshansky
DmitryOlshansky / output_adapter.d
Last active December 21, 2015 17:19
Output range adapters proof of concept
import std.range, std.traits, std.stdio, std.format, std.digest.sha;
auto demux(Output...)(Output sinks)
{
static struct Demux
{
Output outs;
this(Output outs)
{
this.outs = outs;
@DmitryOlshansky
DmitryOlshansky / fast_stride.d
Created May 26, 2013 20:31
Experimential UTF-8 stride calculation
import std.traits, std.utf : UTFException;
uint stride(S)(auto ref S str, size_t index)
if (is(S : const char[]) ||
(isRandomAccessRange!S && is(Unqual!(ElementType!S) == char)))
{
immutable c = str[index];
if (c < 0x80)
return 1;
else
@DmitryOlshansky
DmitryOlshansky / virt_bench.d
Created February 2, 2013 21:39
(ab)using virtual memory for fun and profit
///Written in the D programming language
// Bench built-in array append, std.array.Appender
// and custom virtual-memory aware VirtualArray in terms of
// memory usage and the time taken
// to append up to X megabytes using different chunk sizes:
// 16 bytes, 64bytes, 256 bytes and 16Kb at a time.
// pass this version to take insert readln's
@DmitryOlshansky
DmitryOlshansky / build.d
Last active December 10, 2015 18:38
A flexible Builder construct for Phobos
import std.traits, std.exception, core.stdc.stdlib;
version(unittest) import std.typetuple;
// preferred size of one Builder chunk
// not too small (to not waste memory with allocation padding, pointer link etc.)
// and not too big (good fit for CPU cache, less per-thread memory usage)
private enum builderChunkSize = 2^^14 - (void*).sizeof;
// TLS chunk cache for builder
@DmitryOlshansky
DmitryOlshansky / sealed.d
Created November 16, 2012 18:34
Drafty Synced!T example with std.concurrancy
import std.concurrency, std.stdio, std.container, core.sync.mutex, core.thread;
struct Synced(T) if(!(is(T == class)))
{
private:
shared T* payload;
__gshared Mutex mut; //mutex should have worked as shared
public this(Args)(Args args...){