Skip to content

Conversation

@Fengzdadi
Copy link
Contributor

Summary

Minor optimization to deserializeCouponList() in hll/coupon_list.go - pre-compute the offset instead of calculating listIntArrStart + it*4 in each iteration.

Related Issue

Closes #86

Changes

- for it := 0; it < couponCount; it++ {
-     list.couponIntArr[it] = int(binary.LittleEndian.Uint32(byteArray[listIntArrStart+it*4 : listIntArrStart+it*4+4]))
- }
+ base := listIntArrStart
+ for it := 0; it < couponCount; it++ {
+     list.couponIntArr[it] = int(binary.LittleEndian.Uint32(byteArray[base:]))
+     base += 4
+ }

Benchmark Results

Method 8 items 32 items 128 items 1024 items
Before 27.6 ns 75.4 ns 270 ns 2153 ns
After 26.8 ns 69.9 ns 253 ns 2000 ns

~7% improvement by replacing multiplication with addition per iteration.

Testing

All existing tests pass:

go test ./hll/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[HLL] Question: Optimization of byte-to-int conversion in coupon_list.go (TODO at line 193)

2 participants