137 Commits
Author SHA1 Message Date
xtaci 3b4ec04d35 fix typo 2026-03-13 17:21:53 +08:00
xtaci cf75cc7890 perf(stream): cache window update threshold to reduce computation
Add windowUpdateThreshold field to stream struct to cache the
MaxStreamBuffer/2 calculation. This avoids repeated pointer
dereferences (s.sess.config.MaxStreamBuffer) and integer division
on every tryReadV2() and writeToV2() call in the hot path.

- Add windowUpdateThreshold uint32 field to stream struct
- Initialize threshold in newStream()
- Use cached value in tryReadV2() and writeToV2()
2026-02-09 15:37:33 +08:00
xtaci 532973c12c perf(stream): optimize Read by hoisting version check out of loop
Move protocol version check from inside the read loop to outside,
eliminating per-iteration switch statement overhead. The version
is constant for the stream's lifetime, so checking once is sufficient.

Before: switch inside for loop (branch on every iteration)
After:  if-else with separate loops (branch once per Read call)
2026-02-09 15:25:16 +08:00
xtaci 8668391e92 perf: optimize bufferRing and shaperLoop for better throughput
- bufferRing: use bitmask instead of modulo for index calculation
  * Ensure capacity is always power of 2
  * Replace % with & for faster index wrapping in push/pop/grow/consumeFront

- shaperLoop: batch drain pending write requests
  * Collect multiple requests before notifying sendLoop
  * Reduces goroutine wake-up overhead under high load
2026-02-09 15:18:17 +08:00
xtaci 8261adb315 refactor: optimize performance with reduced lock contention and code deduplication
- bufferRing: add consumeFront() method to eliminate duplicate buffer
  consumption code in tryReadV1/tryReadV2, move allocator.Put() outside
  critical section to reduce lock hold time

- Session.IsClosed(): replace select-based check with atomic flag for
  faster hot path execution, add 'closed' field with atomic operations

- shaperQueue: convert count to atomic int64, Len() and IsEmpty() now
  lock-free using atomic.LoadInt64()

- recvLoop: move stream.fin() and stream.update() calls outside lock
  scope in cmdFIN/cmdUPD handlers to reduce lock contention

- stream: extract checkWriteClosed() helper to deduplicate close checks
  in writeV1/writeV2

All tests pass with race detector enabled.
2026-01-29 13:02:44 +08:00
xtaci c55575ea34 feat: autocleanup when both sides called CloseWrite() 2026-01-26 20:47:46 +08:00
xtaci baf236b97a feat(stream): implement TCP half-close support via CloseWrite()
Add CloseWrite() method to enable TCP-like half-close semantics without
protocol changes. This allows one side to signal EOF while still receiving
data from the peer.

Changes:
- Add chWriteClosed channel to track local write-side closure
- Add CloseWrite() method that sends FIN but keeps read side open
- Modify writeV1/writeV2 to check local write state instead of remote FIN
- Update Close() to also close write side for backward compatibility

The existing cmdFIN protocol command is reused - CloseWrite sends FIN to
signal write completion while the stream remains readable.

Usage:
  stream.Write(data)
  stream.CloseWrite()  // send FIN, can still read
  io.ReadAll(stream)   // read peer's response
2026-01-26 18:20:43 +08:00
xtaci 4af4b02304 use bufferRing in stream.go to reduce memory allocations 2026-01-26 15:17:20 +08:00
xtaci cb81ae99ee perf: reduce memory allocations in hot paths
- Add writeRequestPool to reuse writeRequest objects and their result
  channels in session.go, reducing allocations per write operation

- Add shaperHeapPool to reuse shaperHeap objects in shaper.go, avoiding
  heap allocations when streams are created/destroyed

- Pre-allocate buffers and heads slices with capacity 8 in newStream()
  to reduce slice growth allocations during data transfer

Benchmark results (128KB data transfer):
  Before: 36 allocs/op, ~910 MB/s
  After:  20 allocs/op, ~950 MB/s

This reduces memory allocations by 44% and improves throughput by ~5%.
2026-01-25 22:56:15 +08:00
xtaci 129520968d Fix memory leak caused by buffer capacity modification during read operations preventing successful allocator recycling. 2025-12-19 12:26:17 +08:00
xtaci b7a5d16a75 properly stop the timer to avoid resource leaks, and add a limit for MaxReceiveBuffer
uncomment
2025-12-18 17:54:27 +08:00
xtaci e49a716823 make numWritten consistent 2025-12-18 17:14:12 +08:00
xtaci d3df2cdee1 add comments 2025-12-14 15:23:59 +08:00
xtaci 18fbbf99f8 (chores): optimize function structure and allow writers to change the deadline during waiting for window update.
rename vars
2025-12-13 22:36:52 +08:00
xtaci 0f82c6d5d6 Merge branch 'rbqvq-dev' 2025-12-10 11:37:20 +08:00
Coia Prant 004a112bb0 refactor: remove else branch 2025-12-09 23:39:19 +08:00
xtaci 489b45e3d3 fix issue github.com/xtaci/smux/issues/107 2025-10-12 22:03:19 +08:00
xtaci 10971dd692 fix issue: https://github.com/xtaci/smux/issues/106 2025-08-19 10:52:02 +08:00
lesismal 80f1038663 allocator optimization: avoid tmp obj's allocation and gc 2025-02-17 17:34:40 +08:00
Coia Prant 41e6a518d8 fix: memory leak 2024-09-29 19:03:12 +08:00
xtaci 784d53ae55 recycle stream immediately if no object holds *Stream pointer 2024-09-15 21:55:22 +08:00
xtaci 990aae2edd add a comment to WriteTo 2024-09-02 11:47:51 +08:00
xtaci f4519fc161 send FIN in normal order instead of in priority 2024-08-31 21:25:26 +08:00
xtaci d245826f89 prevent sending more data in passive closing 2024-08-31 00:09:56 +08:00
xtaci 44d69f6d1a refine comments 2024-08-01 17:25:10 +08:00
xtaci 18a4b3cad9 elaborate comments on source codes 2024-07-28 22:10:35 +08:00
xtaci 0ec7e350e2 classify windowUpdate as CLSCTRL 2024-07-28 21:17:23 +08:00
xtaci 28b48935d0 add license to source codes 2024-07-25 21:59:09 +08:00
xtaci 79ed6364e6 differentiate control frame and data frame 2023-02-14 22:19:42 +08:00
xtaci a54d6cb361 control of shaper heap size 2023-02-12 19:51:18 +08:00
xtaci 09e2c01560 fix shaper prio overflow 2021-09-22 21:38:58 +08:00
xtaci fa45f32081 bug fix for https://github.com/xtaci/smux/issues/82 2020-11-25 22:53:43 +08:00
xtaci d2d7690bcc trigger waitRead to return in SetReadDeadline 2020-02-09 18:33:05 +08:00
xtaci 6da22a180e hide unused api 2020-01-16 22:23:37 +08:00
xtaci e7525af09e remove poller API 2020-01-16 22:07:10 +08:00
xtaci b8558d7e96 update 2020-01-16 21:51:41 +08:00
xtaci 3c744e6653 tidy 2020-01-16 21:47:48 +08:00
xtaci 211bc4a98c fix spelling 2020-01-04 00:23:22 +08:00
xtaci fe8b9a2233 Squashed commit of the following:
commit 4d53a564a987a171d9cc563ee73e92c7faa3624a
Author: xtaci <daniel820313@gmail.com>
Date:   Sat Dec 28 22:14:17 2019 +0800

    add function PeekSize

commit 9ca9c83c3f06aca7ceef161f39adce12de2d1ec5
Author: xtaci <daniel820313@gmail.com>
Date:   Sat Dec 28 21:53:41 2019 +0800

    add Peak
2019-12-28 22:16:15 +08:00
xtaci 32b9d9aa29 remove redundant line 2019-12-28 21:04:08 +08:00
xtaci f182401f19 merge protocol version 2 in master 2019-12-26 17:25:11 +08:00
xtaci fe34c960fb Revert "config protocol Version in Config"
This reverts commit 9f184cedda.
2019-12-26 13:39:01 +08:00
xtaci 9f184cedda config protocol Version in Config 2019-12-26 12:30:53 +08:00
xtaci f386d90508 Squashed commit of the following:
commit 486431f53deb10b0e6ee51b47785f6f06f0d5733
Author: xtaci <daniel820313@gmail.com>
Date:   Thu Dec 26 12:02:52 2019 +0800

    remove in-package pkg/errors

commit 213597603a726735678024ec6051800c46ed0bd0
Author: xtaci <daniel820313@gmail.com>
Date:   Thu Dec 26 11:58:36 2019 +0800

    fix poll

commit d1f7aeefbeb40da99d47ba2b7db5bd403c09ddba
Author: xtaci <daniel820313@gmail.com>
Date:   Thu Dec 26 11:17:28 2019 +0800

    fix error

commit 152918f9b531c06b42c764f0aff35d4bb9315713
Author: xtaci <daniel820313@gmail.com>
Date:   Wed Dec 25 23:27:18 2019 +0800

    poll for v1
2019-12-26 12:06:21 +08:00
xtaci 3b54c0fc00 fix a possible race condition 2019-12-19 16:18:34 +08:00
xtaci 36273058e1 Revert "remove allocator"
This reverts commit f893f033aa.
2019-12-18 22:13:51 +08:00
xtaci f893f033aa remove allocator 2019-12-18 21:44:35 +08:00
xtaci 795b9695f3 should return all tokens 2019-11-26 22:59:58 +08:00
xtaci c913b2d39b add stream.WriteTo method 2019-11-26 21:17:12 +08:00
xtaci 0e3229b827 expose errors definition in smux 2019-09-22 14:31:20 +08:00