Skip to content

Instantly share code, notes, and snippets.

View mkock's full-sized avatar
💭
Currently building stuff in Go

Martin Kock mkock

💭
Currently building stuff in Go
View GitHub Profile
@mkock
mkock / dmr-parser.go
Created January 25, 2018 19:55
Go implemenation of DMR parser
import (
"bufio"
"fmt"
"os"
"strings"
)
// StringParser parses XML as strings.
type StringParser struct{}
@mkock
mkock / app.ts
Created January 25, 2018 20:04
TypeScript implementation of the DMR parser.
import * as fs from 'fs'
import * as es from 'event-stream'
async function processFile() {
console.log('Processing file started, optimized version')
let lineNr = 0
const artNummer: string = '<ns:KoeretoejArtNummer>'
const artNummerCar: string = '<ns:KoeretoejArtNummer>1<'
const vehicleStart: string = '<ns:KoeretoejBetegnelseStruktur>'
@mkock
mkock / concurrent-performance.csv
Created February 11, 2018 21:20
Performance measurements of concurrent DMR XML parser in Go
parser 3 workers 6 workers 12 workers
string 5m17s 5m11s 5m11s
xml 22m35s 22m52s 23m46s
@mkock
mkock / concurrent-dmr-parser.go
Last active February 11, 2018 22:00
Concurrent implementation of the DMR parser.
func main() {
flag.Parse()
xmlFileName := filepath.Join("/tmp/", "out.xml")
if _, err := os.Stat(*inFile); os.IsNotExist(err) {
fmt.Printf("abort: file %q does not seem to exist\n", *inFile)
return
}
@mkock
mkock / concurrent-string-parser.go
Last active February 11, 2018 22:02
Concurrent version of the DMR string parser in Go.
// ParseExcerpt runs the string parser.
func (p *StringParser) ParseExcerpt(id int, lines <-chan []string, parsed chan<- string, done chan<- int) {
var isCar bool
csv, brand, model := "", "", ""
proc := 0 // How many excerpts did we process?
for excerpt := range lines {
for _, line := range excerpt {
if strings.HasPrefix(line, "<ns:KoeretoejArtNummer>") {
isCar = strings.HasPrefix(line, "<ns:KoeretoejArtNummer>1<")
continue
@mkock
mkock / concurrent-xml-parser.go
Last active April 27, 2018 12:29
Concurrent version of the DMR XML parser in Go.
// Same data structures as before.
// ParseExcerpt parses XML file using XML decoding.
func (p *XMLParser) ParseExcerpt(id int, lines <-chan []string, parsed chan<- string, done chan<- int) {
proc := 0 // How many excerpts did we process?
var stat vehicleStat
for excerpt := range lines {
if err := xml.Unmarshal([]byte(strings.Join(excerpt, "\n")), &stat); err != nil {
panic(err) // We _could_ skip it, but it's better to halt execution here.
}
@mkock
mkock / example1.go
Created January 13, 2020 21:41
The Flyweight Pattern in Go, Example One
// Player represents a player in a game.
type Player struct {
ID uint32
Handle, Name, Country string
Games []string
}
var (
playerIDMap = make(map[uint32]Player)
playerHandleMap = make(map[string]uint32) // ref: playerIDMap
@mkock
mkock / example2.go
Created January 13, 2020 21:47
The Flyweight Pattern in Go, Example Two
// Player represents a player in a game.
type Player struct {
ID uint32
Handle, Name, Country string
Games []string
}
// cachedPlayer is a Player, but without the Games field.
type cachedPlayer struct {
ID uint32
@mkock
mkock / example1_1.go
Last active January 19, 2020 06:13
Type Player
// Player represents a player in a game.
type Player struct {
ID uint32
Handle, Name, Country string
Games []string
}
@mkock
mkock / example1_2.go
Last active January 19, 2020 06:14
Player One
playerOne := Player {
ID: 1,
Handle: "Razzor",
Name: "Dave McTesty",
Country: "US",
Games: []string{"Fire Emblem", "Smash Bros Ultimate"},
}