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

Fix/ip2 country out of memory #1628

Merged
merged 4 commits into from
Aug 21, 2024
Merged

Conversation

ThomasSession
Copy link
Collaborator

Helping with the out of memory crash we are getting from the playstore:
https://play.google.com/console/u/0/developers/8294769987979883884/app/4974570314892392602/vitals/crashes/a4b76784091e144fe86ab5a64cb86295/details?days=28&versionCode=3795%2C3785&isUserPerceived=true

Avoid memory allocation in the ipv4Int logic and avoiding reading the whole csv file.

@simophin
Copy link

Neat! One nitpick I have is about the manual closing of Closable, there's a use that is perfect suited for this occasion. So instead of this:

 val csv = CSVReader(FileReader(file.absoluteFile)).apply {
            skip(1)
        }

        val ipCountries = csv.asSequence().associateTo(TreeMap()) { cols ->
            Ipv4Int(cols[0]) to cols[1].toIntOrNull()
        }

        csv.close()

        ipCountries

You can do this:

        CSVReader(FileReader(file.absoluteFile)).use { csv ->
            csv.skip(1)
  
            csv.asSequence().associateTo(TreeMap()) { cols ->
                Ipv4Int(cols[0]) to cols[1].toIntOrNull()
            }
        }

It'll make sure the close is always called even if there's an exception in between

Copy link

@simophin simophin left a comment

Choose a reason for hiding this comment

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

Nice!

@ThomasSession ThomasSession merged commit e731c7d into dev Aug 21, 2024
2 checks passed
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.

2 participants