📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

Extreme Data compression (Route 85)

Google for Developers2:56

Transcription

TODD KERPELMAN: So what if I told you there was a way that you could compress nearly any stream of data by a factor of 10x or more? Wouldn't that be something you'd be interested in? Yeah. I thought so. Let's find out more on this episode of Route 85.

[MUSIC PLAYING]

So I want you to take a look at this array of numbers here. Imagine that we wanted to send this array of integers from a server to your user's device. Looks like just a bunch of random numbers, right? Well, that word random is actually the key to compressing these in an incredibly efficient manner.

As you probably know, a random number generator isn't truly random. Supply a random number generator with the same seed, and you'll get the same results out every time. And we can take advantage of that fact to recreate that list of integers using a random number generator.

You see, all I need to do to regenerate that array on a device is to supply three parameters: the seed for an agreed upon random number generator, an upper bound to apply to these results, and the length of the list. I simply supply those numbers to a method that looks a little like this. And I can recreate that original number stream. Just like that, I've built my array of 30 integers using just two integers and an int32. That's a 92% compression rate.

Now granted, finding that initial seed did take some work. But you know what? That work can happen in the cloud. So it doesn't really matter. What's important is that on the device, I'm able to decompress that number stream in order and time. And then of course, once you start looking around, you can see that there's a ton of data you can compress this way.

I mean, need to compress a text string? Well, what's a string but a stream of encoded integers? Once I have my stream of integers, I simply figure out what seed I need to generate them. And voila! I've compressed my string down into just three numbers. It's a pretty amazing savings, right? Anybody with a username of StjxmsDZxGDqbpP will be singing your praises in their reviews.

And my gosh, if you think about it, an image is really just a stream of numbers broken out into several channels. Take a look at this image here. And you can see how, using a random number generator, I have been able to replace it with just three sets of integers for the red, green, and blue channels respectively.

Now once again, finding the right seed can take some time. And I haven't found the perfect seed just yet. So if you look at the results carefully, you can see that this is not quite a lossless compression scheme. But I think you'll agree that, for this kind of savings, these tradeoffs just might be worth it.

Anyway, I hope you consider using this technique the next time you have data that needs to be compressed. Remember, the more efficient you are with your user's data, the more they'll love you. Thanks again for watching. Be sure to check out other episodes of Route 85. And remember that, as my coworkers on the Android team like to say, perf matters. All right. Thanks, guys. I think we're done.

MALE SPEAKER: Who let him into the studio again?

FEMALE SPEAKER: I just, I couldn't say no to Elijah Wood.

MALE SPEAKER: But that's--

FEMALE SPEAKER: Elijah. Wood.