Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve size of gob file/load speed #19

Merged
merged 3 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified build/cows.gob
Binary file not shown.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ require (
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
)

replace src/internal/timer => ./src/internal/timer
// replace src/internal/timer => ./src/internal/timer
54 changes: 35 additions & 19 deletions pokesay.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bufio"
"encoding/json"
_ "embed"
"encoding/binary"
"flag"
Expand Down Expand Up @@ -73,30 +74,37 @@ func printPokemon(choice pokedex.PokemonEntry) {

}

func chooseRandomCategory(entries pokedex.PokemonEntryMap) []pokedex.PokemonEntry {
choice := randomInt(entries.NCategories)
func chooseRandomCategory(entries pokedex.PokemonEntryMap) []*pokedex.PokemonEntry {
categoryChoice := randomInt(entries.NCategories)
var choice []*pokedex.PokemonEntry
var defaultChoice []*pokedex.PokemonEntry
idx := 0

for category, _ := range entries.Categories {
if idx == choice {
return entries.Categories[category]
defaultChoice = entries.Categories[category]
if idx == categoryChoice {
choice = entries.Categories[category]
}
idx++
}
// If something goes wrong, select from the parent category "cows"
return entries.Categories["cows"]
// If something goes wrong, just return the first category by default
choice = defaultChoice
return choice
}

func chooseRandomPokemon(pokemon []pokedex.PokemonEntry) pokedex.PokemonEntry {
return pokemon[randomInt(len(pokemon))]
func chooseRandomPokemon(pokemon []*pokedex.PokemonEntry) pokedex.PokemonEntry {
return *pokemon[randomInt(len(pokemon))]
}

func collectPokemonWithToken(entries pokedex.PokemonEntryMap, token string) []pokedex.PokemonEntry {
found := []pokedex.PokemonEntry{}
func collectPokemonWithToken(entries pokedex.PokemonEntryMap, token string) []*pokedex.PokemonEntry {
found := []*pokedex.PokemonEntry{}

for _, entry := range entries.Categories["cows"] {
for _, t := range entry.NameTokens {
if t == token {
found = append(found, entry)
for _, entries := range entries.Categories {
for _, entry := range entries {
for _, t := range entry.NameTokens {
if t == token {
found = append(found, entry)
}
}
}
}
Expand All @@ -119,7 +127,7 @@ func parseFlags() Args {
tabWidth := flag.Int("tabwidth", 4, "replace any tab characters with N spaces")
noTabSpaces := flag.Bool("notabspaces", false, "do not replace tab characters (fastest)")
fastest := flag.Bool("fastest", false, "run with the fastest possible configuration (-nowrap -notabspaces)")
category := flag.String("category", "cows", "choose a pokemon from a specific category")
category := flag.String("category", "", "choose a pokemon from a specific category")
name := flag.String("name", "", "choose a pokemon from a specific name")
listCategories := flag.Bool("category-list", false, "list all available categories")

Expand Down Expand Up @@ -168,10 +176,18 @@ func main() {
os.Exit(0)
}

if category, ok := pokemon.Categories[args.Category]; ok {
printSpeechBubble(bufio.NewScanner(os.Stdin), args)
printPokemon(chooseRandomPokemon(category))
categoryName := args.Category
var category []*pokedex.PokemonEntry
var ok bool

if categoryName == "" {
category = chooseRandomCategory(pokemon)
} else {
log.Fatal(fmt.Sprintf("Not a valid category: '%s'", args.Category))
if category, ok = pokemon.Categories[categoryName]; !ok {
log.Fatal(fmt.Sprintf("Not a valid category: '%s'", args.Category))
}
}

printSpeechBubble(bufio.NewScanner(os.Stdin), args)
printPokemon(chooseRandomPokemon(category))
}
2 changes: 1 addition & 1 deletion scripts/timer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

# Run pokesay and get the timings only

fortune | "${1:-./pokesay}" 2>&1 > /dev/null | jq .
echo w | "${1:-./pokesay}" 2>&1 > /dev/null | jq .
23 changes: 12 additions & 11 deletions src/pokedex.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func check(e error) {
}

func findFiles(dirpath string, ext string, skip []string) pokedex.PokemonEntryMap {
categories := &pokedex.PokemonEntryMap{Categories: make(map[string][]pokedex.PokemonEntry)}
categories := &pokedex.PokemonEntryMap{Categories: make(map[string][]*pokedex.PokemonEntry)}
err := filepath.Walk(dirpath, func(fpath string, f os.FileInfo, err error) error {
for _, s := range skip {
if strings.Contains(fpath, s) {
Expand All @@ -29,21 +29,22 @@ func findFiles(dirpath string, ext string, skip []string) pokedex.PokemonEntryMa
if !f.IsDir() && filepath.Ext(f.Name()) == ext {
data, err := os.ReadFile(fpath)
check(err)

pokemonCategories := createCategories(fpath)
p := pokedex.NewPokemonEntry(
data,
createName(fpath),
tokenizeName(fpath),
createCategories(fpath),
)

for _, c := range pokemonCategories {
p := pokedex.NewPokemonEntry(
data,
createName(fpath),
tokenizeName(fpath),
createCategories(fpath),
)
if val, ok := categories.Categories[c]; ok {
val = append(val, *p)
val = append(val, p)
} else {
categories.Categories[c] = []pokedex.PokemonEntry{*p}
categories.Categories[c] = []*pokedex.PokemonEntry{p}
}
categories.Categories[c] = append(categories.Categories[c], *p)
categories.Categories[c] = append(categories.Categories[c], p)
}
}
return err
Expand All @@ -66,7 +67,7 @@ func tokenizeName(fpath string) []string {

func createCategories(fpath string) []string {
parts := strings.Split(fpath, "/")
return parts[2 : len(parts)-1]
return parts[3 : len(parts)-1]
}

type CowBuildArgs struct {
Expand Down
2 changes: 1 addition & 1 deletion src/pokedex/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type PokemonEntry struct {
}

type PokemonEntryMap struct {
Categories map[string][]PokemonEntry
Categories map[string][]*PokemonEntry
NCategories int
}

Expand Down