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 / 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 / example2_4.go
Last active January 19, 2020 06:08
Implementation of FindPlayerByID
// FindPlayerByID returns the player with the given ID, if exists.
// Otherwise, an empty Player is returned.
func FindPlayerByID(ID uint32) Player {
if cp, ok := crashyGamesPlayers[ID]; ok {
// cachedPlayer found.
return cp.convertWith(games) // Using globally cached games.
}
return Player{}
}
@mkock
mkock / example2_3.go
Last active January 19, 2020 06:10
Converter method for cachedPlayer
// convertWith returns the Player that matches cachedPlayer,
// with game titles attached.
func (c cachedPlayer) convertWith(games []string) Player {
return Player{
ID: c.ID,
Handle: c.Handle,
Name: c.Name,
Country: c.Country,
Games: games,
}
@mkock
mkock / example2_2.go
Last active January 19, 2020 06:11
Cache and games list
var games = []string{"Flappy Fish", "Ducks'n'Dogs", "Backflip Pro"}
var crashyGamesPlayers map[uint32]cachedPlayer