# Readability of Optimized Kotlin Code

DevFeed: [Readability of Optimized Kotlin Code](<https://devfeed.tech/articles/readability-of-optimized-kotlin-code-25605.md>)

Original publisher: [Read original article](<https://www.romainguy.dev/posts/2024/readability-of-optimized-kotlin-code/>)

Author: Romain Guy

Published: 2024-05-03T00:00:00Z

Content type: article

Language: en

Sources: [Posts on Romain Guy](<https://devfeed.tech/sources/posts-on-romain-guy.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Jetpack Compose](<https://devfeed.tech/topics/jetpack-compose.md>), [Data structures](<https://devfeed.tech/topics/data-structures.md>), [API](<https://devfeed.tech/topics/api.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [api](<https://devfeed.tech/tags/api.md>), [assembly](<https://devfeed.tech/tags/assembly.md>), [code](<https://devfeed.tech/tags/code.md>), [data-structure](<https://devfeed.tech/tags/data-structure.md>), [developer](<https://devfeed.tech/tags/developer.md>), [graphics](<https://devfeed.tech/tags/graphics.md>), [jetpack-compose](<https://devfeed.tech/tags/jetpack-compose.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [performance](<https://devfeed.tech/tags/performance.md>)

## AI overview

This article explains how Kotlin can keep low-level, micro-optimized code readable while preserving efficiency. It demonstrates a type-safe 8x8 grid encoded as a bitfield, using value classes and operator overloading for optimized rectangle and cell operations.

## Source excerpt

Leland and I were recently discussing how to best implement a new data structure to speed up a specific aspect of Jetpack Compose. He came up with a great idea, and nerd sniped me in the process. The problem was to efficiently encode the occupancy of an 8x8 grid represented as a Long (each bit representing a cell in the grid). After coming up with the bit twiddling code that quickly "rasterizes" a rectangle into the grid as a bitfield, I found myself thinking about how incredibly helpful Kotlin can be at making low-level/micro-optimized code easy to read1 for users of an API.