This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from itzmeanjan/develop
Performance Improvement
- Loading branch information
Showing
11 changed files
with
348 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package data | ||
|
||
// MemPoolTxsAsc - List of mempool tx(s) | ||
// | ||
// @note This structure to be used for sorting tx(s) | ||
// in ascending way, using gas price they're paying | ||
type MemPoolTxsAsc []*MemPoolTx | ||
|
||
// len - Number of tx(s) present in this slice | ||
func (m MemPoolTxsAsc) len() int { | ||
return len(m) | ||
} | ||
|
||
// cap - Number of elements can be kept in slice | ||
// without further memory allocation | ||
func (m MemPoolTxsAsc) cap() int { | ||
return cap(m) | ||
} | ||
|
||
// get - Return slice of txs | ||
func (m MemPoolTxsAsc) get() []*MemPoolTx { | ||
return m | ||
} | ||
|
||
// findInsertionPoint - Find index at which newly arrived tx should be entered to | ||
// keep this slice sorted | ||
func (m MemPoolTxsAsc) findInsertionPoint(low int, high int, tx *MemPoolTx) int { | ||
|
||
if low > high { | ||
return 0 | ||
} | ||
|
||
if low == high { | ||
|
||
if BigHexToBigDecimal(m[low].GasPrice).Cmp(BigHexToBigDecimal(tx.GasPrice)) > 0 { | ||
return low | ||
} | ||
|
||
return low + 1 | ||
|
||
} | ||
|
||
mid := (low + high) / 2 | ||
if BigHexToBigDecimal(m[mid].GasPrice).Cmp(BigHexToBigDecimal(tx.GasPrice)) > 0 { | ||
|
||
return m.findInsertionPoint(low, mid, tx) | ||
|
||
} | ||
|
||
return m.findInsertionPoint(mid+1, high, tx) | ||
|
||
} | ||
|
||
// findTx - Find index of tx, which is already present in this sorted slice | ||
func (m MemPoolTxsAsc) findTx(low int, high int, tx *MemPoolTx) int { | ||
|
||
if low > high { | ||
return -1 | ||
} | ||
|
||
if low == high { | ||
|
||
idx := findTxFromSlice(m[low:], tx) | ||
if idx == -1 { | ||
return -1 | ||
} | ||
|
||
return low + idx | ||
|
||
} | ||
|
||
mid := (low + high) / 2 | ||
if BigHexToBigDecimal(m[mid].GasPrice).Cmp(BigHexToBigDecimal(tx.GasPrice)) >= 0 { | ||
return m.findTx(low, mid, tx) | ||
} | ||
|
||
return m.findTx(mid+1, high, tx) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package data | ||
|
||
// MemPoolTxsDesc - List of mempool tx(s) | ||
// | ||
// @note This structure to be used for sorting tx(s) | ||
// in descending way, using gas price they're paying | ||
type MemPoolTxsDesc []*MemPoolTx | ||
|
||
// len - Number of txs present in slice | ||
func (m MemPoolTxsDesc) len() int { | ||
return len(m) | ||
} | ||
|
||
// cap - Number of elements can be kept in slice | ||
// without further memory allocation | ||
func (m MemPoolTxsDesc) cap() int { | ||
return cap(m) | ||
} | ||
|
||
// get - Return slice of txs | ||
func (m MemPoolTxsDesc) get() []*MemPoolTx { | ||
return m | ||
} | ||
|
||
// findInsertionPoint - Find index at which newly arrived tx should be entered to | ||
// keep this slice sorted | ||
func (m MemPoolTxsDesc) findInsertionPoint(low int, high int, tx *MemPoolTx) int { | ||
|
||
if low > high { | ||
return 0 | ||
} | ||
|
||
if low == high { | ||
|
||
if !(BigHexToBigDecimal(m[low].GasPrice).Cmp(BigHexToBigDecimal(tx.GasPrice)) > 0) { | ||
return low | ||
} | ||
|
||
return low + 1 | ||
|
||
} | ||
|
||
mid := (low + high) / 2 | ||
if !(BigHexToBigDecimal(m[mid].GasPrice).Cmp(BigHexToBigDecimal(tx.GasPrice)) > 0) { | ||
|
||
return m.findInsertionPoint(low, mid, tx) | ||
|
||
} | ||
|
||
return m.findInsertionPoint(mid+1, high, tx) | ||
|
||
} | ||
|
||
// findTx - Find index of tx, which is already present in this sorted slice | ||
func (m MemPoolTxsDesc) findTx(low int, high int, tx *MemPoolTx) int { | ||
|
||
if low > high { | ||
return -1 | ||
} | ||
|
||
if low == high { | ||
|
||
idx := findTxFromSlice(m[low:], tx) | ||
if idx == -1 { | ||
return -1 | ||
} | ||
|
||
return low + idx | ||
|
||
} | ||
|
||
mid := (low + high) / 2 | ||
if !(BigHexToBigDecimal(m[mid].GasPrice).Cmp(BigHexToBigDecimal(tx.GasPrice)) > 0) { | ||
return m.findTx(low, mid, tx) | ||
} | ||
|
||
return m.findTx(mid+1, high, tx) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.