Tool to detect Go structs that would take less memory if their fields were sorted.

Owner
Matthew Dempsky
I eat plants and write Go.
Matthew Dempsky
Comments
  • maligned ignores custom build tags recently

    maligned ignores custom build tags recently

    Since c37866e2d38b8e33600b215e4095af870becd4d8, maligned complains on duplicate methods in different custom build tags.

    main.go

    package main
    
    func main() {
    	debug("x")
    }
    

    debug.go

    // +build debug
    
    package main
    
    func debug(s string) { println(s) }
    

    release.go

    // +build !debug
    
    package main
    
    func debug(string) {}
    
     % maligned *.go
    /tmp/release.go:5:6: debug redeclared in this block
    /tmp/debug.go:5:6:      other declaration of debug
    /tmp/release.go:5:6: debug redeclared in this block
    /tmp/debug.go:5:6:      other declaration of debug
    maligned: 4 errors during loading
    

    Before c37866e2d38b8e33600b215e4095af870becd4d8, maligned worked well in this situation.

  • Use unsafe.Sizeof() to get basic types' sizes

    Use unsafe.Sizeof() to get basic types' sizes

    This seems much simpler and more reliable than hard-coding the sizes. Additionally, it seems strange that types.Int and types.Uint weren't originally included in the basicSizes map. Maybe they were defined as aliases of size-specific types when maligned was written but have since been redefined as unique types?

  • Print optimal field order

    Print optimal field order

    For complex structs, it's almost impossible to find the correct combination without knowing the fine details of the alignment rules.

    If it was easy, this whole tool would not be needed at all.

  • Optimize for pointer slots too

    Optimize for pointer slots too

    When scanning objects during GC, the standard compiler/runtime only scan as far as the last pointer slot in an object. If fields are sorted so that all of the pointer-y fields come first, then the GC may be able to short-circuit its scan work slightly.

  • github.com/golang/tools API change

    github.com/golang/tools API change

    I got this compilation error while executing go get github.com/mdempsky/maligned with Go1.5.1:

    ./maligned.go:50: impossible type assertion: *"golang.org/x/tools/go/types".Struct does not implement "go/types".Type (wrong type for Underlying method) have Underlying() "golang.org/x/tools/go/types".Type want Underlying() "go/types".Type

    github.com/golang/tools introduced an API change for go/types around commit: https://github.com/golang/tools/commit/2477c0d5780ddec6439a9322a6862dfe20cc8b81.

  • port from go/loader to go/packages

    port from go/loader to go/packages

    As pointed out on Twitter. This would add support for modules, and also modernize the tool quite a bit as go/loader hasn't been supported in years.

    When done, please update https://github.com/golang/go/issues/24661 with a comment so that the "maligned" checklist item can be ticked off.

  • Handle [package ...] arguments like most Go tools.

    Handle [package ...] arguments like most Go tools.

    Support import path patterns, including within relative import paths. When given 0 arguments, use the package in current working directory. If the import path patterns match no packages, print a warning and exit with 0 exit code.

    Rely on go/loader to resolve relative import paths relative to current working directory.

    Fixes #4.

    Similar to mdempsky/unconvert#9.

  • Skipping check per struct

    Skipping check per struct

    Hi,

    I am wondering if I can explicitly skip running maligned check on any given struct rather than globally checking either all or none?

    Regards

    This is how I wish to return JSON response

    {
       "id": 1,
       "name": "hello",
       "is_active": true,
       "created_at": "2019-01-01"
    }
    
    struct{
    	ID       	uint16
    	Name     	string
    	IsActive 	bool
    	CreatedAt	string
    }
    

    However, it is not possible because struct is not maligned and I have to malign it and the response looks odd.

    struct{
    	Name     	string
    	CreatedAt	string
    	ID       	uint16
    	IsActive 	bool
    }
    
    {
       "name": "hello",
       "created_at": "2019-01-01",
       "id": 1,
       "is_active": true
    }
    

    Some annotation solution like this, maybe!

    // @skip: maligned
    struct{
    	ID       	uint16
    	Name     	string
    	IsActive 	bool
    	CreatedAt	string
    }
    
  • Archiving repo to prevent usage of deprecated tool

    Archiving repo to prevent usage of deprecated tool

    👋🏽 hi

    What

    awesome tool! I see it is deprecated, so I think it's best to archive the repo, which will keep it public, but warn the users that it is no longer maintained 🙂

  • using autofixing in own code

    using autofixing in own code

    hi, i know that fieldalignment now preferred way to check/fix field alignment in golang structs. how to deal with it if i want to have automatic align fields via own golang program? (for example i'm generate code from protobuf)

  • Add verbose info

    Add verbose info

    Add -v to display fields where padding is added.

    Example:

    ...\encoder_options.go:14:21: struct of size 64 could be 56
    Field sizes in order:
            *  concurrent int: 8:8
            *  crc bool: 1:1
            !!!  single *bool: 8:15
            *  pad int: 8:8
            *  blockSize int: 8:8
            *  windowSize int: 8:8
            *  level github.com/klauspost/compress/zstd.EncoderLevel: 8:8
            *  fullZero bool: 1:1
            *  noEntropy bool: 1:1
    
  • Add brief summary of memory alignment to README

    Add brief summary of memory alignment to README

    This is a great project. Thank you for sharing it with the community.

    I discovered the project when gometalinter ran against some of my code and found an inefficient alignment in one of my code's structs. But when I looked up the project, I found nothing in the README or the GoDoc that explained why memory alignment is a good thing or how I might achieve it.

    I read Padding is hard which helped, but wonder if we might add something in the README or even the source code to help others who might have the same problem.

    /cc @markmandel

  • Possible defect on malaligned calc

    Possible defect on malaligned calc

    Given the code:

    model.go

    package addrs
    
    // ASDExchanger bla bla
    type ASDExchanger struct {
    	ASDBtcExchangeRate    string `mapstructure:"asd_btc_exchange_rate"`
    	ASDBtcExchangeRateUSD string `mapstructure:"asd_btc_exchange_rate_usd"`
    	ASDBtcExchangeLabel   string `mapstructure:"asd_btc_exchange_label"`
    	ASDBtcExchangeEnabled bool   `mapstructure:"asd_btc_exchange_enabled"`
    
    	ASDEthExchangeRate    string `mapstructure:"asd_eth_exchange_rate"`
    	ASDEthExchangeRateUSD string `mapstructure:"asd_eth_exchange_rate_usd"`
    	ASDEthExchangeLabel   string `mapstructure:"asd_eth_exchange_label"`
    	ASDEthExchangeEnabled bool   `mapstructure:"asd_eth_exchange_enabled"`
    
    	ASDSkyExchangeRate    string `mapstructure:"asd_sky_exchange_rate"`
    	ASDSkyExchangeRateUSD string `mapstructure:"asd_sky_exchange_rate_usd"`
    	ASDSkyExchangeLabel   string `mapstructure:"asd_sky_exchange_label"`
    	ASDSkyExchangeEnabled bool   `mapstructure:"asd_sky_exchange_enabled"`
    
    	ASDWavesExchangeRate    string `mapstructure:"asd_waves_exchange_rate"`
    	ASDWavesExchangeRateUSD string `mapstructure:"asd_waves_exchange_rate_usd"`
    	ASDWavesExchangeLabel   string `mapstructure:"asd_waves_exchange_label"`
    	ASDWavesExchangeEnabled bool   `mapstructure:"asd_waves_exchange_enabled"`
    
    	ASDWavesASDExchangeRate    string `mapstructure:"asd_waves_asd_exchange_rate"`
    	ASDWavesASDExchangeRateUSD string `mapstructure:"asd_waves_asd_exchange_rate_usd"`
    	ASDWavesASDExchangeLabel   string `mapstructure:"asd_waves_asd_exchange_label"`
    	ASDWavesASDExchangeEnabled bool   `mapstructure:"asd_waves_asd_exchange_enabled"`
    }
    

    When I run gometalinter and maligned:

    gometalinter --deadline=3m -j 2 --disable-all --tests --vendor \                                                                                             
            -E maligned \       
            ./...
    

    Then throws me err:

    model.go:4:19:warning: struct of size 280 could be 248 (maligned)
    

    When I remove the bool and or replace with string, it works with no errs.

    Any ideas ?

Executor - Fast exec task with go and less mem ops

executor fast exec task with go and less mem ops Why we need executor? Go with g

Dec 19, 2022
Highly extensible, customizable application launcher and window switcher written in less than 300 lines of Golang and fyne
Highly extensible, customizable application launcher and window switcher written in less than 300 lines of Golang and fyne

golauncher A go application launcher A simple, highly extensible, customizable application launcher and window switcher written in less than 300 lines

Aug 21, 2022
Solver for wordle hard mode - achieves 5 attempts or less 100% of the time

wordier Solver for wordle hard mode - achieves 5 attempts or less 100% of the time Example - Spoiler ➜ wordier git:(master) ✗ go run main.go scamp ➜

Jan 12, 2022
customer.io full stack engineer take home project
customer.io full stack engineer take home project

customer.io full stack engineer take home project

Jan 21, 2022
[TOOL, CLI] - Filter and examine Go type structures, interfaces and their transitive dependencies and relationships. Export structural types as TypeScript value object or bare type representations.

typex Examine Go types and their transitive dependencies. Export results as TypeScript value objects (or types) declaration. Installation go get -u gi

Dec 6, 2022
RTS: request to struct. Generates Go structs from JSON server responses.

RTS: Request to Struct Generate Go structs definitions from JSON server responses. RTS defines type names using the specified lines in the route file

Dec 7, 2022
Initialize structs with default values

defaults Initialize structs with default values Supports almost all kind of types Scalar types int/8/16/32/64, uint/8/16/32/64, float32/64 uintptr, bo

Jan 7, 2023
Notification library for gophers and their furry friends.
Notification library for gophers and their furry friends.

Shoutrrr Notification library for gophers and their furry friends. Heavily inspired by caronc/apprise. Quick Start As a package Using shoutrrr is easy

Jan 3, 2023
Gorsair hacks its way into remote docker containers that expose their APIs
Gorsair hacks its way into remote docker containers that expose their APIs

Gorsair Gorsair is a penetration testing tool for discovering and remotely accessing Docker APIs from vulnerable Docker containers. Once it has access

Dec 31, 2022
This project is an implementation of Fermat's factorization method in which multiples of prime numbers are factored into their constituent primes

This project is an implementation of Fermat's factorization method in which multiples of prime numbers are factored into their constituent primes. It is a vanity attempt to break RSA Encryption which relies on prime multiples for encryption.

Jun 3, 2022
A profiling tool to peek and profile the memory or cpu usage of a process
A profiling tool to peek and profile the memory or cpu usage of a process

Peekprof Get the CPU and Memory usage of a single process, monitor it live, and extract it in CSV and HTML. Get the best out of your optimizations. Us

Jan 9, 2023
:chart_with_upwards_trend: Monitors Go MemStats + System stats such as Memory, Swap and CPU and sends via UDP anywhere you want for logging etc...

Package stats Package stats allows for gathering of statistics regarding your Go application and system it is running on and sent them via UDP to a se

Nov 10, 2022
Helps exercise your memory by giving you random tokens and poems to memorize.

memory-enhancer Helps exercise your memory by giving you random tokens and poems to memorize. Using Every day when you first open your terminal you wi

Nov 9, 2021
Implement a toy in-memory store information service for a delivery company

Implement a toy in-memory store information service for a delivery company

Nov 22, 2021
An in-memory, key-value store HTTP API service

This is an in-memory key-value store HTTP API service, with the following endpoints: /get/{key} : GET method. Returns the value of a previously set ke

May 23, 2022
A project that provides an in-memory key-value store as a REST API. Also, it's containerized and can be used as a microservice.

Easy to Use In-Memory Key-Value Store A project that provides an in-memory key-value store as a REST API. Also, it's containerized and can be used as

Mar 6, 2022
Safira - In memory of my dog Safira
Safira - In memory of my dog Safira

In memory of my dog Safira. The tool safira replaces pattern_offset.rb and patte

Jan 11, 2022
Proc-peepin - Capture process cpu and memory and send it off to influx

proc-peepin Capture process cpu and memory and send it off to influx Running loc

Feb 13, 2022
The forgotten go tool that executes and caches binaries included in go.mod files.
The forgotten go tool that executes and caches binaries included in go.mod files.

The forgotten go tool that executes and caches binaries included in go.mod files. This makes it easy to version cli tools in your projects such as gol

Sep 27, 2022