通过在Rust中进行内存优化来支持更快的文件加载时间
Figma’s multiplayer system handles loading files, propagating updates from multiple collaborators, and storing regular snapshots of file state. In order to make real-time collaboration as fast as possible across the complex graph in a Figma file, a lot of the file is loaded into memory.
Figma的 多人系统 处理文件加载、从多个协作者传播更新以及存储文件状态的常规快照。为了使Figma文件中复杂图形的实时协作尽可能快速,文件的大部分内容被加载到内存中。
As Figma grows, we look for ways to scale efficiently while preserving a great user experience. When we released dynamic page loading, we saw a 30% increase in the number of files we need to decode server-side. To handle this new load, we investigated several performance optimizations in Rust that resulted in faster load times and improved memory efficiency.
随着Figma的增长,我们寻找在保持良好用户体验的同时高效扩展的方法。当我们发布 动态页面加载 时,我们看到需要在服务器端解码的文件数量增加了30%。为了应对这一新负载,我们研究了几种Rust中的性能优化,结果实现了更快的加载时间和更好的内存效率。
Smaller, memory-efficient maps
更小的、节省内存的映射
A Figma file is conceptually a collection of nodes. Each node represents something different: a triangle, a square, a frame, etc. And each node can be thought of as a bundle of properties that tell us how to display it: its color, its shape, its parent, etc.
Figma 文件在概念上是一组节点的集合。每个节点代表不同的东西:一个三角形、一个正方形、一个框架等。每个节点可以被视为一组属性的集合,这些属性告诉我们如何显示它:它的颜色、形状、父节点等。
On the server, we represent nodes as a map of keys (property ID) to values, or Map<property_name (u16), property_value (u64 pointer)>
, where u16
and u64
refer to the bit-size of the entries in the map.
在服务器上,我们将节点表示为键(属性ID)到值的映射,或 Map<property_name (u16), property_value (u64 pointer)>
,其中 u16
和 u64
指的是映射中条目的位大小。
This map is in the critical path for loading a file, so any speedups here would propagate to improved file load times. Additionally, through some memory profiling we discovered that this map was responsible for more than 60% of a file’s memory usage. (Given that the map just stores metadata and not data, we were pretty surprised by this finding!)
这个映射在加载文件的关键路径中,因此这里的任何加速都会传播到改进的文件加载时间。此外,通过一些内存分析,我们发现这个映射占据了文件内存使用的 60% 以上。(考虑到这个映射只存储元数据而不是数据,我们对这个发现感到相当惊讶!)
So, we examined t...