GithubHelp home page GithubHelp logo

anandvarma / namegen Goto Github PK

View Code? Open in Web Editor NEW
19.0 2.0 3.0 1.09 MB

๐ŸŒˆ A unique(ish) random name and id generator

License: Apache License 2.0

Go 100.00%
name-generator id-generator go

namegen's Introduction

namegen
GitHub license GoReportCard Go Reference

preview

What's this?

Go package and associated command line utility to generate random yet human-readable names and identifiers. Somewhat inspired by the random names that Docker gives its containers and GCP uses as project-ids.

Use it to generate email-ids, gamer-tags, component-ids, passwords or startup names.

For instance, it came up with gems like: boujee-coorgi, young-flamingo, articulate-otter-92, ancient-mako-7ab1g3 and mighty-pale-pikachu-7133.

Features

  • 1 Million+ unique possibilities with just names and nearly infinite named ids
  • Zero Allocations! No additional memory allocations other than the string returned
  • Blazing fast!! (10,000,000+ Op/s)

API & Usage

Generate names using default config.

ngen := namegen.New()
fmt.Println(ngen.Get()) // twisted-flamingo

Configure name generator to generate named ids of the form "adjective-color-animal-xxxx".

name_schema := []namegen.DictType{
	namegen.Adjectives,
	namegen.Colors,
	namegen.Animals,
}
ngen := namegen.NewWithPostfixId(name_schema, namegen.Numeric, 4)
fmt.Println(ngen.Get()) // energetic-pink-pug-3729

Go Reference


Benchmarks

goos: linux
goarch: amd64
pkg: github.com/anandvarma/namegen
cpu: AMD Ryzen 5 3600 6-Core Processor              
BenchmarkNew-12                 1000000000               0.2561 ns/op          0 B/op          0 allocs/op
BenchmarkGet-12                 14617033                82.80 ns/op           18 B/op          1 allocs/op
BenchmarkGetForId-12            21623218                51.25 ns/op           16 B/op          1 allocs/op
BenchmarkGetWithPostfix-12      10882941               111.7 ns/op            26 B/op          1 allocs/op
BenchmarkGetPostfixId-12        31611556                37.07 ns/op           20 B/op          0 allocs/op
PASS
ok      github.com/anandvarma/namegen   5.293s

namegen's People

Contributors

anandvarma avatar gegaryfa avatar rafiramadhana avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

namegen's Issues

Extend the dictionary_test to spot duplicates across dictionaries

Today there exists logic to detect duplicates within a given dictionary of words. We need to add a test to spot duplicates across dictionaries too. For instance colors might show up in the color and adjective dictionaries which when used together could end up creating names with duplicate words in them.

Add benchmarks

Add benchmarks and optimize runtime and memory allocations.

Preallocate strings.Builder

Since we know the size of all the random strings as well as the postfix before hand, we can preallocate the builder to cut down on resizes and to make the memory allocations more deterministic.

Evaluate reusing the strings.Builder object

Every call to GetForId() creates a new strings.Builder object. Although this allocation should happen on the stack and is not a concern as such, see if it would help to Reset() the builder instead of creating a new one each time. Might come in useful particularly when we need not return the string such as with #9

Optimize allocations in GetForId()

GetForId() makes 3 allocations without prefix and 5 with. This is mostly down to initialized array appends and the call to strings.Join. Also evaluate and use strings.Builder to cut down on concatenation related allocations.

Benchmark outputs for reference:
BenchmarkGet-4 4428757 256.4 ns/op 65 B/op 3 allocs/op
BenchmarkGetWithPostfix-4 2684820 440.3 ns/op 162 B/op 5 allocs/op

Update README

  • Features
  • API documentation
  • Examples
  • CLI screenshots and usage

Implement io.Reader

See if it makes sense to implement the io.Reader interface for zero copy use-cases.

Optimize getPostfixId() memory usage

The getPostfixId implementation first encodes the random number into base-10 or base-36 and then returns a substring of it.
These encoded intermediary strings are 24 Bytes long in the common case. But the returned strings are typically 1-8 Bytes, leading to a large amount of fragmentation and trapped memory.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.