Giter VIP home page Giter VIP logo

betteralign's Introduction

betteralign

GitHub license GitHub release

About

betteralign is a tool to detect structs that would use less memory if their fields were sorted and optionally sort such fields.

This is a fork of an official Go fieldalignment tool and vast majority of the alignment code has remained the same. There are however some notable changes:

  • skips over generated files, either files with known "generated" suffix (_generated.go, _gen.go, .gen.go, .pb.go, .pb.gw.go) or due to package-level comment containing Code generated by... DO NOT EDIT. string,
  • skips over test files (files with _test.go suffix),
  • skips over structs marked with comment betteralign:ignore,
  • doesn't lose comments (field comments, doc comments, floating comments or otherwise) but the comment position heuristics is still work in progress,
  • does very reliable atomic file I/O with strong promise not to corrupt and/or lose contents upon rewrite (not on Windows platform),
  • has more thorough testing in regards to expected optimised vs golden results,
  • integrates better with environments with restricted CPU and/or memory resources (Docker containers, K8s containers, LXC, LXD etc).

Retaining comments has been done with using DST (Decorated Syntax Tree) with decorating regular AST. Sadly when using DST we cannot use "fix" mode with SuggestedFixes, but we have to print whole DST to retain decorations.

In case you are wondering why DST and not AST, in general sense AST does not associate comments to nodes, but it holds fixed offsets. Original fieldalignment tool just erases all struct/field/floating comments due to this issue and while there is a CL with a possible fix, it's still a work in progress as of this time.

Note, this is a single-pass analysis tool and sorting all structs optimally might require more than one pass.

Let us also mention original projects handling this task and without which this derivative work wouldn't exist in the first place:

Installation

There are two ways of installing betteralign:

Manual:

Download your preferred flavor from the releases page and install manually, typically to /usr/local/bin/betteralign

Using go install:

go install github.com/dkorunic/betteralign/cmd/betteralign@latest

Usage

betteralign: find structs that would use less memory if their fields were sorted

Usage: betteralign [-flag] [package]

Flags:
  -apply
    	apply suggested fixes
  -generated_files
    	also check and fix generated files
  -test_files
    	also check and fix test files

To get all recommendations on your project:

betteralign ./...

To automatically fix your project files, while ignoring test and generated files:

betteralign -apply ./...

It is possible to include generated files and test files by enabling generated_files and/or test_files flags.

Star history

Star History Chart

betteralign's People

Contributors

dkorunic avatar dependabot[bot] avatar marselester avatar

Watchers

 avatar

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.