@@ -16,6 +16,12 @@ import (
1616 "github.com/pkg/errors"
1717)
1818
19+ var (
20+ accountsPool = newPool [models.Account ]()
21+ vehicleSnapshotsPool = newPool [models.VehicleSnapshot ]()
22+ accountSnapshotsPool = newPool [models.AccountSnapshot ]()
23+ )
24+
1925type AccountsWithReference map [string ]string
2026
2127func WithReference (accountID string , referenceID string ) AccountsWithReference {
@@ -143,10 +149,10 @@ func RecordAccountSnapshots(ctx context.Context, wgClient wargaming.Client, dbCl
143149 group .Wait ()
144150
145151 var accountErrors = make (map [string ]error )
146- var accountUpdates = make (map [string ]models.Account )
152+ var accountUpdates = make (map [string ]* models.Account )
147153
148- var accountSnapshots []models.AccountSnapshot
149- var vehicleSnapshots = make (map [string ][]models.VehicleSnapshot )
154+ var accountSnapshots []* models.AccountSnapshot
155+ var vehicleSnapshots = make (map [string ][]* models.VehicleSnapshot )
150156
151157 for _ , accountID := range accountsNeedAnUpdate {
152158 vehicles := accountVehicles [accountID ]
@@ -162,16 +168,25 @@ func RecordAccountSnapshots(ctx context.Context, wgClient wargaming.Client, dbCl
162168
163169 snapshotStats := fetch .WargamingToStats (realm , accounts [accountID ], clans [accountID ], vehicles .Data )
164170 { // account snapshot
165- accountSnapshots = append (accountSnapshots , models.AccountSnapshot {
171+ sht := accountSnapshotsPool .Get ()
172+ defer accountSnapshotsPool .Put (sht )
173+
174+ * sht = models.AccountSnapshot {
166175 Type : models .SnapshotTypeDaily ,
167176 CreatedAt : createdAt ,
168177 ReferenceID : accountRefID ,
169178 AccountID : snapshotStats .Account .ID ,
170179 LastBattleTime : snapshotStats .LastBattleTime ,
171180 RatingBattles : snapshotStats .RatingBattles .StatsFrame ,
172181 RegularBattles : snapshotStats .RegularBattles .StatsFrame ,
173- })
174- accountUpdates [accountID ] = snapshotStats .Account
182+ }
183+ accountSnapshots = append (accountSnapshots , sht )
184+
185+ asht := accountsPool .Get ()
186+ defer accountsPool .Put (asht )
187+
188+ * asht = snapshotStats .Account
189+ accountUpdates [accountID ] = asht
175190 }
176191
177192 // vehicle snapshots
@@ -180,15 +195,20 @@ func RecordAccountSnapshots(ctx context.Context, wgClient wargaming.Client, dbCl
180195 if len (vehicles .Data ) > 0 {
181196 for id , vehicle := range vehicleStats {
182197 vehicleLastBattleTimes [id ] = vehicleBattleData {vehicle .LastBattleTime , int (vehicle .Battles .Float ())}
183- vehicleSnapshots [accountID ] = append (vehicleSnapshots [accountID ], models.VehicleSnapshot {
198+
199+ sht := vehicleSnapshotsPool .Get ()
200+ defer vehicleSnapshotsPool .Put (sht )
201+
202+ * sht = models.VehicleSnapshot {
184203 Type : models .SnapshotTypeDaily ,
185204 LastBattleTime : vehicle .LastBattleTime ,
186205 Stats : * vehicle .StatsFrame ,
187206 VehicleID : vehicle .VehicleID ,
188207 ReferenceID : accountRefID ,
189208 AccountID : accountID ,
190209 CreatedAt : createdAt ,
191- })
210+ }
211+ vehicleSnapshots [accountID ] = append (vehicleSnapshots [accountID ], sht )
192212 }
193213 }
194214 }
@@ -212,7 +232,7 @@ func RecordAccountSnapshots(ctx context.Context, wgClient wargaming.Client, dbCl
212232 }
213233
214234 // update account cache, non critical and should not fail the flow
215- _ , err = dbClient .UpsertAccounts (ctx , []models. Account { accountUpdates [accountSnapshot .AccountID ]} )
235+ _ , err = dbClient .UpsertAccounts (ctx , accountUpdates [accountSnapshot .AccountID ])
216236 if err != nil {
217237 log .Err (err ).Str ("accountId" , accountSnapshot .AccountID ).Msg ("failed to upsert account" )
218238 }
0 commit comments