973 Commits
Author SHA1 Message Date
xtaci 2693fa8e71 perf: reduce heap allocations and fix memory leaks across multiple components
Optimizations:
- ringbuffer: optimize Len(), IsEmpty(), Discard(), Clear() methods with
  branchless arithmetic and simplified logic
- fec: fix memory leak in shardHeap.Pop() by setting element to nil before
  truncating slice, preventing GC from holding stale references
- timedsched: fix memory leak in timedFuncHeap.Pop() with same nil-before-truncate
  pattern; optimize prepend() using slice swap instead of append
- autotune: add sortCache field to Autotuner to reuse slice allocation in
  FindPeriod(), avoiding repeated allocations during period detection
- snmp: replace fmt.Sprint with strconv.FormatUint for better performance
  in Copy() and Reset() methods
- entropy: remove defer overhead in rngAES.Read() and rngChacha8.Read()
  hot paths for reduced function call cost
- bufferpool: pre-allocate ErrBufferPoolSizeExceeded error; reset slice
  length on Put() to allow proper reuse
- sess: remove defer overhead in 6 setter methods (SetWriteDelay,
  SetWindowSize, SetStreamMode, SetACKNoDelay, SetDUP, SetNoDelay)

These changes reduce memory allocations, fix potential memory leaks in
heap-based data structures, and eliminate defer overhead in frequently
called methods.
2026-01-29 12:22:33 +08:00
xtaci 41b30e042b keep kcp.buffer compatible with C version 2026-01-28 12:20:44 +08:00
xtaci 8fd6d90d50 perf(test): reduce 1GB echo allocations and random overhead
- Reuse buffers and use rng.Read to cut per-iteration allocations in random echo test
- Separate RNG for payload length to keep data stream deterministic
- Optimize UDP source validation by comparing UDPAddr directly to avoid per-packet String allocation
v5.6.64
2026-01-26 11:23:51 +08:00
xtaci b27887e8ce fix(fec): prevent permanent blocking in auto-tune when parameters unchanged
Fix shouldTune flag not being reset when auto-detected FEC parameters
match current parameters. This could cause the decoder to permanently
discard all incoming packets if a corrupted packet triggered shouldTune
but the actual FEC configuration hadn't changed.

Move `dec.shouldTune = false` outside the parameter-change check to
ensure the flag is always reset when valid parameters are detected.
2026-01-26 11:02:30 +08:00
xtaci ca00425149 add more tests to aututune 2026-01-26 10:54:37 +08:00
xtaci 9d77619765 perf(autotune): replace heap with fixed-size ring buffer to reduce allocations
Replace the heap-based pulseHeap implementation with a fixed-size ring
buffer array to avoid interface{} allocations from container/heap
Push/Pop operations.

Changes:
- Use fixed-size array [maxAutoTuneSamples]pulse instead of slice
- Track elements with head/tail/count instead of heap operations
- Copy elements from ring buffer in FindPeriod() for sorting
- Remove container/heap import dependency

Benchmark results (BenchmarkEchoSpeed4K):
- Before: 185 allocs/op
- After:  163 allocs/op (~12% reduction)

All existing tests pass with identical behavior.
v5.6.63
2026-01-25 23:35:07 +08:00
xtaci cbe05256b0 Merge branch 'master' into dev 2026-01-25 22:13:18 +08:00
xtaci ad2550932b crypt: optimize encryption routines with bitwise operations and lock improvements
- Replace division and modulo operations with bitwise equivalents:
  - len(src) / 8  -> len(src) >> 3
  - len(src) / 16 -> len(src) >> 4
  - n / 8  -> n >> 3
  - n % 8  -> n & 7

- Improve lock handling in blockCrypt:
  - Split single mutex into separate encMu/decMu for encrypt/decrypt
  - Remove defer for Lock/Unlock to reduce overhead
  - Cache blockSize in struct to avoid repeated method calls

- Optimize Salsa20 and None ciphers:
  - Skip unnecessary copy when dst and src point to same memory
  - Add //go:nosplit directive to hot path functions

All encryption tests pass. Benchmark results show no regression with
zero memory allocations maintained (0 B/op, 0 allocs/op).
2026-01-25 22:10:17 +08:00
xtaci 8cdb131af5 Squashed commit of the following:
commit e51740f7e6
Author: xtaci <imap@live.com>
Date:   Sat Jan 24 22:11:58 2026 +0800

    optimize crypto using subtle/XORBytes

commit 071dc4a69d
Author: xtaci <imap@live.com>
Date:   Sat Jan 24 21:55:49 2026 +0800

    1. Fix incorrect RepeatSegs counter in parse_data() function
       - Previously, the repeat flag was never set to true when duplicate packets
         were detected, causing incorrect statistics reporting
       - Added missing else branch to properly mark duplicate segments
       - Impact: Correct tracking of repeated packet statistics

    2. Fix memory leak in FEC decoder auto-tune functionality
       - When FEC parameters were adjusted dynamically, the old shardSet map
         was discarded without recycling its buffer pool entries
       - Added proper cleanup loop to return old shard packets to buffer pool
         before creating new shardSet
       - Impact: Prevents memory growth during long-lived connections with
         dynamic FEC parameter adjustments

    Files modified:
    - kcp.go: Added else branch in parse_data() to set repeat=true for duplicates
    - fec.go: Added buffer cleanup loop before shardSet recreation in decode()

    All existing tests pass, including FEC encoding/decoding and lossy connection tests.
v5.6.62
2026-01-24 22:13:21 +08:00
xtaci e51740f7e6 optimize crypto using subtle/XORBytes 2026-01-24 22:11:58 +08:00
xtaci 071dc4a69d 1. Fix incorrect RepeatSegs counter in parse_data() function
- Previously, the repeat flag was never set to true when duplicate packets
     were detected, causing incorrect statistics reporting
   - Added missing else branch to properly mark duplicate segments
   - Impact: Correct tracking of repeated packet statistics

2. Fix memory leak in FEC decoder auto-tune functionality
   - When FEC parameters were adjusted dynamically, the old shardSet map
     was discarded without recycling its buffer pool entries
   - Added proper cleanup loop to return old shard packets to buffer pool
     before creating new shardSet
   - Impact: Prevents memory growth during long-lived connections with
     dynamic FEC parameter adjustments

Files modified:
- kcp.go: Added else branch in parse_data() to set repeat=true for duplicates
- fec.go: Added buffer cleanup loop before shardSet recreation in decode()

All existing tests pass, including FEC encoding/decoding and lossy connection tests.
2026-01-24 21:55:49 +08:00
xtaci b38e76e5b0 add a test for variable length input in fec_test.go 2026-01-01 18:23:07 +08:00
xtaci d3d1f00432 update README for gists v5.6.61 2025-12-30 12:18:56 +08:00
xtaci 55536867fe refine toc 2025-12-29 11:48:59 +08:00
xtaci b893d5527e fix tests 2025-12-28 21:17:15 +08:00
xtaci 83866a3c41 Add files via upload 2025-12-28 18:29:39 +08:00
xtaci 73dc4141c1 update link 2025-12-28 12:12:38 +08:00
xtaci 2b4be79032 update link 2025-12-28 12:10:27 +08:00
xtaci e1d91f7e8d add TOC to README 2025-12-28 11:23:23 +08:00
xtaci 53c14d4570 refactor: unify and simplify OOB handler interface for UDPSession
- Replace atomic.Pointer[func([]byte)] with atomic.Value for OOB callback storage to improve compatibility.
- Rename SetCallbackForOOB to SetOOBHandler and GetMaxSizeForOOB to GetOOBMaxSize for clearer, consistent API naming.
- Add locking in SendOOB to ensure thread safety during OOB packet construction.
- Update related test cases in sess_test.go to use the new interface names.
- Revise comments and documentation for better clarity and maintainability.

user takes care of the nil handler

use 0xffffffff as the seqid of OOB

return error on nil handler

fix race in test

shorten test data size

add more test on OOB

enhance Error test

allow nil from sender side

add OOBPackets in snmp

use dummy handler instead of resetting the atomic.Value

add test for dummy handler

remove a comment

formalize callback function prototype

use const

rename to expose

Add comments to clarify KCP input and FEC decoding order for FEC packets.

fix test

comments

add TestOOB_OneSideHandler
v5.6.60
2025-12-27 23:25:03 +08:00
xtaci e51e7ce2bd Merge branch 'lonnywong-dev' into dev 2025-12-27 14:14:18 +08:00
Lonny Wong eb7d8ee32f Add out-of-band (OOB) data support 2025-12-27 10:12:56 +08:00
xtaci bca9b95082 refine struct to support oob 2025-12-24 20:40:47 +08:00
xtaci 6e5555659b Add files via upload 2025-12-22 15:49:25 +08:00
xtaci 72a7cd956b Update image tag in README_zh.md 2025-12-22 15:43:16 +08:00
xtaci 54a5b52d8b Modify image tag for layer model in README
Updated image tag in README to remove height attribute.
2025-12-22 15:42:48 +08:00
xtaci b3941d00d3 Add files via upload 2025-12-22 15:42:08 +08:00
xtaci f53a3373f8 optimize: modernize -fix -test . 2025-12-22 13:10:47 +08:00
xtaci 4c43e64cf9 optimize diagram 2025-12-21 21:39:31 +08:00
xtaci 187fbc7a8d refine FEC chinese translation 2025-12-21 21:18:24 +08:00
xtaci 1c7bade67e refine chinese 2025-12-21 21:15:24 +08:00
xtaci c69903395e add FEC charicteristics in README files 2025-12-21 20:34:08 +08:00
xtaci 2c60a3962c optimize chinese readme 2025-12-21 12:00:42 +08:00
xtaci 1f862ed7b2 fix an error in README 2025-12-20 21:10:12 +08:00
xtaci 0882ff65f3 update README layout 2025-12-20 21:07:27 +08:00
xtaci 714b8d3472 update README 2025-12-20 21:02:45 +08:00
xtaci 3077b3456b update link 2025-12-20 19:18:36 +08:00
xtaci 0ff7f67f31 update link 2025-12-20 19:17:36 +08:00
xtaci 4018dee15c optimize translation 2025-12-20 19:11:57 +08:00
xtaci 34c23191e3 update README 2025-12-20 18:44:46 +08:00
xtaci a3f373a12d Clean up README_zh.md formatting
Removed unnecessary markdown formatting from README_zh.md.
2025-12-20 18:39:29 +08:00
xtaci efc292a42a docs: add Chinese README and link from README.md 2025-12-20 18:37:51 +08:00
xtaci 0f19623e41 Remove unused fields from data structure
Removed unused fields from the data structure in the README.
2025-12-20 17:12:29 +08:00
xtaci 105cf50bcc Update README.md with Layer-Model and examples
Reorganize and update sections for clarity and completeness.
2025-12-20 17:10:53 +08:00
xtaci 708a281ba8 Update README.md 2025-12-20 17:00:38 +08:00
xtaci 2f94386ff4 Add files via upload 2025-12-20 16:59:39 +08:00
xtaci 325304f6fa add FEC RTO and Skip Parity Test 2025-12-20 16:42:44 +08:00
xtaci 9458fa7a51 let Put returns error if capacity is invalid 2025-12-20 16:10:08 +08:00
xtaci 34a5fc6600 fix: expected value and actual value swapped 2025-12-20 15:49:47 +08:00
xtaci 052b09682f refine tests for better coverage
mv entropy related test to entropy_test.go
2025-12-19 21:59:05 +08:00