The Q6600
Benchmarks Game

k-nucleotide Dart snapshot #3 program

source code

/* The Computer Language Benchmarks Game

   https://salsa.debian.org/benchmarksgame-team/benchmarksgame/

   Contributed by Philip Rogers
   Based on a javascript implementation by Jesse Millikan and Matt Baker
   Optimized and parallelized by Dwayne Slater
*/

import 'dart:async';
import 'dart:collection';
import 'dart:convert';
import 'dart:io';
import 'dart:isolate';
import 'dart:typed_data';

class CodeList {
  Uint64List buffer;
  int length;
  int codeBuffer = 0;
  int codeBufferLen = 32;
  int codeLen = 0;

  CodeList(int initialCapacity)
      : buffer = Uint64List(initialCapacity),
        length = 0;

  void appendBuffer(int codes) {
    if (length == buffer.length) {
      final newBuffer = Uint64List(length * 2);
      newBuffer.setRange(0, length, buffer);
      buffer = newBuffer;
    }
    buffer[length++] = codes;
  }

  void add(int n) {
    codeBuffer = (codeBuffer << 2) | n;
    if ((--codeBufferLen) == 0) {
      appendBuffer(codeBuffer);
      codeBuffer = 0;
      codeBufferLen = 32;
    }
  }

  void flush() {
    codeLen = (length * 32) + (32 - codeBufferLen);
    while (codeBuffer != 0) add(0);
  }
}

String codeToString(int code, int len) {
  final b = Uint8List(len);
  for (int i = len - 1; i >= 0; i--) {
    const int $A = 0x41;
    const int $T = 0x54;
    const int $C = 0x43;
    const int $G = 0x47;
    b[i] = const [$A, $C, $T, $G][code & 3];
    code >>= 2;
  }
  return String.fromCharCodes(b);
}

Future<String> readInput() {
  return stdin
      .transform(ascii.decoder)
      .transform(LineSplitter())
      .skipWhile((line) => !line.startsWith(">THREE"))
      .skip(1)
      .takeWhile((line) => !line.startsWith(">"))
      .map((s) => s.toUpperCase())
      .join();
}

Future<CodeList> readCodes() {
  final codeList = CodeList(1024 * 1024 * 4);
  return stdin
      .map((s) => String.fromCharCodes(s))
      .transform(LineSplitter())
      .skipWhile((line) => !line.startsWith(">THREE"))
      .skip(1)
      .takeWhile((line) => !line.startsWith(">"))
      .forEach((line) {
    final units = line.codeUnits;
    for (int i = 0; i < units.length; i++) {
      codeList.add((units[i] >> 1) & 3);
    }
  }).then((_) {
    codeList.flush();
    return codeList;
  });
}

Map<String, int> frequency(CodeList codes, int length) {
  final freq = HashMap<int, int>();
  final int shift = 64 - (length * 2);
  int window = codes.buffer[0];
  int next = codes.buffer[1];
  int count = codes.codeLen - length + 1;
  int cd = 32;
  int i = 2;
  while (count > 0) {
    freq[window >> shift] = (freq[window >> shift] ?? 0) + 1;

    window = (window << 2) | ((next >> 62) & 0x3);
    next <<= 2;
    count--;

    if ((--cd) == 0) {
      cd = 32;
      next = codes.buffer[i++];
    }
  }

  return Map.fromEntries(freq.entries.map((entry) => MapEntry(
      codeToString(entry.key.toUnsigned(length * 2), length), entry.value)));
}

void sort(CodeList codes, int length) {
  final freq = frequency(codes, length);
  final keys = freq.keys.toList();
  int n = codes.codeLen - length + 1;
  keys.sort((a, b) => (freq[b] - freq[a]));
  for (final key in keys) {
    String count = (freq[key] * 100 / n).toStringAsFixed(3);
    print('$key $count');
  }
  print('');
}

String find(CodeList codes, String string) {
  Map<String, int> freq = frequency(codes, string.length);
  return '${(freq[string])}\t$string';
}

void main(args) async {
  final sequence = await readCodes();
  final a = par(sequence, ['GGT', 'GGTA', 'GGTATT']);
  final b = par(sequence, ['GGTATTTTAATT']);
  final c = par(sequence, ['GGTATTTTAATTTATAGT']);
  sort(sequence, 1);
  sort(sequence, 2);
  (await a).forEach(print);
  (await b).forEach(print);
  (await c).forEach(print);
}

void findMultiple(List<dynamic> data) {
  final codes = data[1] as CodeList;
  final l = (data[2] as List<String>)
      .map((s) => find(codes, s))
      .toList(growable: false);
  (data[0] as SendPort).send(l);
}

Future<List<String>> par(CodeList codes, List<String> s) {
  final completer = Completer<List<String>>.sync();
  final recv = RawReceivePort((data) {
    completer.complete(data);
  });

  Isolate.spawn(findMultiple, [recv.sendPort, codes, s]);

  completer.future.whenComplete(() => recv.close());

  return completer.future;
}
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
Dart VM version: 2.8.1 (stable) (Unknown timestamp) on "linux_x64"
--snapshot-kind=app-jit


Thu, 07 May 2020 03:32:47 GMT

MAKE:
/usr/bin/dartanalyzer knucleotide.dartsnapshot-3.dartsnapshot
make: /usr/bin/dartanalyzer: Command not found
make: [/home/dunham/8000-benchmarksgame/nanobench/makefiles/u64q.programs.Makefile:448: knucleotide.dartsnapshot-3.dartsnapshot_run] Error 127 (ignored)
/usr/bin/dart --snapshot=knucleotide.dartsnapshot-3.snapshot --snapshot-kind=app-jit knucleotide.dartsnapshot-3.dartsnapshot 0 < ../knucleotide-input50000.txt
A 30.346
T 30.052
C 19.807
G 19.795

AA 9.212
TA 9.118
AT 9.097
TT 9.040
AC 6.019
AG 6.018
CA 6.018
GA 5.998
CT 5.959
GT 5.957
TC 5.951
TG 5.944
GC 3.923
GG 3.917
CG 3.916
CC 3.914

2948	GGT
889	GGTA
89	GGTATT
2	GGTATTTTAATT
2	GGTATTTTAATTTATAGT

5.25s to complete and log all make actions

COMMAND LINE:
/usr/bin/dart  knucleotide.dartsnapshot-3.snapshot 0 < knucleotide-input25000000.txt

PROGRAM OUTPUT:
A 30.295
T 30.151
C 19.800
G 19.754

AA 9.177
TA 9.132
AT 9.131
TT 9.091
CA 6.002
AC 6.001
AG 5.987
GA 5.984
CT 5.971
TC 5.971
GT 5.957
TG 5.956
CC 3.917
GC 3.911
CG 3.909
GG 3.902

1471758	GGT
446535	GGTA
47336	GGTATT
893	GGTATTTTAATT
893	GGTATTTTAATTTATAGT