Memory is precious

Posted  by Rahul Malik • 3 MinuteRead

Hi everybody, as you already know i have started my journey towards Rust from Java. Having a web development background one can say that managing memory and caring about it is  the last thing in our dev flow. Likes of Java, Javascript gives a false sense of memory managment, as the language does that job for the developer.

In my previous post i had mentioned that why my love for Java is waning. Java is great and it's GC is also good as they say "with great power comes a great responsibility", meaning Java GC requires a beast of machine to run even when the traffic is negligble.

I had to upgrade my 2gb ram linode to 4gb ram just so that CPU and Disk I/O remain low. Even now i have only one Java service out of mutiple services in Golang and Rust and still usage is very high.

Rust's borrow checker forces the developer look at variables and it's scope through a new paradigm, means once you give something to somebody it's gone. As you can see in the code snippet below i had to "clone" email before value is moved into different scopes because at the end of each scope value is dropped.

if let AppError::NotFoundError = err {
                let _email = email.clone();
                let _name = name.clone();
                let _another_email = email.clone();

                let hash_task = tokio::spawn(async move {
                    helpers::generate_hash(HashData::Email(_email.clone()), verify.clone())
                });
                let hash_string = match hash_task.await {
                    Ok(data) => data.await,
                    Err(_err) => return Err(AppError::ServerError),
                };
                let salt: [u8; 32] = rand::thread_rng().gen();
                let config = Config::default();
                let hash = match argon2::hash_encoded(password.as_bytes(), &salt, &config) {
                    Ok(hash) => hash,
                    Err(err) => {
                        tracing::error!("error hashing password: {}", err);
                        return Err(AppError::ServerError);
                    }
                };
                let member_entity = MemberEntity {
                    id: Uuid::new_v4().to_string(),
                    mobile: 1234567890,
                    name: name.clone(),
                    email: email.clone(),
                    password: hash,
                    email_verified: false,
                    mobile_verified: false,
                };

                let _email_strat = EmailStrategy {
                    email_name: name.clone(),
                    email_addr: email.clone(),
                };

                match member_entity
                    .create_member(
                        data,
                        verify.clone(),
                        expiry_string,
                        RegisterStrategy::Email(_email_strat),
                    )
                    .await
                {
                    Ok(_) => {
                        return Ok(HashResponseDTO { hash: hash_string });
                    }
                    Err(err) => {
                        return Err(err);
                    }
                }
            }
            return Err(AppError::ServerError);
        }


These type of qualities make learning rust a steep curve and can be frustrating at times. But it's good in the end as it gives you the love you deserve. 

For comparision one can see that same service in Rust compared to Java decreased resource usage by almost 70%.

Thanks for reading. No hate here for anybody all LOVE