• Stars
    star
    790
  • Rank 57,622 (Top 2 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created about 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

structslop is a static analyzer for Go that recommends struct field rearrangements to provide for maximum space/allocation efficiency.

structslop

Build status

Package structslop defines an Analyzer that checks if struct fields can be re-arranged to optimize size.

Installation

With Go modules:

go get github.com/orijtech/structslop/cmd/structslop

Without Go modules:

$ cd $GOPATH/src/github.com/orijtech/structslop
$ git checkout v0.0.6
$ go get
$ install ./cmd/structslop

Usage

You can run structslop either on a Go package or Go files, the same way as other Go tools work.

Example:

$ structslop github.com/orijtech/structslop/testdata/src/struct

or:

$ structslop ./testdata/src/struct/p.go

Sample output:

/go/src/github.com/orijtech/structslop/testdata/struct/p.go:30:9: struct has size 24 (size class 32), could be 16 (size class 16), you'll save 50.00% if you rearrange it to:
struct {
	y uint64
	x uint32
	z uint32
}

/go/src/github.com/orijtech/structslop/testdata/struct/p.go:36:9: struct has size 40 (size class 48), could be 24 (size class 32), you'll save 33.33% if you rearrange it to:
struct {
	_  [0]func()
	i1 int
	i2 int
	a3 [3]bool
	b  bool
}

/go/src/github.com/orijtech/structslop/testdata/struct/p.go:59:9: struct has size 40 (size class 48), could be 32 (size class 32), you'll save 33.33% if you rearrange it to:
struct {
	y uint64
	t *httptest.Server
	w uint64
	x uint32
	z uint32
}

/go/src/github.com/orijtech/structslop/testdata/struct/p.go:67:9: struct has size 40 (size class 48), could be 32 (size class 32), you'll save 33.33% if you rearrange it to:
struct {
	y uint64
	t *s
	w uint64
	x uint32
	z uint32
}

Example, for the first report above, the output meaning:

  • The current struct size is 24, the size that the Go runtime will allocate for that struct is 32.
  • The optimal struct size is 16, the size that the Go runtime will allocate for that struct is 16.
  • The layout of optimal struct.
  • The percentage savings with new struct layout.

That said, some structs may have a smaller size, but for efficiency, the Go runtime will allocate them in the same size class, then those structs are not considered sloppy:

type s1 struct {
	x uint32
	y uint64
	z *s
	t uint32
}

and:

type s2 struct {
	y uint64
	z *s
	x uint32
	t uint32
}

have the same size class 32, though s2 layout is only 24 byte in size.

However, you can still get this information when you want, using -verbose flag:

$ structslop -verbose ./testdata/src/verbose/p.go
/go/src/github.com/orijtech/structslop/testdata/src/verbose/p.go:17:8: struct has size 0 (size class 0)
/go/src/github.com/orijtech/structslop/testdata/src/verbose/p.go:19:9: struct has size 1 (size class 8)
/go/src/github.com/orijtech/structslop/testdata/src/verbose/p.go:23:9: struct has size 32 (size class 32), could be 24 (size class 32), optimal fields order:
struct {
	y uint64
	z *s
	x uint32
	t uint32
}

Note

For applying suggested fix, use -apply flag, instead of -fix.

Development

Go 1.20+

Running test

Add test case to testdata/src/struct directory, then run:

go test

Contributing

TODO

More Repositories

1

frontender

Setup a server frontend with HTTPS that then proxies to traffic to a backend/cluster
Go
32
star
2

httperroryzer

Static analyzer to catch invalid uses of http.Error without a return statement which can cause expected bugs
Go
24
star
3

uber

Uber API client
Go
19
star
4

infra

Infrastructure management for Google Cloud Platform
Go
14
star
5

media-search

Media search's code
Go
14
star
6

arxiv

Go API client for arxiv.org
Go
12
star
7

prunehorst

Prune HORST -- Go port of Jean-Phillipe Aumasson and Guillaume Endignoux's new Post Quantum algorithm
Go
10
star
8

asana

Asana API client in Go
Go
10
star
9

opencensus-for-grpc-go-developers

Tutorial for OpenCensus for gRPC Go developers
Go
9
star
10

staticmajor-action

Public GitHub Action for staticmajor
7
star
11

tickeryzer

Check missing (*time.Ticker).Stop() call, which can cause resources leak.
Go
6
star
12

go-opencensus-integrations

OpenCensus integrations for the Go programming language
Go
5
star
13

consensuswarn

cosmos-sdk state diffing tool
Go
5
star
14

sigchanyzer

signalchan is a static analyzer for Go to detect usage of unbuffer os.Signal channel, which can be at risk of missing the signal.
Go
5
star
15

giphy

Giphy API client in Go
Go
4
star
16

otils

Go utility functions
Go
4
star
17

chdbg

Cosmos Chain Halt Debugger
Go
4
star
18

pragmyzer

Static analyzer to flag wrong pragma uses in Go such as "// go:embed file" instead of "//go:embed file"
Go
4
star
19

cosmosloadtester

Cosmos load tester
TypeScript
4
star
20

cacher

Global resources cacher with AWS S3 for storage and Google Cloud Spanner for the DB
Go
3
star
21

coinbase

coinbase API client
Go
3
star
22

oragent-external

Orijtech observability service
Shell
3
star
23

youtube

YouTube client for the command line
Go
3
star
24

500px

Go API client for 500px
Go
3
star
25

opencensus-for-grpc-python-developers

OpenCensus for gRPC Python developers
Python
3
star
26

haraka

Haraka v2 – Efficient Short-Input Hashing for Post-Quantum Applications
Go
3
star
27

earthquake-visualizations

Earthquake visualizations by applying k-means clustering
Go
3
star
28

wsu

Websockets Utilities
Go
2
star
29

rspjs

Realtime Search Platform
JavaScript
2
star
30

opentelemetry-go-exporter-oragent

OpenTelemetry Go exporters for Oragent
Go
2
star
31

opencensus-demos

OpenCensus demos of instrumented backends
Go
2
star
32

authmid

Authentication middleware for signed requests, useful for webhook authentication verifying identities
Go
2
star
33

telemetry-perfbench

Benchmarks and comparisons between various telemetry projects
Go
2
star
34

mapbox

Mapbox Go API
Go
1
star
35

orijgo

Go API clients for accessing the orijtech API services
Go
1
star
36

fandango

Fandango API client in Go
Go
1
star
37

medisa

MEDISA(media search app)
CSS
1
star
38

goshippo

Ship your packages programmatically with GoShippo's API
Go
1
star
39

golico

Apache 2.0, BSD LICENSE conformation tool for Go source code files
Go
1
star
40

odexchange

odexchange
Go
1
star
41

sqlcommenter-website

sqlcommenter-website
JavaScript
1
star
42

ocjedis

Jedis wrapper instrumented with OpenCensus
Java
1
star
43

promreceiver

Prometheus receiver that then converts into OpenCensus metrics
Go
1
star
44

orijpython

Python client for the orijtech API
Python
1
star
45

nasa

API client for various endpoints for NASA APIs e.g Mars Rover pictures
Go
1
star
46

text2speech

Text to Speech packages
Go
1
star
47

gcla

Github CLAs
Go
1
star
48

uberclick

Uber button for any website
Go
1
star
49

arxiv-instant

arXiv instant search powered by rsp powered by https://github.com/orijtech/arxiv
HTML
1
star
50

zeroconv

Go
1
star
51

tos3

Helper API for talking to Amazon's S3
Go
1
star
52

callback

Package for dealing with HTTP callbacks to backends
Go
1
star
53

mapbox-search

Mapbox search cluster/service backed by groupcache for caching
Go
1
star
54

itunes-search

Polyglot search backend cached by Redis and instrumented by OpenCensus end-to-end
Go
1
star
55

cmstore

Redis server and client setup that's easily deployable
Go
1
star
56

talks

Talks
HTML
1
star
57

test-gobencher

Testing gobencher
Go
1
star
58

itunes

iTunes search API client
Go
1
star
59

prometheus-go-metrics-exporter

OpenCensus Prometheus Go Metrics exporter
Go
1
star
60

recaptcha

ReCaptcha API client. Currently used in production at https://orijtech.com
Go
1
star