Skip to content

Commit

Permalink
Refactor struct's time to remove unnecessary memory usage (#3142)
Browse files Browse the repository at this point in the history
* refactor struct's time to remove unnecessary memory usage

* use AsTimePtr simple code

* fix tests

* fix time compare

* fix template on gpg

* use AddDuration instead of Add
  • Loading branch information
lunny authored and lafriks committed Dec 11, 2017
1 parent c082c3b commit f2e20c8
Show file tree
Hide file tree
Showing 67 changed files with 334 additions and 479 deletions.
15 changes: 5 additions & 10 deletions models/action.go
Expand Up @@ -18,6 +18,7 @@ import (
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
api "code.gitea.io/sdk/gitea"

"github.com/Unknwon/com"
Expand Down Expand Up @@ -85,15 +86,9 @@ type Action struct {
Comment *Comment `xorm:"-"`
IsDeleted bool `xorm:"INDEX NOT NULL DEFAULT false"`
RefName string
IsPrivate bool `xorm:"INDEX NOT NULL DEFAULT false"`
Content string `xorm:"TEXT"`
Created time.Time `xorm:"-"`
CreatedUnix int64 `xorm:"INDEX created"`
}

// AfterLoad is invoked from XORM after setting the values of all fields of this object.
func (a *Action) AfterLoad() {
a.Created = time.Unix(a.CreatedUnix, 0).Local()
IsPrivate bool `xorm:"INDEX NOT NULL DEFAULT false"`
Content string `xorm:"TEXT"`
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
}

// GetOpType gets the ActionType of this action.
Expand Down Expand Up @@ -229,7 +224,7 @@ func (a *Action) GetContent() string {

// GetCreate returns the action creation time.
func (a *Action) GetCreate() time.Time {
return a.Created
return a.CreatedUnix.AsTime()
}

// GetIssueInfos returns a list of issues associated with
Expand Down
11 changes: 2 additions & 9 deletions models/admin.go
Expand Up @@ -6,7 +6,6 @@ package models

import (
"fmt"
"time"

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util"
Expand All @@ -26,14 +25,8 @@ const (
type Notice struct {
ID int64 `xorm:"pk autoincr"`
Type NoticeType
Description string `xorm:"TEXT"`
Created time.Time `xorm:"-"`
CreatedUnix int64 `xorm:"INDEX created"`
}

// AfterLoad is invoked from XORM after setting the values of all fields of this object.
func (n *Notice) AfterLoad() {
n.Created = time.Unix(n.CreatedUnix, 0).Local()
Description string `xorm:"TEXT"`
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
}

// TrStr returns a translation format string.
Expand Down
13 changes: 3 additions & 10 deletions models/attachment.go
Expand Up @@ -10,11 +10,11 @@ import (
"mime/multipart"
"os"
"path"
"time"

gouuid "github.com/satori/go.uuid"

"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
)

// Attachment represent a attachment of issue/comment/release.
Expand All @@ -25,15 +25,8 @@ type Attachment struct {
ReleaseID int64 `xorm:"INDEX"`
CommentID int64
Name string
DownloadCount int64 `xorm:"DEFAULT 0"`
Created time.Time `xorm:"-"`
CreatedUnix int64 `xorm:"created"`
}

// AfterLoad is invoked from XORM after setting the value of a field of
// this object.
func (a *Attachment) AfterLoad() {
a.Created = time.Unix(a.CreatedUnix, 0).Local()
DownloadCount int64 `xorm:"DEFAULT 0"`
CreatedUnix util.TimeStamp `xorm:"created"`
}

// IncreaseDownloadCount is update download count + 1
Expand Down
30 changes: 11 additions & 19 deletions models/branches.go
Expand Up @@ -29,12 +29,10 @@ type ProtectedBranch struct {
BranchName string `xorm:"UNIQUE(s)"`
CanPush bool `xorm:"NOT NULL DEFAULT false"`
EnableWhitelist bool
WhitelistUserIDs []int64 `xorm:"JSON TEXT"`
WhitelistTeamIDs []int64 `xorm:"JSON TEXT"`
Created time.Time `xorm:"-"`
CreatedUnix int64 `xorm:"created"`
Updated time.Time `xorm:"-"`
UpdatedUnix int64 `xorm:"updated"`
WhitelistUserIDs []int64 `xorm:"JSON TEXT"`
WhitelistTeamIDs []int64 `xorm:"JSON TEXT"`
CreatedUnix util.TimeStamp `xorm:"created"`
UpdatedUnix util.TimeStamp `xorm:"updated"`
}

// IsProtected returns if the branch is protected
Expand Down Expand Up @@ -197,19 +195,13 @@ func (repo *Repository) DeleteProtectedBranch(id int64) (err error) {

// DeletedBranch struct
type DeletedBranch struct {
ID int64 `xorm:"pk autoincr"`
RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
Name string `xorm:"UNIQUE(s) NOT NULL"`
Commit string `xorm:"UNIQUE(s) NOT NULL"`
DeletedByID int64 `xorm:"INDEX"`
DeletedBy *User `xorm:"-"`
Deleted time.Time `xorm:"-"`
DeletedUnix int64 `xorm:"INDEX created"`
}

// AfterLoad is invoked from XORM after setting the values of all fields of this object.
func (deletedBranch *DeletedBranch) AfterLoad() {
deletedBranch.Deleted = time.Unix(deletedBranch.DeletedUnix, 0).Local()
ID int64 `xorm:"pk autoincr"`
RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
Name string `xorm:"UNIQUE(s) NOT NULL"`
Commit string `xorm:"UNIQUE(s) NOT NULL"`
DeletedByID int64 `xorm:"INDEX"`
DeletedBy *User `xorm:"-"`
DeletedUnix util.TimeStamp `xorm:"INDEX created"`
}

// AddDeletedBranch adds a deleted branch to the database
Expand Down
36 changes: 14 additions & 22 deletions models/gpg_key.go
Expand Up @@ -17,6 +17,7 @@ import (

"code.gitea.io/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util"

"github.com/go-xorm/xorm"
"github.com/keybase/go-crypto/openpgp"
Expand All @@ -26,17 +27,14 @@ import (

// GPGKey represents a GPG key.
type GPGKey struct {
ID int64 `xorm:"pk autoincr"`
OwnerID int64 `xorm:"INDEX NOT NULL"`
KeyID string `xorm:"INDEX CHAR(16) NOT NULL"`
PrimaryKeyID string `xorm:"CHAR(16)"`
Content string `xorm:"TEXT NOT NULL"`
Created time.Time `xorm:"-"`
CreatedUnix int64
Expired time.Time `xorm:"-"`
ExpiredUnix int64
Added time.Time `xorm:"-"`
AddedUnix int64
ID int64 `xorm:"pk autoincr"`
OwnerID int64 `xorm:"INDEX NOT NULL"`
KeyID string `xorm:"INDEX CHAR(16) NOT NULL"`
PrimaryKeyID string `xorm:"CHAR(16)"`
Content string `xorm:"TEXT NOT NULL"`
CreatedUnix util.TimeStamp `xorm:"created"`
ExpiredUnix util.TimeStamp
AddedUnix util.TimeStamp
SubsKey []*GPGKey `xorm:"-"`
Emails []*EmailAddress
CanSign bool
Expand All @@ -47,17 +45,11 @@ type GPGKey struct {

// BeforeInsert will be invoked by XORM before inserting a record
func (key *GPGKey) BeforeInsert() {
key.AddedUnix = time.Now().Unix()
key.ExpiredUnix = key.Expired.Unix()
key.CreatedUnix = key.Created.Unix()
key.AddedUnix = util.TimeStampNow()
}

// AfterLoad is invoked from XORM after setting the values of all fields of this object.
func (key *GPGKey) AfterLoad(session *xorm.Session) {
key.Added = time.Unix(key.AddedUnix, 0).Local()
key.Expired = time.Unix(key.ExpiredUnix, 0).Local()
key.Created = time.Unix(key.CreatedUnix, 0).Local()

err := session.Where("primary_key_id=?", key.KeyID).Find(&key.SubsKey)
if err != nil {
log.Error(3, "Find Sub GPGkeys[%d]: %v", key.KeyID, err)
Expand Down Expand Up @@ -163,8 +155,8 @@ func parseSubGPGKey(ownerID int64, primaryID string, pubkey *packet.PublicKey, e
KeyID: pubkey.KeyIdString(),
PrimaryKeyID: primaryID,
Content: content,
Created: pubkey.CreationTime,
Expired: expiry,
CreatedUnix: util.TimeStamp(pubkey.CreationTime.Unix()),
ExpiredUnix: util.TimeStamp(expiry.Unix()),
CanSign: pubkey.CanSign(),
CanEncryptComms: pubkey.PubKeyAlgo.CanEncrypt(),
CanEncryptStorage: pubkey.PubKeyAlgo.CanEncrypt(),
Expand Down Expand Up @@ -236,8 +228,8 @@ func parseGPGKey(ownerID int64, e *openpgp.Entity) (*GPGKey, error) {
KeyID: pubkey.KeyIdString(),
PrimaryKeyID: "",
Content: content,
Created: pubkey.CreationTime,
Expired: expiry,
CreatedUnix: util.TimeStamp(pubkey.CreationTime.Unix()),
ExpiredUnix: util.TimeStamp(expiry.Unix()),
Emails: emails,
SubsKey: subkeys,
CanSign: pubkey.CanSign(),
Expand Down
6 changes: 4 additions & 2 deletions models/gpg_key_test.go
Expand Up @@ -7,6 +7,8 @@ package models
import (
"testing"

"code.gitea.io/gitea/modules/util"

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -109,7 +111,7 @@ MkM/fdpyc2hY7Dl/+qFmN5MG5yGmMpQcX+RNNR222ibNC1D3wg==
key := &GPGKey{
KeyID: pubkey.KeyIdString(),
Content: content,
Created: pubkey.CreationTime,
CreatedUnix: util.TimeStamp(pubkey.CreationTime.Unix()),
CanSign: pubkey.CanSign(),
CanEncryptComms: pubkey.PubKeyAlgo.CanEncrypt(),
CanEncryptStorage: pubkey.PubKeyAlgo.CanEncrypt(),
Expand All @@ -119,7 +121,7 @@ MkM/fdpyc2hY7Dl/+qFmN5MG5yGmMpQcX+RNNR222ibNC1D3wg==
cannotsignkey := &GPGKey{
KeyID: pubkey.KeyIdString(),
Content: content,
Created: pubkey.CreationTime,
CreatedUnix: util.TimeStamp(pubkey.CreationTime.Unix()),
CanSign: false,
CanEncryptComms: false,
CanEncryptStorage: false,
Expand Down
29 changes: 6 additions & 23 deletions models/issue.go
Expand Up @@ -9,7 +9,6 @@ import (
"path"
"sort"
"strings"
"time"

api "code.gitea.io/sdk/gitea"
"github.com/Unknwon/com"
Expand Down Expand Up @@ -45,31 +44,15 @@ type Issue struct {
NumComments int
Ref string

Deadline time.Time `xorm:"-"`
DeadlineUnix int64 `xorm:"INDEX"`
Created time.Time `xorm:"-"`
CreatedUnix int64 `xorm:"INDEX created"`
Updated time.Time `xorm:"-"`
UpdatedUnix int64 `xorm:"INDEX updated"`
DeadlineUnix util.TimeStamp `xorm:"INDEX"`
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`

Attachments []*Attachment `xorm:"-"`
Comments []*Comment `xorm:"-"`
Reactions ReactionList `xorm:"-"`
}

// BeforeUpdate is invoked from XORM before updating this object.
func (issue *Issue) BeforeUpdate() {
issue.DeadlineUnix = issue.Deadline.Unix()
}

// AfterLoad is invoked from XORM after setting the value of a field of
// this object.
func (issue *Issue) AfterLoad() {
issue.Deadline = time.Unix(issue.DeadlineUnix, 0).Local()
issue.Created = time.Unix(issue.CreatedUnix, 0).Local()
issue.Updated = time.Unix(issue.UpdatedUnix, 0).Local()
}

func (issue *Issue) loadRepo(e Engine) (err error) {
if issue.Repo == nil {
issue.Repo, err = getRepositoryByID(e, issue.RepoID)
Expand Down Expand Up @@ -307,8 +290,8 @@ func (issue *Issue) APIFormat() *api.Issue {
Labels: apiLabels,
State: issue.State(),
Comments: issue.NumComments,
Created: issue.Created,
Updated: issue.Updated,
Created: issue.CreatedUnix.AsTime(),
Updated: issue.UpdatedUnix.AsTime(),
}

if issue.Milestone != nil {
Expand All @@ -322,7 +305,7 @@ func (issue *Issue) APIFormat() *api.Issue {
HasMerged: issue.PullRequest.HasMerged,
}
if issue.PullRequest.HasMerged {
apiIssue.PullRequest.Merged = &issue.PullRequest.Merged
apiIssue.PullRequest.Merged = issue.PullRequest.MergedUnix.AsTimePtr()
}
}

Expand Down
15 changes: 5 additions & 10 deletions models/issue_comment.go
Expand Up @@ -7,7 +7,6 @@ package models
import (
"fmt"
"strings"
"time"

"github.com/Unknwon/com"
"github.com/go-xorm/builder"
Expand All @@ -17,6 +16,7 @@ import (

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/util"
)

// CommentType defines whether a comment is just a simple comment, an action (like close) or a reference.
Expand Down Expand Up @@ -98,10 +98,8 @@ type Comment struct {
Content string `xorm:"TEXT"`
RenderedContent string `xorm:"-"`

Created time.Time `xorm:"-"`
CreatedUnix int64 `xorm:"INDEX created"`
Updated time.Time `xorm:"-"`
UpdatedUnix int64 `xorm:"INDEX updated"`
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`

// Reference issue in commit message
CommitSHA string `xorm:"VARCHAR(40)"`
Expand All @@ -115,9 +113,6 @@ type Comment struct {

// AfterLoad is invoked from XORM after setting the values of all fields of this object.
func (c *Comment) AfterLoad(session *xorm.Session) {
c.Created = time.Unix(c.CreatedUnix, 0).Local()
c.Updated = time.Unix(c.UpdatedUnix, 0).Local()

var err error
c.Attachments, err = getAttachmentsByCommentID(session, c.ID)
if err != nil {
Expand Down Expand Up @@ -191,8 +186,8 @@ func (c *Comment) APIFormat() *api.Comment {
IssueURL: c.IssueURL(),
PRURL: c.PRURL(),
Body: c.Content,
Created: c.Created,
Updated: c.Updated,
Created: c.CreatedUnix.AsTime(),
Updated: c.UpdatedUnix.AsTime(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions models/issue_comment_test.go
Expand Up @@ -33,9 +33,9 @@ func TestCreateComment(t *testing.T) {
assert.EqualValues(t, "Hello", comment.Content)
assert.EqualValues(t, issue.ID, comment.IssueID)
assert.EqualValues(t, doer.ID, comment.PosterID)
AssertInt64InRange(t, now, then, comment.CreatedUnix)
AssertInt64InRange(t, now, then, int64(comment.CreatedUnix))
AssertExistsAndLoadBean(t, comment) // assert actually added to DB

updatedIssue := AssertExistsAndLoadBean(t, &Issue{ID: issue.ID}).(*Issue)
AssertInt64InRange(t, now, then, updatedIssue.UpdatedUnix)
AssertInt64InRange(t, now, then, int64(updatedIssue.UpdatedUnix))
}

0 comments on commit f2e20c8

Please sign in to comment.