Skip to content

Request for better struct alignment and allocation with 6g and 8g compilers #482

@gopherbot

Description

@gopherbot

by kenliz@cruzio.com:

Currently the 6g and 5g compilers impose an 8 byte alignment on structs containing small
data 
elements.

This is a request to set struct alignment to the biggest alignment required by the
elements.

This will make structs competitive with arrays when considering storage requirements for
large 
collections.

What steps will reproduce the problem?

1. compile, link, and run the following program:

package main
import "unsafe"
type testS struct {
    a uint16
    b uint16
}
type testA [2] uint16
func main() {
    var ts testS
    var tsa [100]testS
    var ta testA
    var taa [100]testA
    println("testS", unsafe.Sizeof(ts), unsafe.Alignof(ts))
    println("[100]testS", unsafe.Sizeof(tsa), unsafe.Alignof(tsa))
    println("testA", unsafe.Sizeof(ta), unsafe.Alignof(ta))
    println("[100]testA", unsafe.Sizeof(taa), unsafe.Alignof(taa))
}

2. The output produced by 6g and 6l is:

testS 8 8
[100]testS 800 8
testA 4 2
[100]testA 400 2

What is the expected output? What do you see instead?

The expected output is that a two element struct is identical in memory requirements to
a two 
element array when the element types are all the same.

Instead, there is a 100% storage penalty for using struct instead of array.

What is your $GOOS?  $GOARCH?
darwin amd64

Which revision are you using?  (hg identify)
a6fcf4303b0a+ release/release.2009-12-22
(same results with latest tip or with x86)

Please provide any additional information below.
I've been told that the 5g compiler behaves the same , but the gccgo compiler does the
more 
efficient alignment and allocation.

Attachments:

  1. test.go (423 bytes)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions