urlyy commented on code in PR #2492:
URL: https://github.com/apache/fory/pull/2492#discussion_r2286969438


##########
rust/fory-core/src/buffer.rs:
##########
@@ -88,31 +88,27 @@ impl Writer {
     }
 
     pub fn var_int32(&mut self, value: i32) {
-        if value >> 7 == 0 {
-            self.u8(value as u8);
-        } else if value >> 14 == 0 {
-            let u1 = (value & 0x7F) | 0x80;
-            let u2 = value >> 7;
-            self.u16(((u1 << 8) | u2) as u16);
-        } else if value >> 21 == 0 {
-            let u1 = (value & 0x7F) | 0x80;
-            let u2 = (value >> 7) | 0x80;
-            self.u16(((u1 << 8) | u2) as u16);
-            self.u8((value >> 14) as u8);
-        } else if value >> 28 == 0 {
-            let u1 = (value & 0x7F) | 0x80;
-            let u2 = (value >> 7) | 0x80;
-            let u3 = (value >> 14) | 0x80;
-            let u4 = (value >> 21) | 0x80;
-            self.u32(((u1 << 24) | (u2 << 16) | (u3 << 8) | u4) as u32);
+        let mut v = value as u32;
+        let len = if v < (1 << 7) {
+            1
+        } else if v < (1 << 14) {
+            2
+        } else if v < (1 << 21) {
+            3
+        } else if v < (1 << 28) {
+            4
         } else {
-            let u1 = (value & 0x7F) | 0x80;
-            let u2 = (value >> 7) | 0x80;
-            let u3 = (value >> 14) | 0x80;
-            let u4 = (value >> 21) | 0x80;
-            self.u32(((u1 << 24) | (u2 << 16) | (u3 << 8) | u4) as u32);
-            self.u8((value >> 28) as u8);
+            5
+        };
+        let mut buf = [0u8; 5];

Review Comment:
   I wasn’t aiming to improve readability. Since the previous implementation 
had issues, I wrote a version that is logically correct but less efficient. The 
issues with the previous implementation were: 1) it didn’t handle 
`little-endian` correctly when calling `u16()` and `u32()`, and 2) it didn’t 
mask the lower 7 bits using `0x7F`. I’ve now added a `unit test` and modified 
the previously written-merge code. The old implementation couldn’t pass the 
test, but the current version does.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to