EUVIMEDOpdag EUVIMED
Crossref

Efficient Data Representation Using FlatBuffers

Chaitanya Koparkar

XRDS: Crossroads, The ACM Magazine for Students · 2022-12 · 29 (2) · 50-51

DOI: 10.1145/3571304 ↗

At a glance

Journal article in XRDS: Crossroads, The ACM Magazine for Students, published online on 2023-01-12. Written by Chaitanya Koparkar. Cited 6 times so far (Crossref).

AbstractBibliografiske oplysningerCiteRelated studies

Abstract (original language)

HOW TO USE FLATBUFFERS? At a high level, there are two steps to using FlatBuffers. First, we need to describe all serializable datatypes of our application to the FlatBuffers library. This is often called the schema of the application. This schema allows us to describe datatypes using familiar features such as structs, enums, unions, and primitive types such as numbers, strings, etc. Next, the flatc compiler uses this schema to generate language-specific code files (e.g. C++, Rust, Go, etc.) that can be used to serialize and deserialize these datatypes. Describing the schema ahead of time like this allows FlatBuffers to generate efficient deserialization code. The next step is to use the generated code files in the application. We will see how this works for a simple tweet datatype shown in Listing 1. A tweet is made up of the content of the tweet, which is a string; the user who tweeted it, which is modeled as a separate type User; the hashtags that are a list of strings; and attachments, which are also a separate type. The types User and Attachment are also defined similarly. The root _ type declares what will be the root level datatype for the serialized data. In our case, we want the data to contain a list of tweets. Next, we will compile this schema to generate code for the Rust programming language using the command: flatc --rust tweet. fbs. This creates a file called tweet _ In a typical program, the representation of data in memory is distinct from its representation in a serialized form on disk or on the network. The former has pointers and an arbitrary, sparse layout, facilitating easy manipulation by a program, while the latter is packed contiguously, facilitating easy I/O. Thus, whenever a program receives data from the network or disk, it must first deserialize it before processing it and serialize it again before I/O. For example, web applications often receive JSON data from the network that is deserialized, processed, reserialized, and then sent over the network again. Having to change representations like this can be a significant source of overhead due to the temporary data structures that are created during this process. Moreover, pointer-heavy representations that are used in most programming languages are highly inefficient—creating such data often requires allocating a lot of memory, and traversing it requires loading (potentially) far away pointers that can cause cache misses. It is much better to represent data densely with minimal allocations, no temporary objects, and good locality. For example, using an array to store a large set of numbers and then computing their sum is vastly more efficient than using a linked list for this purpose. Using an efficient representation is even more important in memoryconstrained environments such as smartwatches, fitness trackers, and other wearable devices. What if we could use a serialized, dense, array-like representation for storing tree-like data even in memory? In this column we’ll look at one way to accomplish this, using Google’s FlatBuffers 1 library. Programs that traverse and construct trees are widely used across all domains of computer science such as compiler passes to the browser Document Object Model, particle simulations with space-partitioning trees, and web applications. The technique described herein can be applied to all these domains to improve performance.

Cite

APA

Koparkar, C. (2022). Efficient Data Representation Using FlatBuffers. XRDS: Crossroads, The ACM Magazine for Students, 29(2), 50-51. https://doi.org/10.1145/3571304

Vancouver

Koparkar C. Efficient Data Representation Using FlatBuffers. XRDS: Crossroads, The ACM Magazine for Students. 2022;29(2):50-51. doi:10.1145/3571304

BibTeX
@article{Koparkar2022,
  author = {Koparkar, Chaitanya},
  title = {Efficient Data Representation Using FlatBuffers},
  journal = {XRDS: Crossroads, The ACM Magazine for Students},
  year = {2022},
  volume = {29},
  number = {2},
  pages = {50--51},
  doi = {10.1145/3571304}
}
Start for free →