Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

performance: remove extra conversions and optimize functions #553

Merged
merged 5 commits into from
Jun 11, 2024

Conversation

JukLee0ira
Copy link

@JukLee0ira JukLee0ira commented May 29, 2024

Proposed changes

  • Removed unnecessary string conversions, using binary forms of special addresses for direct bit comparisons to cut conversion costs and speed up comparisons.
  • Reduced repeated calls by introducing local variables to cache results, minimizing multiple calculations of the same expression, thus boosting performance.
  • Simplified code expressions for easier readability and maintenance.
  • Wrote unit tests to ensure consistent function behavior before and after optimization.

Example 1

  • Before: Checks quoteToken as string, converts common.XDCNativeAddress hex to address
if order.QuoteToken().String() != common.XDCNativeAddress {
		quotePrice = XDCxStateDb.GetLastPrice(GetTradingOrderBookHash(order.QuoteToken(),common.HexToAddress(common.XDCNativeAddress)))
		log.Debug("TryGet quotePrice QuoteToken/XDC", "quotePrice", quotePrice)
}
  • After: Directly compares common.XDCNativeAddressBinary by binary form, fetches price without conversion.
if order.QuoteToken() != common.XDCNativeAddressBinary {
		quotePrice = XDCxStateDb.GetLastPrice(GetTradingOrderBookHash(order.QuoteToken(), common.XDCNativeAddressBinary))
		log.Debug("TryGet quotePrice QuoteToken/XDC", "quotePrice", quotePrice)
}

Example 2

  • Before: Check token string equality with common.XDCNativeAddress and log with string conversions.
if token.String() == common.XDCNativeAddress {
		balance := statedb.GetBalance(addr)
		log.Debug("ApplyXDCXMatchedTransaction settle balance: SUB XDC NATIVE BALANCE BEFORE", "token", token.String(), "address", addr.String(), "balance", balance, "orderValue", value)
}
  • After: With the value of token known as XDCNativeAddress, use its string type directly.
if token == common.XDCNativeAddressBinary {
		balance := statedb.GetBalance(addr)
		log.Debug("ApplyXDCXMatchedTransaction settle balance: SUB XDC NATIVE BALANCE BEFORE", "token", common.XDCNativeAddress, "address", addr.String(), "balance", balance, "orderValue", value)
}

Example 3

  • Before: There were many type conversion functions and repetitive calls to the To() function to retrieve the same result in the logic.
func (tx *Transaction) IsSpecialTransaction() bool {
	if tx.To() == nil {
		return false
	}
	toBytes := tx.To().Bytes()
	randomizeSMCBytes := common.HexToAddress(common.RandomizeSMC).Bytes()
	blockSignersBytes := common.HexToAddress(common.BlockSigners).Bytes()
	return bytes.Equal(toBytes, randomizeSMCBytes) || bytes.Equal(toBytes, blockSignersBytes)
}
  • After: Direct bit comparison is used, and the result of tx.To() is stored in a variable for reuse, leading to a more streamlined and easily comprehensible code structure.
func (tx *Transaction) IsSpecialTransaction() bool {
	to := tx.To()
	return to != nil && (*to == common.RandomizeSMCBinary || *to == common.BlockSignersBinary)
}

Types of changes

What types of changes does your code introduce to XDC network?
Put an in the boxes that apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update (if none of the other choices apply)
  • Regular KTLO or any of the maintaince work. e.g code style
  • CICD Improvement
  • Performance

Impacted Components

Which part of the codebase this PR will touch base on,

Put an in the boxes that apply

  • Consensus
  • Account
  • Network
  • Geth
  • Smart Contract
  • External components
  • Not sure (Please specify below)

Checklist

Put an in the boxes once you have confirmed below actions (or provide reasons on not doing so) that

  • This PR has sufficient test coverage (unit/integration test) OR I have provided reason in the PR description for not having test coverage
  • Provide an end-to-end test plan in the PR description on how to manually test it on the devnet/testnet.
  • Tested the backwards compatibility.
  • Tested with XDC nodes running this version co-exist with those running the previous version.
  • Relevant documentation has been updated as part of this PR
  • N/A

@JukLee0ira JukLee0ira force-pushed the noString branch 2 times, most recently from 9e8b54a to 2d1425e Compare May 30, 2024 02:17
@gzliudan gzliudan changed the title performance: compare address directly instead of by function String() WIP: compare address directly instead of by function String() May 30, 2024
@JukLee0ira JukLee0ira force-pushed the noString branch 7 times, most recently from e83c50b to b04f843 Compare June 1, 2024 08:32
@JukLee0ira JukLee0ira changed the title WIP: compare address directly instead of by function String() performance: compare address directly Jun 1, 2024
Copy link
Collaborator

@gzliudan gzliudan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also change skipNonceDestinationAddress = map[string]bool to skipNonceDestinationAddress = map[common.Address]bool or skipNonceDestinationAddress = map[common.Address]struct{}. The latter one is better.

core/types/transaction.go Outdated Show resolved Hide resolved
core/types/transaction.go Outdated Show resolved Hide resolved
@JukLee0ira JukLee0ira force-pushed the noString branch 2 times, most recently from 5832c40 to 3e8f4bf Compare June 3, 2024 07:09
@JukLee0ira JukLee0ira changed the title performance: compare address directly WIP: performance: compare address directly Jun 3, 2024
core/types/transaction.go Outdated Show resolved Hide resolved
core/types/transaction.go Outdated Show resolved Hide resolved
@JukLee0ira JukLee0ira force-pushed the noString branch 5 times, most recently from 1193d82 to 80a687e Compare June 5, 2024 02:20
@JukLee0ira JukLee0ira force-pushed the noString branch 3 times, most recently from f5af2f7 to bc47c26 Compare June 6, 2024 09:29
@JukLee0ira JukLee0ira changed the title WIP: performance: compare address directly performance: remove extra conversions and optimize functions, include unit tests Jun 6, 2024
@JukLee0ira JukLee0ira changed the title performance: remove extra conversions and optimize functions, include unit tests performance: remove extra conversions and optimize functions Jun 6, 2024
Copy link
Collaborator

@gzliudan gzliudan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link

@liam-icheng-lai liam-icheng-lai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good Improvement

Copy link
Collaborator

@liam-lai liam-lai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good improvement

@gzliudan gzliudan merged commit b718f35 into XinFinOrg:dev-upgrade Jun 11, 2024
17 checks passed
@JukLee0ira JukLee0ira deleted the noString branch June 14, 2024 11:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants