Replace map[a]bool with map[a]struct{}

The later uses no memory storage for values and provides the same
functionality.
This commit is contained in:
Tomás Senart 2014-07-02 16:45:17 +02:00 committed by Dave Collins
parent 76d258e2a1
commit cc2c486791
5 changed files with 75 additions and 75 deletions

View file

@ -164,8 +164,8 @@ type blockManager struct {
started int32 started int32
shutdown int32 shutdown int32
blockChain *btcchain.BlockChain blockChain *btcchain.BlockChain
requestedTxns map[btcwire.ShaHash]bool requestedTxns map[btcwire.ShaHash]struct{}
requestedBlocks map[btcwire.ShaHash]bool requestedBlocks map[btcwire.ShaHash]struct{}
receivedLogBlocks int64 receivedLogBlocks int64
receivedLogTx int64 receivedLogTx int64
lastBlockLogTime time.Time lastBlockLogTime time.Time
@ -701,8 +701,8 @@ func (b *blockManager) fetchHeaderBlocks() {
iv := btcwire.NewInvVect(btcwire.InvTypeBlock, node.sha) iv := btcwire.NewInvVect(btcwire.InvTypeBlock, node.sha)
if !b.haveInventory(iv) { if !b.haveInventory(iv) {
b.requestedBlocks[*node.sha] = true b.requestedBlocks[*node.sha] = struct{}{}
b.syncPeer.requestedBlocks[*node.sha] = true b.syncPeer.requestedBlocks[*node.sha] = struct{}{}
gdmsg.AddInvVect(iv) gdmsg.AddInvVect(iv)
numRequested++ numRequested++
} }
@ -954,8 +954,8 @@ func (b *blockManager) handleInvMsg(imsg *invMsg) {
// Request the block if there is not already a pending // Request the block if there is not already a pending
// request. // request.
if _, exists := b.requestedBlocks[iv.Hash]; !exists { if _, exists := b.requestedBlocks[iv.Hash]; !exists {
b.requestedBlocks[iv.Hash] = true b.requestedBlocks[iv.Hash] = struct{}{}
imsg.peer.requestedBlocks[iv.Hash] = true imsg.peer.requestedBlocks[iv.Hash] = struct{}{}
gdmsg.AddInvVect(iv) gdmsg.AddInvVect(iv)
numRequested++ numRequested++
} }
@ -964,8 +964,8 @@ func (b *blockManager) handleInvMsg(imsg *invMsg) {
// Request the transaction if there is not already a // Request the transaction if there is not already a
// pending request. // pending request.
if _, exists := b.requestedTxns[iv.Hash]; !exists { if _, exists := b.requestedTxns[iv.Hash]; !exists {
b.requestedTxns[iv.Hash] = true b.requestedTxns[iv.Hash] = struct{}{}
imsg.peer.requestedTxns[iv.Hash] = true imsg.peer.requestedTxns[iv.Hash] = struct{}{}
gdmsg.AddInvVect(iv) gdmsg.AddInvVect(iv)
numRequested++ numRequested++
} }
@ -1304,8 +1304,8 @@ func newBlockManager(s *server) (*blockManager, error) {
bm := blockManager{ bm := blockManager{
server: s, server: s,
requestedTxns: make(map[btcwire.ShaHash]bool), requestedTxns: make(map[btcwire.ShaHash]struct{}),
requestedBlocks: make(map[btcwire.ShaHash]bool), requestedBlocks: make(map[btcwire.ShaHash]struct{}),
lastBlockLogTime: time.Now(), lastBlockLogTime: time.Now(),
msgChan: make(chan interface{}, cfg.MaxPeers*3), msgChan: make(chan interface{}, cfg.MaxPeers*3),
headerList: list.New(), headerList: list.New(),

View file

@ -233,11 +233,11 @@ func validDbType(dbType string) bool {
// addrs removed. // addrs removed.
func removeDuplicateAddresses(addrs []string) []string { func removeDuplicateAddresses(addrs []string) []string {
result := make([]string, 0, len(addrs)) result := make([]string, 0, len(addrs))
seen := map[string]bool{} seen := map[string]struct{}{}
for _, val := range addrs { for _, val := range addrs {
if _, ok := seen[val]; !ok { if _, ok := seen[val]; !ok {
result = append(result, val) result = append(result, val)
seen[val] = true seen[val] = struct{}{}
} }
} }
return result return result

16
peer.go
View file

@ -146,11 +146,11 @@ type peer struct {
connected int32 connected int32
disconnect int32 // only to be used atomically disconnect int32 // only to be used atomically
persistent bool persistent bool
knownAddresses map[string]bool knownAddresses map[string]struct{}
knownInventory *MruInventoryMap knownInventory *MruInventoryMap
knownInvMutex sync.Mutex knownInvMutex sync.Mutex
requestedTxns map[btcwire.ShaHash]bool // owned by blockmanager requestedTxns map[btcwire.ShaHash]struct{} // owned by blockmanager
requestedBlocks map[btcwire.ShaHash]bool // owned by blockmanager requestedBlocks map[btcwire.ShaHash]struct{} // owned by blockmanager
retryCount int64 retryCount int64
prevGetBlocksBegin *btcwire.ShaHash // owned by blockmanager prevGetBlocksBegin *btcwire.ShaHash // owned by blockmanager
prevGetBlocksStop *btcwire.ShaHash // owned by blockmanager prevGetBlocksStop *btcwire.ShaHash // owned by blockmanager
@ -911,7 +911,7 @@ func (p *peer) pushAddrMsg(addresses []*btcwire.NetAddress) error {
msg := btcwire.NewMsgAddr() msg := btcwire.NewMsgAddr()
for _, na := range addresses { for _, na := range addresses {
// Filter addresses the peer already knows about. // Filter addresses the peer already knows about.
if p.knownAddresses[NetAddressKey(na)] { if _, ok := p.knownAddresses[NetAddressKey(na)]; ok {
continue continue
} }
@ -979,7 +979,7 @@ func (p *peer) handleAddrMsg(msg *btcwire.MsgAddr) {
} }
// Add address to known addresses for this peer. // Add address to known addresses for this peer.
p.knownAddresses[NetAddressKey(na)] = true p.knownAddresses[NetAddressKey(na)] = struct{}{}
} }
// Add addresses to server address manager. The address manager handles // Add addresses to server address manager. The address manager handles
@ -1616,10 +1616,10 @@ func newPeerBase(s *server, inbound bool) *peer {
btcnet: s.netParams.Net, btcnet: s.netParams.Net,
services: btcwire.SFNodeNetwork, services: btcwire.SFNodeNetwork,
inbound: inbound, inbound: inbound,
knownAddresses: make(map[string]bool), knownAddresses: make(map[string]struct{}),
knownInventory: NewMruInventoryMap(maxKnownInventory), knownInventory: NewMruInventoryMap(maxKnownInventory),
requestedTxns: make(map[btcwire.ShaHash]bool), requestedTxns: make(map[btcwire.ShaHash]struct{}),
requestedBlocks: make(map[btcwire.ShaHash]bool), requestedBlocks: make(map[btcwire.ShaHash]struct{}),
requestQueue: list.New(), requestQueue: list.New(),
outputQueue: make(chan outMsg, outputBufferSize), outputQueue: make(chan outMsg, outputBufferSize),
sendQueue: make(chan outMsg, 1), // nonblocking sync sendQueue: make(chan outMsg, 1), // nonblocking sync

View file

@ -124,56 +124,56 @@ func init() {
// list of commands that we recognise, but for which btcd has no support because // list of commands that we recognise, but for which btcd has no support because
// it lacks support for wallet functionality. For these commands the user // it lacks support for wallet functionality. For these commands the user
// should ask a connected instance of btcwallet. // should ask a connected instance of btcwallet.
var rpcAskWallet = map[string]bool{ var rpcAskWallet = map[string]struct{}{
"addmultisigaddress": true, "addmultisigaddress": struct{}{},
"backupwallet": true, "backupwallet": struct{}{},
"createencryptedwallet": true, "createencryptedwallet": struct{}{},
"createmultisig": true, "createmultisig": struct{}{},
"dumpprivkey": true, "dumpprivkey": struct{}{},
"dumpwallet": true, "dumpwallet": struct{}{},
"encryptwallet": true, "encryptwallet": struct{}{},
"getaccount": true, "getaccount": struct{}{},
"getaccountaddress": true, "getaccountaddress": struct{}{},
"getaddressesbyaccount": true, "getaddressesbyaccount": struct{}{},
"getbalance": true, "getbalance": struct{}{},
"getnewaddress": true, "getnewaddress": struct{}{},
"getrawchangeaddress": true, "getrawchangeaddress": struct{}{},
"getreceivedbyaccount": true, "getreceivedbyaccount": struct{}{},
"getreceivedbyaddress": true, "getreceivedbyaddress": struct{}{},
"gettransaction": true, "gettransaction": struct{}{},
"gettxout": true, "gettxout": struct{}{},
"gettxoutsetinfo": true, "gettxoutsetinfo": struct{}{},
"getunconfirmedbalance": true, "getunconfirmedbalance": struct{}{},
"getwalletinfo": true, "getwalletinfo": struct{}{},
"importprivkey": true, "importprivkey": struct{}{},
"importwallet": true, "importwallet": struct{}{},
"keypoolrefill": true, "keypoolrefill": struct{}{},
"listaccounts": true, "listaccounts": struct{}{},
"listaddressgroupings": true, "listaddressgroupings": struct{}{},
"listlockunspent": true, "listlockunspent": struct{}{},
"listreceivedbyaccount": true, "listreceivedbyaccount": struct{}{},
"listreceivedbyaddress": true, "listreceivedbyaddress": struct{}{},
"listsinceblock": true, "listsinceblock": struct{}{},
"listtransactions": true, "listtransactions": struct{}{},
"listunspent": true, "listunspent": struct{}{},
"lockunspent": true, "lockunspent": struct{}{},
"move": true, "move": struct{}{},
"sendfrom": true, "sendfrom": struct{}{},
"sendmany": true, "sendmany": struct{}{},
"sendtoaddress": true, "sendtoaddress": struct{}{},
"setaccount": true, "setaccount": struct{}{},
"settxfee": true, "settxfee": struct{}{},
"signmessage": true, "signmessage": struct{}{},
"signrawtransaction": true, "signrawtransaction": struct{}{},
"validateaddress": true, "validateaddress": struct{}{},
"verifymessage": true, "verifymessage": struct{}{},
"walletlock": true, "walletlock": struct{}{},
"walletpassphrase": true, "walletpassphrase": struct{}{},
"walletpassphrasechange": true, "walletpassphrasechange": struct{}{},
} }
// Commands that are temporarily unimplemented. // Commands that are temporarily unimplemented.
var rpcUnimplemented = map[string]bool{} var rpcUnimplemented = map[string]struct{}{}
// workStateBlockInfo houses information about how to reconstruct a block given // workStateBlockInfo houses information about how to reconstruct a block given
// its template and signature script. // its template and signature script.

View file

@ -58,8 +58,8 @@ var wsHandlers = map[string]wsCommandHandler{
// asynchronously to the main input handler goroutine. This allows long-running // asynchronously to the main input handler goroutine. This allows long-running
// operations to run concurrently (and one at a time) while still responding // operations to run concurrently (and one at a time) while still responding
// to the majority of normal requests which can be answered quickly. // to the majority of normal requests which can be answered quickly.
var wsAsyncHandlers = map[string]bool{ var wsAsyncHandlers = map[string]struct{}{
"rescan": true, "rescan": struct{}{},
} }
// WebsocketHandler handles a new websocket client by creating a new wsClient, // WebsocketHandler handles a new websocket client by creating a new wsClient,
@ -618,7 +618,7 @@ func (m *wsNotificationManager) notifyForTxOuts(ops map[btcwire.OutPoint]map[cha
} }
txHex := "" txHex := ""
wscNotified := make(map[chan bool]bool) wscNotified := make(map[chan bool]struct{})
for i, txOut := range tx.MsgTx().TxOut { for i, txOut := range tx.MsgTx().TxOut {
_, txAddrs, _, err := btcscript.ExtractPkScriptAddrs( _, txAddrs, _, err := btcscript.ExtractPkScriptAddrs(
txOut.PkScript, m.server.server.netParams) txOut.PkScript, m.server.server.netParams)
@ -647,8 +647,8 @@ func (m *wsNotificationManager) notifyForTxOuts(ops map[btcwire.OutPoint]map[cha
for wscQuit, wsc := range cmap { for wscQuit, wsc := range cmap {
m.addSpentRequest(ops, wsc, op) m.addSpentRequest(ops, wsc, op)
if !wscNotified[wscQuit] { if _, ok := wscNotified[wscQuit]; !ok {
wscNotified[wscQuit] = true wscNotified[wscQuit] = struct{}{}
wsc.QueueNotification(marshalledJSON) wsc.QueueNotification(marshalledJSON)
} }
} }
@ -683,7 +683,7 @@ func (m *wsNotificationManager) notifyForTxIns(ops map[btcwire.OutPoint]map[chan
} }
txHex := "" txHex := ""
wscNotified := make(map[chan bool]bool) wscNotified := make(map[chan bool]struct{})
for _, txIn := range tx.MsgTx().TxIn { for _, txIn := range tx.MsgTx().TxIn {
prevOut := &txIn.PreviousOutpoint prevOut := &txIn.PreviousOutpoint
if cmap, ok := ops[*prevOut]; ok { if cmap, ok := ops[*prevOut]; ok {
@ -700,8 +700,8 @@ func (m *wsNotificationManager) notifyForTxIns(ops map[btcwire.OutPoint]map[chan
m.removeSpentRequest(ops, wsc, prevOut) m.removeSpentRequest(ops, wsc, prevOut)
} }
if !wscNotified[wscQuit] { if _, ok := wscNotified[wscQuit]; !ok {
wscNotified[wscQuit] = true wscNotified[wscQuit] = struct{}{}
wsc.QueueNotification(marshalledJSON) wsc.QueueNotification(marshalledJSON)
} }
} }