Created
November 5, 2019 08:46
-
-
Save run-dlang/46c753f7233a624bb5639db615c9634a to your computer and use it in GitHub Desktop.
Code shared from run.dlang.io.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version (Posix) | |
{ | |
import core.sys.posix.sys.stat : stat_t, fstat; | |
import core.sys.posix.unistd : sysconf, _SC_PAGESIZE, _SC_LEVEL2_CACHE_SIZE, | |
_SC_LEVEL3_CACHE_SIZE; | |
} | |
else | |
{ | |
static assert(0, "Only posix OS is supported!"); | |
} | |
import std.stdio : File, writefln; | |
import core.stdc.stdio : fileno; | |
import std.exception : errnoEnforce; | |
import std.parallelism : TaskPool; | |
import std.typecons : tuple; | |
import std.algorithm.iteration : filter, map; | |
import std.range : iota; | |
import std.file : deleteme, remove; | |
@system getNbOfTasks(scope ref File file, immutable size_t bufferSize) | |
{ | |
stat_t statbuf = void; | |
int fd = fileno(file.getFP()); | |
errnoEnforce(fstat(fd, &statbuf) == 0); | |
size_t nbTasks = 1; | |
if (statbuf.st_size > bufferSize) | |
nbTasks = statbuf.st_size / bufferSize; | |
return nbTasks; | |
} | |
@system nothrow size_t getBufferSize() | |
{ | |
immutable size_t PAGE_SIZE = cast(size_t) sysconf(_SC_PAGESIZE); | |
immutable size_t LEVEL3_CACHE_SIZE = cast(size_t) sysconf(_SC_LEVEL3_CACHE_SIZE); | |
return LEVEL3_CACHE_SIZE - PAGE_SIZE; | |
} | |
@safe pure nothrow alias parse = (scope string data) { | |
size_t result = 0; | |
foreach (ref c; data) | |
{ | |
if (c == '\n') | |
result += 1; | |
} | |
return result; | |
}; | |
static void countAllLines(scope string filePath, scope const ushort nbThreads) | |
{ | |
TaskPool taskPool = new TaskPool(nbThreads); | |
immutable bufferSize = getBufferSize(); | |
File file = File(filePath, "rb"); | |
char[] buffer = file.rawRead(new char[bufferSize]); | |
shared string shared_data = buffer.idup(); | |
immutable nbTasks = getNbOfTasks(file, bufferSize); | |
size_t result = 0; | |
foreach (part; iota(0, nbTasks, nbThreads)) | |
{ | |
result += taskPool.reduce!"a + b"(nbThreads.iota.map!( | |
i => parse(shared_data[i * bufferSize .. ((i + 1) * bufferSize) - 1]))); | |
} | |
writefln("Count %d lines", result); | |
} | |
void main(string[] args) | |
{ | |
string test_file = deleteme ~ "-testing.txt"; | |
string data = r"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor. Cras elementum ultrices diam. Maecenas ligula massa, varius a, semper congue, euismod non, mi. Proin porttitor, orci nec nonummy molestie, enim est eleifend mi, non fermentum diam nisl sit amet erat. Duis semper. Duis arcu massa, scelerisque vitae, consequat in, pretium a, enim. Pellentesque congue. Ut in risus volutpat libero pharetra tempor. Cras vestibulum bibendum augue. Praesent egestas leo in pede. Praesent blandit odio eu enim. Pellentesque sed dui ut augue blandit sodales. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aliquam nibh. Mauris ac mauris sed pede pellentesque fermentum. Maecenas adipiscing ante non diam sodales hendrerit. | |
Ut velit mauris, egestas sed, gravida nec, ornare ut, mi. Aenean ut orci vel massa suscipit pulvinar. Nulla sollicitudin. Fusce varius, ligula non tempus aliquam, nunc turpis ullamcorper nibh, in tempus sapien eros vitae ligula. Pellentesque rhoncus nunc et augue. Integer id felis. Curabitur aliquet pellentesque diam. Integer quis metus vitae elit lobortis egestas. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi vel erat non mauris convallis vehicula. Nulla et sapien. Integer tortor tellus, aliquam faucibus, convallis id, congue eu, quam. Mauris ullamcorper felis vitae erat. Proin feugiat, augue non elementum posuere, metus purus iaculis lectus, et tristique ligula justo vitae magna. | |
Aliquam convallis sollicitudin purus. Praesent aliquam, enim at fermentum mollis, ligula massa adipiscing nisl, ac euismod nibh nisl eu lectus. Fusce vulputate sem at sapien. Vivamus leo. Aliquam euismod libero eu enim. Nulla nec felis sed leo placerat imperdiet. Aenean suscipit nulla in justo. Suspendisse cursus rutrum augue. Nulla tincidunt tincidunt mi. Curabitur iaculis, lorem vel rhoncus faucibus, felis magna fermentum augue, et ultricies lacus lorem varius purus. Curabitur eu amet. | |
"; | |
auto f = File(deleteme, "w"); | |
// write 100 * data ~= 224k | |
foreach (i; 0 .. 100) | |
f.write(data); | |
f.close(); | |
countAllLines(deleteme, 4); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment