今日已更新 344 条资讯 | 累计 37249 条内容
关于我们

PR#1: Make SurrealDB performance slightly better

Hossein Mobarakian 2026年08月22日 20:50 6 次阅读 来源:Dev.to

At the first step, I picked up the SurrealDB project for contribution. I didn't know how I could help this project become better. So I asked my beautiful OpenCode to find parts of the project that could be better. It suggested this file of the project(core/src/val/value/get.rs) to me and said it has a double-cloning issue. So I opened up VS Code, and I started checking the issue. The code was something like this: let mut a = Vec :: new (); for v in v .iter () { let cur = v .clone () .into (); if stk .run (| stk | w .compute ( stk , ctx , opt , Some ( & cur ))) .await .catch_return () ? .is_truthy () { a .push ( v .clone ()); } } First Optimization: As you can see at line 3 and line 9, we have multiple clones from a single document. I thought about how I could fix this issue; I went to see the CursorDoc structure because the first clone is converted to it: #[derive(Clone, Debug)] pub ( crate ) struct CursorDoc { pub ( crate ) rid : Option < Arc < RecordId >> , pub ( crate ) ir : Option < Arc < IteratorRecord >> , pub ( crate ) doc : CursorRecord , pub ( crate ) fields_computed : bool , } impl From < Value > for CursorDoc { fn from ( val : Value ) -> Self { Self { rid : None , ir : None , doc : val .into (), fields_computed : false , } } } #[derive(Clone, Debug)] pub ( crate ) struct CursorRecord { /// The underlying record, shared via Arc for copy-on-write record : Arc < Record > , } impl CursorRecord { // .... // /// cloning. Otherwise the value is cloned. pub ( crate ) fn into_owned ( self ) -> Value { match Arc :: try_unwrap ( self .record ) { Ok ( record ) => record .data , Err ( arc ) => arc .data .clone (), } } // .... // } impl From < Value > for CursorRecord { fn from ( value : Value ) -> Self { Self { record : Arc :: new ( Record :: new ( value )), } } } I saw that the value passed through CursorDoc is directly stored in a field in CursorRecord without any changes, and it is accessible using .into_owned() from CursorRecord. That is the solution; I edited the

本文内容来源于互联网,版权归原作者所有
查看原文