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

View File

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

16
peer.go
View File

@ -146,11 +146,11 @@ type peer struct {
connected int32
disconnect int32 // only to be used atomically
persistent bool
knownAddresses map[string]bool
knownAddresses map[string]struct{}
knownInventory *MruInventoryMap
knownInvMutex sync.Mutex
requestedTxns map[btcwire.ShaHash]bool // owned by blockmanager
requestedBlocks map[btcwire.ShaHash]bool // owned by blockmanager
requestedTxns map[btcwire.ShaHash]struct{} // owned by blockmanager
requestedBlocks map[btcwire.ShaHash]struct{} // owned by blockmanager
retryCount int64
prevGetBlocksBegin *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()
for _, na := range addresses {
// Filter addresses the peer already knows about.
if p.knownAddresses[NetAddressKey(na)] {
if _, ok := p.knownAddresses[NetAddressKey(na)]; ok {
continue
}
@ -979,7 +979,7 @@ func (p *peer) handleAddrMsg(msg *btcwire.MsgAddr) {
}
// 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
@ -1616,10 +1616,10 @@ func newPeerBase(s *server, inbound bool) *peer {
btcnet: s.netParams.Net,
services: btcwire.SFNodeNetwork,
inbound: inbound,
knownAddresses: make(map[string]bool),
knownAddresses: make(map[string]struct{}),
knownInventory: NewMruInventoryMap(maxKnownInventory),
requestedTxns: make(map[btcwire.ShaHash]bool),
requestedBlocks: make(map[btcwire.ShaHash]bool),
requestedTxns: make(map[btcwire.ShaHash]struct{}),
requestedBlocks: make(map[btcwire.ShaHash]struct{}),
requestQueue: list.New(),
outputQueue: make(chan outMsg, outputBufferSize),
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
// it lacks support for wallet functionality. For these commands the user
// should ask a connected instance of btcwallet.
var rpcAskWallet = map[string]bool{
"addmultisigaddress": true,
"backupwallet": true,
"createencryptedwallet": true,
"createmultisig": true,
"dumpprivkey": true,
"dumpwallet": true,
"encryptwallet": true,
"getaccount": true,
"getaccountaddress": true,
"getaddressesbyaccount": true,
"getbalance": true,
"getnewaddress": true,
"getrawchangeaddress": true,
"getreceivedbyaccount": true,
"getreceivedbyaddress": true,
"gettransaction": true,
"gettxout": true,
"gettxoutsetinfo": true,
"getunconfirmedbalance": true,
"getwalletinfo": true,
"importprivkey": true,
"importwallet": true,
"keypoolrefill": true,
"listaccounts": true,
"listaddressgroupings": true,
"listlockunspent": true,
"listreceivedbyaccount": true,
"listreceivedbyaddress": true,
"listsinceblock": true,
"listtransactions": true,
"listunspent": true,
"lockunspent": true,
"move": true,
"sendfrom": true,
"sendmany": true,
"sendtoaddress": true,
"setaccount": true,
"settxfee": true,
"signmessage": true,
"signrawtransaction": true,
"validateaddress": true,
"verifymessage": true,
"walletlock": true,
"walletpassphrase": true,
"walletpassphrasechange": true,
var rpcAskWallet = map[string]struct{}{
"addmultisigaddress": struct{}{},
"backupwallet": struct{}{},
"createencryptedwallet": struct{}{},
"createmultisig": struct{}{},
"dumpprivkey": struct{}{},
"dumpwallet": struct{}{},
"encryptwallet": struct{}{},
"getaccount": struct{}{},
"getaccountaddress": struct{}{},
"getaddressesbyaccount": struct{}{},
"getbalance": struct{}{},
"getnewaddress": struct{}{},
"getrawchangeaddress": struct{}{},
"getreceivedbyaccount": struct{}{},
"getreceivedbyaddress": struct{}{},
"gettransaction": struct{}{},
"gettxout": struct{}{},
"gettxoutsetinfo": struct{}{},
"getunconfirmedbalance": struct{}{},
"getwalletinfo": struct{}{},
"importprivkey": struct{}{},
"importwallet": struct{}{},
"keypoolrefill": struct{}{},
"listaccounts": struct{}{},
"listaddressgroupings": struct{}{},
"listlockunspent": struct{}{},
"listreceivedbyaccount": struct{}{},
"listreceivedbyaddress": struct{}{},
"listsinceblock": struct{}{},
"listtransactions": struct{}{},
"listunspent": struct{}{},
"lockunspent": struct{}{},
"move": struct{}{},
"sendfrom": struct{}{},
"sendmany": struct{}{},
"sendtoaddress": struct{}{},
"setaccount": struct{}{},
"settxfee": struct{}{},
"signmessage": struct{}{},
"signrawtransaction": struct{}{},
"validateaddress": struct{}{},
"verifymessage": struct{}{},
"walletlock": struct{}{},
"walletpassphrase": struct{}{},
"walletpassphrasechange": struct{}{},
}
// 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
// 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
// operations to run concurrently (and one at a time) while still responding
// to the majority of normal requests which can be answered quickly.
var wsAsyncHandlers = map[string]bool{
"rescan": true,
var wsAsyncHandlers = map[string]struct{}{
"rescan": struct{}{},
}
// 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 := ""
wscNotified := make(map[chan bool]bool)
wscNotified := make(map[chan bool]struct{})
for i, txOut := range tx.MsgTx().TxOut {
_, txAddrs, _, err := btcscript.ExtractPkScriptAddrs(
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 {
m.addSpentRequest(ops, wsc, op)
if !wscNotified[wscQuit] {
wscNotified[wscQuit] = true
if _, ok := wscNotified[wscQuit]; !ok {
wscNotified[wscQuit] = struct{}{}
wsc.QueueNotification(marshalledJSON)
}
}
@ -683,7 +683,7 @@ func (m *wsNotificationManager) notifyForTxIns(ops map[btcwire.OutPoint]map[chan
}
txHex := ""
wscNotified := make(map[chan bool]bool)
wscNotified := make(map[chan bool]struct{})
for _, txIn := range tx.MsgTx().TxIn {
prevOut := &txIn.PreviousOutpoint
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)
}
if !wscNotified[wscQuit] {
wscNotified[wscQuit] = true
if _, ok := wscNotified[wscQuit]; !ok {
wscNotified[wscQuit] = struct{}{}
wsc.QueueNotification(marshalledJSON)
}
}