diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2021-04-21 08:01:30 +0200 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2021-04-21 14:05:20 +0200 |
| commit | ad3ce6afab30fb68cfcda40a1910b87279795c5a (patch) | |
| tree | 6a6febdaae83e071a85576bb6fa1fd5b8abb2936 /pkg/compiler | |
| parent | 7ffd6ad61b0d0c6290f73b3652f79c63b305351e (diff) | |
pkg/compiler: optimize array[const] representation
Represent array[const[X, int8], N] as string["XX...X"].
This replaces potentially huge number of:
NONFAILING(*(uint8_t*)0x2000126c = 0);
NONFAILING(*(uint8_t*)0x2000126d = 0);
NONFAILING(*(uint8_t*)0x2000126e = 0);
with a single memcpy. In one reproducer we had 3991 such lines.
Also replace memcpy's with memset's when possible.
Update #1070
Diffstat (limited to 'pkg/compiler')
| -rw-r--r-- | pkg/compiler/types.go | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/pkg/compiler/types.go b/pkg/compiler/types.go index b2c8c3204..038a05f24 100644 --- a/pkg/compiler/types.go +++ b/pkg/compiler/types.go @@ -4,6 +4,8 @@ package compiler import ( + "bytes" + "encoding/binary" "fmt" "sort" "strconv" @@ -249,6 +251,28 @@ var typeArray = &typeDesc{ RangeEnd: end, } } + if ct, ok := elemType.(*prog.ConstType); ok && + (ct.ArgFormat == prog.FormatNative || ct.ArgFormat == prog.FormatBigEndian) && + kind == prog.ArrayRangeLen && begin == end { + // Special case: const string takes less space in C programs. + base.TypeSize = begin * ct.Size() + base.TypeAlign = ct.TypeAlign + val := make([]byte, 8) + if ct.ArgFormat == prog.FormatBigEndian { + binary.BigEndian.PutUint64(val, ct.Val) + val = val[8-ct.Size():] + } else { + binary.LittleEndian.PutUint64(val, ct.Val) + val = val[:ct.Size()] + } + val = bytes.Repeat(val, int(begin)) + return &prog.BufferType{ + TypeCommon: base.TypeCommon, + Kind: prog.BufferString, + Values: []string{string(val)}, + NoZ: true, + } + } // TypeSize is assigned later in layoutArray. base.TypeAlign = elemType.Alignment() return &prog.ArrayType{ |
