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

coin_processor optimization: compile regex once #333

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions rust/processor/src/models/coin_models/coin_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
use anyhow::{Context, Result};
use aptos_protos::transaction::v1::{move_type::Content, MoveType, WriteResource};
use bigdecimal::BigDecimal;
use once_cell::sync::Lazy;
use regex::Regex;
use serde::{Deserialize, Serialize};
use tracing::error;
Expand Down Expand Up @@ -162,15 +163,22 @@ pub struct CoinInfoType {
creator_address: String,
}

static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(<(.*)>)").unwrap());

static COIN_RESOURCES: Lazy<[String; 2]> = Lazy::new(|| {
[
format!("{}::coin::CoinInfo", COIN_ADDR),
format!("{}::coin::CoinStore", COIN_ADDR),
]
});

impl CoinInfoType {
/// get creator address from move_type, and get coin type from move_type_str
/// Since move_type_str will contain things we don't need, e.g. 0x1::coin::CoinInfo<T>. We will use
/// regex to extract T.
pub fn from_move_type(move_type: &MoveType, move_type_str: &str, txn_version: i64) -> Self {
if let Content::Struct(struct_tag) = move_type.content.as_ref().unwrap() {
let re = Regex::new(r"(<(.*)>)").unwrap();

let matched = re.captures(move_type_str).unwrap_or_else(|| {
let matched = RE.captures(move_type_str).unwrap_or_else(|| {
error!(
txn_version = txn_version,
move_type_str = move_type_str,
Expand Down Expand Up @@ -225,11 +233,7 @@ pub enum CoinResource {

impl CoinResource {
pub fn is_resource_supported(data_type: &str) -> bool {
[
format!("{}::coin::CoinInfo", COIN_ADDR),
format!("{}::coin::CoinStore", COIN_ADDR),
]
.contains(&data_type.to_string())
COIN_RESOURCES.contains(&data_type.to_string())
}

pub fn from_resource(
Expand Down
Loading