Skip to content

Commit f2e20c8

Browse files
lunnylafriks
authored andcommitted
Refactor struct's time to remove unnecessary memory usage (#3142)
* 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
1 parent c082c3b commit f2e20c8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+334
-479
lines changed

models/action.go

+5-10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"code.gitea.io/gitea/modules/base"
1919
"code.gitea.io/gitea/modules/log"
2020
"code.gitea.io/gitea/modules/setting"
21+
"code.gitea.io/gitea/modules/util"
2122
api "code.gitea.io/sdk/gitea"
2223

2324
"github.com/Unknwon/com"
@@ -85,15 +86,9 @@ type Action struct {
8586
Comment *Comment `xorm:"-"`
8687
IsDeleted bool `xorm:"INDEX NOT NULL DEFAULT false"`
8788
RefName string
88-
IsPrivate bool `xorm:"INDEX NOT NULL DEFAULT false"`
89-
Content string `xorm:"TEXT"`
90-
Created time.Time `xorm:"-"`
91-
CreatedUnix int64 `xorm:"INDEX created"`
92-
}
93-
94-
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
95-
func (a *Action) AfterLoad() {
96-
a.Created = time.Unix(a.CreatedUnix, 0).Local()
89+
IsPrivate bool `xorm:"INDEX NOT NULL DEFAULT false"`
90+
Content string `xorm:"TEXT"`
91+
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
9792
}
9893

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

230225
// GetCreate returns the action creation time.
231226
func (a *Action) GetCreate() time.Time {
232-
return a.Created
227+
return a.CreatedUnix.AsTime()
233228
}
234229

235230
// GetIssueInfos returns a list of issues associated with

models/admin.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package models
66

77
import (
88
"fmt"
9-
"time"
109

1110
"code.gitea.io/gitea/modules/log"
1211
"code.gitea.io/gitea/modules/util"
@@ -26,14 +25,8 @@ const (
2625
type Notice struct {
2726
ID int64 `xorm:"pk autoincr"`
2827
Type NoticeType
29-
Description string `xorm:"TEXT"`
30-
Created time.Time `xorm:"-"`
31-
CreatedUnix int64 `xorm:"INDEX created"`
32-
}
33-
34-
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
35-
func (n *Notice) AfterLoad() {
36-
n.Created = time.Unix(n.CreatedUnix, 0).Local()
28+
Description string `xorm:"TEXT"`
29+
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
3730
}
3831

3932
// TrStr returns a translation format string.

models/attachment.go

+3-10
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010
"mime/multipart"
1111
"os"
1212
"path"
13-
"time"
1413

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

1716
"code.gitea.io/gitea/modules/setting"
17+
"code.gitea.io/gitea/modules/util"
1818
)
1919

2020
// Attachment represent a attachment of issue/comment/release.
@@ -25,15 +25,8 @@ type Attachment struct {
2525
ReleaseID int64 `xorm:"INDEX"`
2626
CommentID int64
2727
Name string
28-
DownloadCount int64 `xorm:"DEFAULT 0"`
29-
Created time.Time `xorm:"-"`
30-
CreatedUnix int64 `xorm:"created"`
31-
}
32-
33-
// AfterLoad is invoked from XORM after setting the value of a field of
34-
// this object.
35-
func (a *Attachment) AfterLoad() {
36-
a.Created = time.Unix(a.CreatedUnix, 0).Local()
28+
DownloadCount int64 `xorm:"DEFAULT 0"`
29+
CreatedUnix util.TimeStamp `xorm:"created"`
3730
}
3831

3932
// IncreaseDownloadCount is update download count + 1

models/branches.go

+11-19
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@ type ProtectedBranch struct {
2929
BranchName string `xorm:"UNIQUE(s)"`
3030
CanPush bool `xorm:"NOT NULL DEFAULT false"`
3131
EnableWhitelist bool
32-
WhitelistUserIDs []int64 `xorm:"JSON TEXT"`
33-
WhitelistTeamIDs []int64 `xorm:"JSON TEXT"`
34-
Created time.Time `xorm:"-"`
35-
CreatedUnix int64 `xorm:"created"`
36-
Updated time.Time `xorm:"-"`
37-
UpdatedUnix int64 `xorm:"updated"`
32+
WhitelistUserIDs []int64 `xorm:"JSON TEXT"`
33+
WhitelistTeamIDs []int64 `xorm:"JSON TEXT"`
34+
CreatedUnix util.TimeStamp `xorm:"created"`
35+
UpdatedUnix util.TimeStamp `xorm:"updated"`
3836
}
3937

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

198196
// DeletedBranch struct
199197
type DeletedBranch struct {
200-
ID int64 `xorm:"pk autoincr"`
201-
RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
202-
Name string `xorm:"UNIQUE(s) NOT NULL"`
203-
Commit string `xorm:"UNIQUE(s) NOT NULL"`
204-
DeletedByID int64 `xorm:"INDEX"`
205-
DeletedBy *User `xorm:"-"`
206-
Deleted time.Time `xorm:"-"`
207-
DeletedUnix int64 `xorm:"INDEX created"`
208-
}
209-
210-
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
211-
func (deletedBranch *DeletedBranch) AfterLoad() {
212-
deletedBranch.Deleted = time.Unix(deletedBranch.DeletedUnix, 0).Local()
198+
ID int64 `xorm:"pk autoincr"`
199+
RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
200+
Name string `xorm:"UNIQUE(s) NOT NULL"`
201+
Commit string `xorm:"UNIQUE(s) NOT NULL"`
202+
DeletedByID int64 `xorm:"INDEX"`
203+
DeletedBy *User `xorm:"-"`
204+
DeletedUnix util.TimeStamp `xorm:"INDEX created"`
213205
}
214206

215207
// AddDeletedBranch adds a deleted branch to the database

models/gpg_key.go

+14-22
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
"code.gitea.io/git"
1919
"code.gitea.io/gitea/modules/log"
20+
"code.gitea.io/gitea/modules/util"
2021

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

2728
// GPGKey represents a GPG key.
2829
type GPGKey struct {
29-
ID int64 `xorm:"pk autoincr"`
30-
OwnerID int64 `xorm:"INDEX NOT NULL"`
31-
KeyID string `xorm:"INDEX CHAR(16) NOT NULL"`
32-
PrimaryKeyID string `xorm:"CHAR(16)"`
33-
Content string `xorm:"TEXT NOT NULL"`
34-
Created time.Time `xorm:"-"`
35-
CreatedUnix int64
36-
Expired time.Time `xorm:"-"`
37-
ExpiredUnix int64
38-
Added time.Time `xorm:"-"`
39-
AddedUnix int64
30+
ID int64 `xorm:"pk autoincr"`
31+
OwnerID int64 `xorm:"INDEX NOT NULL"`
32+
KeyID string `xorm:"INDEX CHAR(16) NOT NULL"`
33+
PrimaryKeyID string `xorm:"CHAR(16)"`
34+
Content string `xorm:"TEXT NOT NULL"`
35+
CreatedUnix util.TimeStamp `xorm:"created"`
36+
ExpiredUnix util.TimeStamp
37+
AddedUnix util.TimeStamp
4038
SubsKey []*GPGKey `xorm:"-"`
4139
Emails []*EmailAddress
4240
CanSign bool
@@ -47,17 +45,11 @@ type GPGKey struct {
4745

4846
// BeforeInsert will be invoked by XORM before inserting a record
4947
func (key *GPGKey) BeforeInsert() {
50-
key.AddedUnix = time.Now().Unix()
51-
key.ExpiredUnix = key.Expired.Unix()
52-
key.CreatedUnix = key.Created.Unix()
48+
key.AddedUnix = util.TimeStampNow()
5349
}
5450

5551
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
5652
func (key *GPGKey) AfterLoad(session *xorm.Session) {
57-
key.Added = time.Unix(key.AddedUnix, 0).Local()
58-
key.Expired = time.Unix(key.ExpiredUnix, 0).Local()
59-
key.Created = time.Unix(key.CreatedUnix, 0).Local()
60-
6153
err := session.Where("primary_key_id=?", key.KeyID).Find(&key.SubsKey)
6254
if err != nil {
6355
log.Error(3, "Find Sub GPGkeys[%d]: %v", key.KeyID, err)
@@ -163,8 +155,8 @@ func parseSubGPGKey(ownerID int64, primaryID string, pubkey *packet.PublicKey, e
163155
KeyID: pubkey.KeyIdString(),
164156
PrimaryKeyID: primaryID,
165157
Content: content,
166-
Created: pubkey.CreationTime,
167-
Expired: expiry,
158+
CreatedUnix: util.TimeStamp(pubkey.CreationTime.Unix()),
159+
ExpiredUnix: util.TimeStamp(expiry.Unix()),
168160
CanSign: pubkey.CanSign(),
169161
CanEncryptComms: pubkey.PubKeyAlgo.CanEncrypt(),
170162
CanEncryptStorage: pubkey.PubKeyAlgo.CanEncrypt(),
@@ -236,8 +228,8 @@ func parseGPGKey(ownerID int64, e *openpgp.Entity) (*GPGKey, error) {
236228
KeyID: pubkey.KeyIdString(),
237229
PrimaryKeyID: "",
238230
Content: content,
239-
Created: pubkey.CreationTime,
240-
Expired: expiry,
231+
CreatedUnix: util.TimeStamp(pubkey.CreationTime.Unix()),
232+
ExpiredUnix: util.TimeStamp(expiry.Unix()),
241233
Emails: emails,
242234
SubsKey: subkeys,
243235
CanSign: pubkey.CanSign(),

models/gpg_key_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package models
77
import (
88
"testing"
99

10+
"code.gitea.io/gitea/modules/util"
11+
1012
"github.com/stretchr/testify/assert"
1113
)
1214

@@ -109,7 +111,7 @@ MkM/fdpyc2hY7Dl/+qFmN5MG5yGmMpQcX+RNNR222ibNC1D3wg==
109111
key := &GPGKey{
110112
KeyID: pubkey.KeyIdString(),
111113
Content: content,
112-
Created: pubkey.CreationTime,
114+
CreatedUnix: util.TimeStamp(pubkey.CreationTime.Unix()),
113115
CanSign: pubkey.CanSign(),
114116
CanEncryptComms: pubkey.PubKeyAlgo.CanEncrypt(),
115117
CanEncryptStorage: pubkey.PubKeyAlgo.CanEncrypt(),
@@ -119,7 +121,7 @@ MkM/fdpyc2hY7Dl/+qFmN5MG5yGmMpQcX+RNNR222ibNC1D3wg==
119121
cannotsignkey := &GPGKey{
120122
KeyID: pubkey.KeyIdString(),
121123
Content: content,
122-
Created: pubkey.CreationTime,
124+
CreatedUnix: util.TimeStamp(pubkey.CreationTime.Unix()),
123125
CanSign: false,
124126
CanEncryptComms: false,
125127
CanEncryptStorage: false,

models/issue.go

+6-23
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"path"
1010
"sort"
1111
"strings"
12-
"time"
1312

1413
api "code.gitea.io/sdk/gitea"
1514
"github.com/Unknwon/com"
@@ -45,31 +44,15 @@ type Issue struct {
4544
NumComments int
4645
Ref string
4746

48-
Deadline time.Time `xorm:"-"`
49-
DeadlineUnix int64 `xorm:"INDEX"`
50-
Created time.Time `xorm:"-"`
51-
CreatedUnix int64 `xorm:"INDEX created"`
52-
Updated time.Time `xorm:"-"`
53-
UpdatedUnix int64 `xorm:"INDEX updated"`
47+
DeadlineUnix util.TimeStamp `xorm:"INDEX"`
48+
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
49+
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
5450

5551
Attachments []*Attachment `xorm:"-"`
5652
Comments []*Comment `xorm:"-"`
5753
Reactions ReactionList `xorm:"-"`
5854
}
5955

60-
// BeforeUpdate is invoked from XORM before updating this object.
61-
func (issue *Issue) BeforeUpdate() {
62-
issue.DeadlineUnix = issue.Deadline.Unix()
63-
}
64-
65-
// AfterLoad is invoked from XORM after setting the value of a field of
66-
// this object.
67-
func (issue *Issue) AfterLoad() {
68-
issue.Deadline = time.Unix(issue.DeadlineUnix, 0).Local()
69-
issue.Created = time.Unix(issue.CreatedUnix, 0).Local()
70-
issue.Updated = time.Unix(issue.UpdatedUnix, 0).Local()
71-
}
72-
7356
func (issue *Issue) loadRepo(e Engine) (err error) {
7457
if issue.Repo == nil {
7558
issue.Repo, err = getRepositoryByID(e, issue.RepoID)
@@ -307,8 +290,8 @@ func (issue *Issue) APIFormat() *api.Issue {
307290
Labels: apiLabels,
308291
State: issue.State(),
309292
Comments: issue.NumComments,
310-
Created: issue.Created,
311-
Updated: issue.Updated,
293+
Created: issue.CreatedUnix.AsTime(),
294+
Updated: issue.UpdatedUnix.AsTime(),
312295
}
313296

314297
if issue.Milestone != nil {
@@ -322,7 +305,7 @@ func (issue *Issue) APIFormat() *api.Issue {
322305
HasMerged: issue.PullRequest.HasMerged,
323306
}
324307
if issue.PullRequest.HasMerged {
325-
apiIssue.PullRequest.Merged = &issue.PullRequest.Merged
308+
apiIssue.PullRequest.Merged = issue.PullRequest.MergedUnix.AsTimePtr()
326309
}
327310
}
328311

models/issue_comment.go

+5-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package models
77
import (
88
"fmt"
99
"strings"
10-
"time"
1110

1211
"github.com/Unknwon/com"
1312
"github.com/go-xorm/builder"
@@ -17,6 +16,7 @@ import (
1716

1817
"code.gitea.io/gitea/modules/log"
1918
"code.gitea.io/gitea/modules/markup"
19+
"code.gitea.io/gitea/modules/util"
2020
)
2121

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

101-
Created time.Time `xorm:"-"`
102-
CreatedUnix int64 `xorm:"INDEX created"`
103-
Updated time.Time `xorm:"-"`
104-
UpdatedUnix int64 `xorm:"INDEX updated"`
101+
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
102+
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
105103

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

116114
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
117115
func (c *Comment) AfterLoad(session *xorm.Session) {
118-
c.Created = time.Unix(c.CreatedUnix, 0).Local()
119-
c.Updated = time.Unix(c.UpdatedUnix, 0).Local()
120-
121116
var err error
122117
c.Attachments, err = getAttachmentsByCommentID(session, c.ID)
123118
if err != nil {
@@ -191,8 +186,8 @@ func (c *Comment) APIFormat() *api.Comment {
191186
IssueURL: c.IssueURL(),
192187
PRURL: c.PRURL(),
193188
Body: c.Content,
194-
Created: c.Created,
195-
Updated: c.Updated,
189+
Created: c.CreatedUnix.AsTime(),
190+
Updated: c.UpdatedUnix.AsTime(),
196191
}
197192
}
198193

models/issue_comment_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ func TestCreateComment(t *testing.T) {
3333
assert.EqualValues(t, "Hello", comment.Content)
3434
assert.EqualValues(t, issue.ID, comment.IssueID)
3535
assert.EqualValues(t, doer.ID, comment.PosterID)
36-
AssertInt64InRange(t, now, then, comment.CreatedUnix)
36+
AssertInt64InRange(t, now, then, int64(comment.CreatedUnix))
3737
AssertExistsAndLoadBean(t, comment) // assert actually added to DB
3838

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

0 commit comments

Comments
 (0)