Skip to content

Commit ed49f19

Browse files
authored
views/explorer: improve address page memory utilization
* Reduce byte size of addressTable template html. Also avoid the unneeded template if and range variables. * explorer: reduce max address rows public/js: set max address rows in address controller * explorer: use strings.Builder instead of bytes.Buffer In (*templates).execTemplateToString, use strings.Builder instead of bytes.Buffer to avoid an unnecessary copy when calling String(). TODO: look into preallocating its buffer. * views: white space scrubbing on address page * views: whitespace scrubbing in micro templates and addressTable Rendered decimalParts, hashElide, and fmtPercentage micro templates should not have newlines and indentation because the indentation is unlikely to be correct for the the parent/containing template. * explorer: debug log the kiB of each rendered address template
1 parent b4594cc commit ed49f19

File tree

6 files changed

+362
-391
lines changed

6 files changed

+362
-391
lines changed

explorer/explorer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const (
5353

5454
// MaxAddressRows is an upper limit on the number of rows that may be shown
5555
// on the address page table.
56-
MaxAddressRows int64 = 1000
56+
MaxAddressRows int64 = 160
5757

5858
testnetNetName = "Testnet"
5959
)

explorer/explorerroutes.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,9 @@ func (exp *explorerUI) AddressPage(w http.ResponseWriter, r *http.Request) {
12891289
return
12901290
}
12911291

1292+
log.Debugf(`"address" template HTML size: %.2f kiB (%s, %v, %d)`,
1293+
float64(len(str))/1024.0, address, txnType, addrData.NumTransactions)
1294+
12921295
w.Header().Set("Content-Type", "text/html")
12931296
w.Header().Set("Turbolinks-Location", r.URL.RequestURI())
12941297
w.WriteHeader(http.StatusOK)
@@ -1333,10 +1336,8 @@ func (exp *explorerUI) AddressTable(w http.ResponseWriter, r *http.Request) {
13331336
return
13341337
}
13351338

1336-
// jsonBytes, err := json.Marshal(response)
1337-
// if err != nil {
1338-
// jsonBytes = []byte("JSON error")
1339-
// }
1339+
log.Debugf(`"addresstable" template HTML size: %.2f kiB (%s, %v, %d)`,
1340+
float64(len(response.HTML))/1024.0, address, txnType, addrData.NumTransactions)
13401341

13411342
w.Header().Set("Content-Type", "application/json")
13421343
enc := json.NewEncoder(w)
@@ -1345,7 +1346,6 @@ func (exp *explorerUI) AddressTable(w http.ResponseWriter, r *http.Request) {
13451346
if err != nil {
13461347
log.Debug(err)
13471348
}
1348-
//w.Write(jsonBytes)
13491349
}
13501350

13511351
// parseAddressParams is used by both /address and /addresstable.

explorer/templates.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package explorer
66

77
import (
8-
"bytes"
98
"encoding/hex"
109
"fmt"
1110
"html/template"
@@ -86,7 +85,7 @@ func (t *templates) execTemplateToString(name string, data interface{}) (string,
8685
return "", fmt.Errorf("Template %s not known", name)
8786
}
8887

89-
var page bytes.Buffer
88+
var page strings.Builder
9089
err := temp.template.ExecuteTemplate(&page, name, data)
9190
return page.String(), err
9291
}

public/js/controllers/address_controller.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import txInBlock from '../helpers/block_helper'
1212
import { fadeIn, animationFrame } from '../helpers/animation_helper'
1313

1414
const blockDuration = 5 * 60000
15+
const maxAddrRows = 160
1516
let Dygraph // lazy loaded on connect
1617

1718
function txTypesFunc (d, binSize) {
@@ -342,10 +343,10 @@ export default class extends Controller {
342343
if (option.value > 100) {
343344
if (rowMax > 100) {
344345
option.disabled = false
345-
option.text = option.value = Math.min(rowMax, 1000)
346+
option.text = option.value = Math.min(rowMax, maxAddrRows)
346347
} else {
347348
option.disabled = true
348-
option.text = option.value = 1000
349+
option.text = option.value = maxAddrRows
349350
}
350351
} else {
351352
option.disabled = rowMax <= option.value

views/address.tmpl

+48-49
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
{{template "html-head" printf "Decred Address %s" .Data.Address}}
66
{{template "navbar" . }}
7-
{{with .Data}}
8-
{{$TxnCount := add .TxnCount .NumUnconfirmed}}
9-
{{$txType := .TxnType}}
7+
{{- with .Data}}
8+
{{- $TxnCount := add .TxnCount .NumUnconfirmed}}
9+
{{- $txType := .TxnType -}}
1010
<div class="container main"
1111
data-controller="address newblock"
1212
data-address-offset="{{.Offset}}"
@@ -16,11 +16,11 @@
1616
>
1717
<div class="row pb-4 px-2">
1818
<div class="col-24 col-xl-10 bg-white px-3 py-3 position-relative">
19-
{{if eq .Address $.DevAddress}}
19+
{{- if eq .Address $.DevAddress}}
2020
<div class="fs22 pb-3">Decred Treasury</div>
21-
{{else}}
21+
{{- else}}
2222
<div class="fs22 pb-3">Address</div>
23-
{{end}}
23+
{{- end}}
2424
<div class="text-left d-flex align-items-start flex-wrap">
2525
<div class="fs15 font-weight-bold break-word d-inline-block hash-box mb-3" data-target="address.addr">{{.Address}}</div>
2626
<a
@@ -40,63 +40,62 @@
4040
<span class="text-secondary fs13">Balance</span>
4141
<br>
4242
<span class="lh1rem d-inline-block pt-1 fs18 fs14-decimal font-weight-bold">
43-
{{if .Balance}}
43+
{{- if .Balance}}
4444
{{template "decimalParts" (amountAsDecimalParts .Balance.TotalUnspent true)}}<span class="text-secondary fs14">DCR</span>
45-
{{else}}
45+
{{- else}}
4646
<span class="fs18">0</span> <span class="text-secondary fs14">DCR</span>
47-
{{end}}
47+
{{- end}}
4848
</span>
4949
<br>
50-
{{if $.FiatBalance}}
50+
{{- if $.FiatBalance}}
5151
<span class="text-secondary fs16 lh1rem">{{threeSigFigs $.FiatBalance.Value}} <span class="fs14">{{$.FiatBalance.Index}}</span></span>
52-
{{end}}
52+
{{- end}}
5353
</div>
5454
<div class="d-inline-block text-left pr-2 pb-3">
5555
<span class="text-secondary fs13">Received</span>
56-
<br>
56+
<br>
5757
<span class="lh1rem d-inline-block pt-1 fs18 fs14-decimal font-weight-bold">
58-
{{if .Balance}}
59-
{{$received := add .Balance.TotalSpent .Balance.TotalUnspent}}
60-
{{template "decimalParts" (amountAsDecimalParts $received true)}} <span class="text-secondary fs14">DCR</span>
61-
{{else}}
58+
{{- if .Balance}}
59+
{{- $received := add .Balance.TotalSpent .Balance.TotalUnspent}}
60+
{{- template "decimalParts" (amountAsDecimalParts $received true)}} <span class="text-secondary fs14">DCR</span>
61+
{{- else}}
6262
<span class="fs18">0</span> <span class="text-secondary fs14">DCR</span>
63-
{{end}}
63+
{{- end}}
6464
</span>
6565
<br>
6666
<span class="text-secondary fs16 lh1rem">{{intComma (add .Balance.NumSpent .Balance.NumUnspent)}} outputs</span>
67-
</span>
6867
</div>
6968
<div class="d-inline-block text-left pr-2 pb-3">
7069
<span class="text-secondary fs13">Spent</span>
7170
<br>
7271
<span class="lh1rem d-inline-block pt-1 fs18 fs14-decimal font-weight-bold">
73-
{{if .Balance}}
74-
{{template "decimalParts" (amountAsDecimalParts .Balance.TotalSpent true)}} <span class="text-secondary fs14">DCR</span>
75-
{{else}}
72+
{{- if .Balance}}
73+
{{- template "decimalParts" (amountAsDecimalParts .Balance.TotalSpent true)}} <span class="text-secondary fs14">DCR</span>
74+
{{- else}}
7675
<span class="fs18">0</span> <span class="text-secondary fs14">DCR</span>
77-
{{end}}
76+
{{- end}}
7877
</span>
7978
<br>
8079
<span class="text-secondary fs16 lh1rem">{{intComma .Balance.NumSpent}} inputs</span>
8180
</div>
8281
<span></span>
8382
</div>
8483
<div class="row pb-2">
85-
{{if ne .NumUnconfirmed 0}}
84+
{{- if ne .NumUnconfirmed 0}}
8685
<div class="col-12 pb-2 fs14 text-secondary text-left" data-target="address.numUnconfirmed">
8786
<span class="font-weight-bold">Unconfirmed</span>: <span class="addr-unconfirmed-count">{{.NumUnconfirmed}}</span>
8887
</div>
89-
{{end}}
90-
{{if .Balance.HasStakeOutputs}}
88+
{{- end}}
89+
{{- if .Balance.HasStakeOutputs}}
9190
<div class="col-12 pb-2 fs14 text-secondary text-left">
9291
<span class="font-weight-bold">Stake spending</span>: {{printf "%.1f" (x100 .Balance.FromStake)}}%
9392
</div>
94-
{{end}}
95-
{{if .Balance.HasStakeInputs}}
93+
{{- end}}
94+
{{- if .Balance.HasStakeInputs}}
9695
<div class="col-12 pb-2 fs14 text-secondary text-left">
9796
<span class="font-weight-bold">Stake income</span>: {{printf "%.1f" (x100 .Balance.ToStake)}}%
9897
</div>
99-
{{end}}
98+
{{- end}}
10099
</div>
101100
<div class="row pb-3 fs16">
102101
<span class="col-24"><a href="{{$.Links.DownloadLink}}" title="Decred downloads" target="_blank" rel="noopener noreferrer">Get Decrediton</a>, the official desktop wallet.</span>
@@ -226,9 +225,10 @@
226225
<div
227226
class="d-flex align-items-center justify-content-between"
228227
>
229-
{{if gt $TxnCount 0}}<a class="d-inline-block p-2 rounded download text-nowrap" href="/download/address/io/{{.Address}}{{if $.CRLFDownload}}?cr=true{{end}}" type="text/csv" download><span class="dcricon-download mx-1"></span> Download CSV</a>{{end}}
230-
<!-- This dummy span ensures left/right alignment of the buttons, even if one is hidden -->
231-
<span></span>
228+
{{- if gt $TxnCount 0}}
229+
<a class="d-inline-block p-2 rounded download text-nowrap" href="/download/address/io/{{.Address}}{{if $.CRLFDownload}}?cr=true{{end}}" type="text/csv" download><span class="dcricon-download mx-1"></span> Download CSV</a>
230+
{{- end}}
231+
<span></span>{{/*This dummy span ensures left/right alignment of the buttons, even if one is hidden.*/}}
232232
<div class="d-inline-block text-right">
233233
<label class="mb-0 mr-1" for="txntype">Type</label>
234234
<select
@@ -246,34 +246,33 @@
246246
</select>
247247
</div>
248248
</div>
249-
<div
250-
class="hidden d-flex align-items-center justify-content-end"
251-
>
249+
<div class="hidden d-flex align-items-center justify-content-end">
252250
<label class="my-2 mr-1" for="pagesize">Page size</label>
253251
<select
254252
name="pagesize"
255253
id="pagesize"
256254
data-target="address.pagesize"
257255
data-action="change->address#changePageSize"
258256
class="form-control-sm mb-2 mr-sm-2 mb-sm-0 {{if lt $TxnCount 20}}disabled{{end}}"
259-
{{if lt $TxnCount 20}}disabled{{end}}
257+
{{- if lt $TxnCount 20}} disabled{{end}}
260258
>
261-
{{$Txlen := len .Transactions}}
262-
<option {{if eq $Txlen 20}}selected{{end}} value="20"{{if lt $TxnCount 20}} disabled{{end}}>20</option>
263-
<option {{if eq $Txlen 100}}selected{{end}} value="100"{{if lt $TxnCount 100}} disabled{{end}}>100</option>
264-
{{if lt $TxnCount 1000}}
265-
<option{{if eq $Txlen $TxnCount}} selected{{end}} value="{{$TxnCount}}"{{if le $TxnCount 100}} disabled{{end}}>{{$TxnCount}}</option>
266-
{{else}}
267-
<option{{if eq $Txlen 1000}} selected{{end}} value="1000">1000</option>
268-
{{end}}
259+
{{- $Txlen := len .Transactions}}
260+
<option {{if eq $Txlen 20}}selected {{end}}value="20"{{if lt $TxnCount 20}} disabled{{end}}>20</option>
261+
<option {{if eq $Txlen 40}}selected {{end}}value="40"{{if lt $TxnCount 40}} disabled{{end}}>40</option>
262+
<option {{if eq $Txlen 80}}selected {{end}}value="80"{{if lt $TxnCount 80}} disabled{{end}}>80</option>
263+
{{- if lt $TxnCount 160}}
264+
<option {{if eq $Txlen $TxnCount}}selected {{end}}value="{{$TxnCount}}"{{if le $TxnCount 160}} disabled{{end}}>{{$TxnCount}}</option>
265+
{{- else}}
266+
<option {{if ge $Txlen 160}}selected {{end}}value="160">160</option>
267+
{{- end}}
268+
</select>
269269
</select>
270270
</div>
271271
</div>
272-
{{end}} {{/* if not .IsDummyAddress */}}
273-
</div> <!-- container main -->
274-
{{end}} {{/* with .Data */}}
275-
{{ template "footer" . }}
276-
272+
{{- end}}{{/* if not .IsDummyAddress */}}
273+
</div>{{/* container main */}}
274+
{{- end}} {{/* with .Data */}}
275+
{{template "footer" . }}
277276
</body>
278277
</html>
279-
{{end}}
278+
{{- end}}

0 commit comments

Comments
 (0)