Skip to content

Commit

Permalink
Merge pull request dlang#1169 from rainers/bench_vdparser
Browse files Browse the repository at this point in the history
add Visual D parser benchmark
  • Loading branch information
MartinNowak committed Feb 13, 2015
2 parents ec479a7 + d19f3d0 commit 4a2810c
Show file tree
Hide file tree
Showing 32 changed files with 25,154 additions and 0 deletions.
34 changes: 34 additions & 0 deletions benchmark/gcbench/vdparser.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import vdc.semantic;
import std.random, std.conv, std.file, std.path;

struct FalsePointers
{
string s;
ubyte[40960] data;
}

FalsePointers* ptrs;

void main(string[] argv)
{
size_t nIter = 20;
if(argv.length > 2)
nIter = to!size_t(argv[2]);

// create some random data as false pointer simulation
version (RANDOMIZE)
auto rnd = Random(unpredictableSeed);
else
auto rnd = Random(2929088778);

ptrs = new FalsePointers;
foreach(ref b; ptrs.data)
b = cast(ubyte) uniform(0, 255, rnd);

Project prj = new Project;
foreach(i; 0..nIter)
{
foreach(string name; dirEntries(buildPath("gcbench", "vdparser.extra"), "*.d", SpanMode.depth))
prj.addAndParseFile(name);
}
}
133 changes: 133 additions & 0 deletions benchmark/gcbench/vdparser.extra/stdext/array.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// This file is part of Visual D
//
// Visual D integrates the D programming language into Visual Studio
// Copyright (c) 2010-2011 by Rainer Schuetze, All Rights Reserved
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt

module stdext.array;

T* contains(T)(T[] arr, bool delegate(ref T t) dg)
{
foreach(ref T t; arr)
if (dg(t))
return &t;
return null;
}

T* contains(T)(T[] arr, T val)
{
foreach(ref T t; arr)
if (t == val)
return &t;
return null;
}

int arrIndex(T)(in T[] arr, T val)
{
for(int i = 0; i < arr.length; i++)
if (arr[i] == val)
return i;
return -1;
}

int arrIndexPtr(T)(in T[] arr, T val)
{
for(int i = 0; i < arr.length; i++)
if (arr[i] is val)
return i;
return -1;
}

void addunique(T)(ref T[] arr, T val)
{
if (!contains(arr, val))
arr ~= val;
}

void addunique(T)(ref T[] arr, T[] vals)
{
foreach(val; vals)
if (!contains(arr, val))
arr ~= val;
}

void remove(T)(ref T[] arr, T val)
{
int idx = arrIndex(arr, val);
if(idx >= 0)
arr = arr[0..idx] ~ arr[idx+1..$];
}

void adduniqueLRU(T)(ref T[] arr, T val, size_t maxEntries)
{
for(size_t i = 0; i < arr.length; i++)
if(val == arr[i])
{
// move to front
if(i > 0)
arr = [val] ~ arr[0..i] ~ arr[i+1 .. $];
return;
}

if(arr.length >= maxEntries)
arr.length = maxEntries - 1;
arr = [val] ~ arr;
}

///////////////////////////////////////////////////////////////////////

struct Set(T)
{
bool[T] _payload;

alias _payload this;

T first()
{
foreach(n, b; _payload)
return n;
return null;
}
}

bool contains(T)(ref bool[T] arr, T val)
{
return (val in arr);
}

void addunique(T)(ref bool[T] arr, T val)
{
arr[val] = true;
}

void addunique(T)(ref bool[T] arr, T[] vals)
{
foreach(val; vals)
arr[val] = true;
}

void addunique(T)(ref bool[T] arr, bool[T] vals)
{
foreach(val, b; vals)
arr[val] = true;
}

// needed in dmd 2.058beta
//version(none):
void addunique(T)(ref Set!T arr, T val)
{
arr[val] = true;
}

void addunique(T)(ref Set!T arr, bool[T] vals)
{
foreach(val, b; vals)
arr[val] = true;
}

void remove(T)(ref bool[T] arr, T val)
{
arr.remove(val);
}
226 changes: 226 additions & 0 deletions benchmark/gcbench/vdparser.extra/stdext/path.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
// This file is part of Visual D
//
// Visual D integrates the D programming language into Visual Studio
// Copyright (c) 2010-2011 by Rainer Schuetze, All Rights Reserved
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt

module stdext.path;

import std.path;
import std.array;
import std.string;
import std.conv;

string normalizeDir(string dir)
{
if(dir.length == 0)
return ".\\";
dir = replace(dir, "/", "\\");
if(dir[$-1] == '\\')
return dir;
return dir ~ "\\";
}

string normalizePath(string path)
{
return replace(path, "/", "\\");
}

string canonicalPath(string path)
{
return toLower(replace(path, "/", "\\"));
}

string makeFilenameAbsolute(string file, string workdir)
{
if(!isAbsolute(file) && workdir.length)
{
if(file == ".")
file = workdir;
else
file = normalizeDir(workdir) ~ file;
}
return file;
}

void makeFilenamesAbsolute(string[] files, string workdir)
{
foreach(ref file; files)
{
if(!isAbsolute(file) && workdir.length)
file = makeFilenameAbsolute(file, workdir);
}
}

string removeDotDotPath(string file)
{
// assumes \\ used as path separator
for( ; ; )
{
// remove duplicate back slashes
auto pos = indexOf(file[1..$], "\\\\");
if(pos < 0)
break;
file = file[0..pos+1] ~ file[pos + 2 .. $];
}
for( ; ; )
{
auto pos = indexOf(file, "\\..\\");
if(pos < 0)
break;
auto lpos = lastIndexOf(file[0..pos], '\\');
if(lpos < 0)
break;
file = file[0..lpos] ~ file[pos + 3 .. $];
}
for( ; ; )
{
auto pos = indexOf(file, "\\.\\");
if(pos < 0)
break;
file = file[0..pos] ~ file[pos + 2 .. $];
}
return file;
}

string makeFilenameCanonical(string file, string workdir)
{
file = makeFilenameAbsolute(file, workdir);
file = normalizePath(file);
file = removeDotDotPath(file);
return file;
}

string makeDirnameCanonical(string dir, string workdir)
{
dir = makeFilenameAbsolute(dir, workdir);
dir = normalizeDir(dir);
dir = removeDotDotPath(dir);
return dir;
}

void makeFilenamesCanonical(string[] files, string workdir)
{
foreach(ref file; files)
file = makeFilenameCanonical(file, workdir);
}

void makeDirnamesCanonical(string[] dirs, string workdir)
{
foreach(ref dir; dirs)
dir = makeDirnameCanonical(dir, workdir);
}

string quoteFilename(string fname)
{
if(fname.length >= 2 && fname[0] == '\"' && fname[$-1] == '\"')
return fname;
if(fname.indexOf('$') >= 0 || indexOf(fname, ' ') >= 0)
fname = "\"" ~ fname ~ "\"";
return fname;
}

void quoteFilenames(string[] files)
{
foreach(ref file; files)
{
file = quoteFilename(file);
}
}

string quoteNormalizeFilename(string fname)
{
return quoteFilename(normalizePath(fname));
}

string getNameWithoutExt(string fname)
{
string bname = baseName(fname);
string name = stripExtension(bname);
if(name.length == 0)
name = bname;
return name;
}

string safeFilename(string fname, string rep = "-") // - instead of _ to not possibly be part of a module name
{
string safefile = fname;
foreach(char ch; ":\\/")
safefile = replace(safefile, to!string(ch), rep);
return safefile;
}

string makeRelative(string file, string path)
{
if(!isAbsolute(file))
return file;
if(!isAbsolute(path))
return file;

file = replace(file, "/", "\\");
path = replace(path, "/", "\\");
if(path[$-1] != '\\')
path ~= "\\";

string lfile = toLower(file);
string lpath = toLower(path);

int posfile = 0;
for( ; ; )
{
auto idxfile = indexOf(lfile, '\\');
auto idxpath = indexOf(lpath, '\\');
assert(idxpath >= 0);

if(idxfile < 0 || idxfile != idxpath || lfile[0..idxfile] != lpath[0 .. idxpath])
{
if(posfile == 0)
return file;

// path longer than file path or different subdirs
string res;
while(idxpath >= 0)
{
res ~= "..\\";
lpath = lpath[idxpath + 1 .. $];
idxpath = indexOf(lpath, '\\');
}
return res ~ file[posfile .. $];
}

lfile = lfile[idxfile + 1 .. $];
lpath = lpath[idxpath + 1 .. $];
posfile += idxfile + 1;

if(lpath.length == 0)
{
// file longer than path
return file[posfile .. $];
}
}
}

unittest
{
string file = "c:\\a\\bc\\def\\ghi.d";
string path = "c:\\a\\bc\\x";
string res = makeRelative(file, path);
assert(res == "..\\def\\ghi.d");

file = "c:\\a\\bc\\def\\ghi.d";
path = "c:\\a\\bc\\def";
res = makeRelative(file, path);
assert(res == "ghi.d");

file = "c:\\a\\bc\\def\\Ghi.d";
path = "c:\\a\\bc\\Def\\ggg\\hhh\\iii";
res = makeRelative(file, path);
assert(res == "..\\..\\..\\Ghi.d");

file = "d:\\a\\bc\\Def\\ghi.d";
path = "c:\\a\\bc\\def\\ggg\\hhh\\iii";
res = makeRelative(file, path);
assert(res == file);
}
Loading

0 comments on commit 4a2810c

Please sign in to comment.