Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize memory alignment of structs #14

Merged
merged 5 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,30 @@ import (

type (
ConfigFile struct {
DbUrl string `yaml:"db_url" env:"DB_URL" env-required:"true"`
BotToken string `yaml:"bot_token" env:"BOT_TOKEN" env-required:"true"`
BotChannelId string `yaml:"bot_channel_id" env-required:"true"`
OneVOne EloType `yaml:"1v1"`
TwoVTwo EloType `yaml:"2v2"`
ThreeVThree EloType `yaml:"3v3"`
FourVFour EloType `yaml:"4v4"`
Custom EloType
EloTypes []EloType `yaml:"-"`
AdminRoles []string `yaml:"admin_roles,flow"`
AdminRolesMap map[string]bool `yaml:"-"`
DbUrl string `yaml:"db_url" env:"DB_URL" env-required:"true"`
BotToken string `yaml:"bot_token" env:"BOT_TOKEN" env-required:"true"`
BotChannelId string `yaml:"bot_channel_id" env-required:"true"`
AdminRoles []string `yaml:"admin_roles,flow"`
EloTypes []EloType `yaml:"-"`
OneVOne EloType `yaml:"1v1"`
TwoVTwo EloType `yaml:"2v2"`
ThreeVThree EloType `yaml:"3v3"`
FourVFour EloType `yaml:"4v4"`
Custom EloType
}

EloType struct {
Enabled bool
RoleMap map[string]int16 `yaml:"-"`
Roles []EloRole `yaml:"roles,omitempty"`
RoleMap map[string]int32 `yaml:"-"`
Enabled bool
}

EloRole struct {
RoleId string `yaml:"role_id"`
RolePriority int32 `yaml:"role_priority"`
StartingElo int32 `yaml:"starting_elo"`
EndingElo int32 `yaml:"ending_elo"`
RolePriority int16 `yaml:"role_priority"`
StartingElo int16 `yaml:"starting_elo"`
EndingElo int16 `yaml:"ending_elo"`
}
)

Expand Down Expand Up @@ -85,7 +85,7 @@ func init() {
&Cfg.Custom,
} {
if eloType.Enabled && len(eloType.Roles) != 0 {
eloType.RoleMap = make(map[string]int32, len(eloType.Roles))
eloType.RoleMap = make(map[string]int16, len(eloType.Roles))
for _, role := range eloType.Roles {
eloType.RoleMap[role.RoleId] = role.RolePriority
}
Expand Down
56 changes: 28 additions & 28 deletions internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ type User struct {
DiscordUserID string
Aoe4Username string
Aoe4Id string
CurrentElo UserElo
NewElo UserElo
CurrentElo userElo
NewElo userElo
}

type UserElo struct {
OneVOne int32
TwoVTwo int32
ThreeVThree int32
FourVFour int32
Custom int32
type userElo struct {
OneVOne int16
TwoVTwo int16
ThreeVThree int16
FourVFour int16
Custom int16
}

var Db *pgxpool.Pool
Expand All @@ -41,18 +41,18 @@ func init() {
username text not null,
guild_id varchar(20),
aoe_id varchar(40) not null,
elo_1v1 int,
elo_2v2 int,
elo_3v3 int,
elo_4v4 int,
elo_custom int,
elo_1v1 smallint,
elo_2v2 smallint,
elo_3v3 smallint,
elo_4v4 smallint,
elo_custom smallint,
primary key(discord_id, guild_id)
)`); err != nil {
log.Fatalf("error setting up database: %v\n", err)
}
}

func RegisterUser(username string, aoeId string, discordId string, guildId string) (err error) {
func RegisterUser(username string, aoeId string, discordId string, guildId string) error {
updateUser, err := Db.Exec(context.Background(),
"update users set username = $1, aoe_id = $2 where discord_id = $3 and guild_id = $4",
username, aoeId, discordId, guildId)
Expand All @@ -67,10 +67,10 @@ func RegisterUser(username string, aoeId string, discordId string, guildId strin
}
}

return
return nil
}

func UpdateUserElo(discordId string, guildId string, elo UserElo) error {
func UpdateUserElo(discordId string, guildId string, elo userElo) error {
updateUser, err := Db.Exec(context.Background(),
`update users set elo_1v1 = $1, elo_2v2 = $2, elo_3v3 = $3, elo_4v4 = $4, elo_custom = $5
where discord_id = $6 and guild_id = $7`,
Expand All @@ -88,8 +88,8 @@ func UpdateUserElo(discordId string, guildId string, elo UserElo) error {
func GetUser(discordId string, guildId string) (*User, error) {
row := Db.QueryRow(context.Background(), "select * from users where discord_id = $1 and guild_id = $2", discordId, guildId)

var oneVOne, twoVTwo, threeVThree, fourVFour, custom pgtype.Int4
u := &User{}
var oneVOne, twoVTwo, threeVThree, fourVFour, custom pgtype.Int2
if err := row.Scan(
&u.DiscordUserID,
&u.Aoe4Username,
Expand All @@ -100,10 +100,10 @@ func GetUser(discordId string, guildId string) (*User, error) {
&threeVThree,
&fourVFour,
&custom); err != nil {
return &User{}, err
return nil, err
}

u.pgToInt(oneVOne, twoVTwo, threeVThree, fourVFour, custom)
u.pgToCurrentElo(oneVOne, twoVTwo, threeVThree, fourVFour, custom)

return u, nil
}
Expand All @@ -116,8 +116,8 @@ func GetUsers(guildId string) (users []User, err error) {
defer rows.Close()

for rows.Next() {
var oneVOne, twoVTwo, threeVThree, fourVFour, custom pgtype.Int4
u := User{}
var u User
var oneVOne, twoVTwo, threeVThree, fourVFour, custom pgtype.Int2
if err := rows.Scan(
&u.DiscordUserID,
&u.Aoe4Username,
Expand All @@ -131,20 +131,20 @@ func GetUsers(guildId string) (users []User, err error) {
return nil, err
}

u.pgToInt(oneVOne, twoVTwo, threeVThree, fourVFour, custom)
u.pgToCurrentElo(oneVOne, twoVTwo, threeVThree, fourVFour, custom)

users = append(users, u)
}

return
}

func (u *User) pgToInt(
oneVOne pgtype.Int4,
twoVTwo pgtype.Int4,
threeVThree pgtype.Int4,
fourVFour pgtype.Int4,
custom pgtype.Int4,
func (u *User) pgToCurrentElo(
oneVOne pgtype.Int2,
twoVTwo pgtype.Int2,
threeVThree pgtype.Int2,
fourVFour pgtype.Int2,
custom pgtype.Int2,
) {
if config.Cfg.OneVOne.Enabled && oneVOne.Status == pgtype.Present {
u.CurrentElo.OneVOne = oneVOne.Int
Expand Down
33 changes: 0 additions & 33 deletions internal/db/elostring.go

This file was deleted.

Loading