Telegram Group & Telegram Channel
Note #19 Reduce sturct size (maligned) 📄

Недавно много говорили, о том что одна и та же структура (type struct), с разными типами полей и с разным порядком, может занимать разное кол-во памяти в зависимости от того, как расположить в памяти компьютера с учетом всех отступов. Более детально прочитать можно в вики [1], почему именно так происходит.

Хотелось бы, чтобы компилятор самостоятельно делал такие оптимизации.

Возьмем структура A:
$ cat main.go
// A is my struct
type A struct {
a bool // 1 byte
b float64 // 8 bytes
c int32 // 4 bytes
}


Хорошо что есть отличный линтер[2], который напомнит нам, что можно что-то улучшить:

$ golangci-lint run --enable=maligned main.go
main.go:4:8: struct of size 24 bytes could be of size 16 bytes (maligned)
type A struct {
^


либо

go get github.com/mdempsky/maligned
$ maligned .
/Users/andrii/work/us/🇺🇦 Go на двоих/com.golang_for_two/main.go:4:8: struct of size 24 could be 16


Links:
- https://en.wikipedia.org/wiki/Data_structure_alignment
- https://github.com/mdempsky/maligned



tg-me.com/golang_for_two/43
Create:
Last Update:

Note #19 Reduce sturct size (maligned) 📄

Недавно много говорили, о том что одна и та же структура (type struct), с разными типами полей и с разным порядком, может занимать разное кол-во памяти в зависимости от того, как расположить в памяти компьютера с учетом всех отступов. Более детально прочитать можно в вики [1], почему именно так происходит.

Хотелось бы, чтобы компилятор самостоятельно делал такие оптимизации.

Возьмем структура A:

$ cat main.go
// A is my struct
type A struct {
a bool // 1 byte
b float64 // 8 bytes
c int32 // 4 bytes
}


Хорошо что есть отличный линтер[2], который напомнит нам, что можно что-то улучшить:

$ golangci-lint run --enable=maligned main.go
main.go:4:8: struct of size 24 bytes could be of size 16 bytes (maligned)
type A struct {
^


либо

go get github.com/mdempsky/maligned
$ maligned .
/Users/andrii/work/us/🇺🇦 Go на двоих/com.golang_for_two/main.go:4:8: struct of size 24 could be 16


Links:
- https://en.wikipedia.org/wiki/Data_structure_alignment
- https://github.com/mdempsky/maligned

BY 🇺🇦 Go for two :)


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/golang_for_two/43

View MORE
Open in Telegram


🇺🇦 Go на двоих Telegram | DID YOU KNOW?

Date: |

Can I mute a Telegram group?

In recent times, Telegram has gained a lot of popularity because of the controversy over WhatsApp’s new privacy policy. In January 2021, Telegram was the most downloaded app worldwide and crossed 500 million monthly active users. And with so many active users on the app, people might get messages in bulk from a group or a channel that can be a little irritating. So to get rid of the same, you can mute groups, chats, and channels on Telegram just like WhatsApp. You can mute notifications for one hour, eight hours, or two days, or you can disable notifications forever.

The S&P 500 slumped 1.8% on Monday and Tuesday, thanks to China Evergrande, the Chinese property company that looks like it is ready to default on its more-than $300 billion in debt. Cries of the next Lehman Brothers—or maybe the next Silverado?—echoed through the canyons of Wall Street as investors prepared for the worst.

🇺🇦 Go на двоих from us


Telegram 🇺🇦 Go for two :)
FROM USA