optimized lock free srtgo #77
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary of Issues Fixed
The original srtgo implementation had several performance bottlenecks that caused excessive CPU usage:
runtime.LockOSThread()called for every operationOptimizations Implemented
1. Polling System Optimization (
pollserver.go,poll.go)Before:
-1) causing potential busy waitingAfter:
runtime.Gosched()to prevent busy spinningImpact: Reduces CPU usage by eliminating busy waiting and reducing lock contention.
2. Runtime Thread Locking Optimization (
srtgo.go)Before:
After:
Impact: Reduces thread contention and allows better goroutine scheduling.
3. Error Handling Efficiency (
errors.go)Before:
After:
srtGetAndClearErrorThreadSafe()helpersrtCheckError()for non-clearing error checksImpact: Reduces CGO overhead and simplifies error handling.
4. Read/Write Operation Optimization (
read.go,write.go)Before:
After:
Impact: Eliminates busy waiting loops and reduces unnecessary polling operations.
5. Callback Optimization (
logging.go,srtgo.go)Before:
After:
Impact: Eliminates goroutine creation overhead for callbacks.
Expected Performance Improvements
Based on the optimizations:
Migration Notes
The optimizations are backward compatible. No API changes were made, only internal implementation improvements.
Future Optimizations
Potential areas for further optimization:
Conclusion
These optimizations address the core performance issues in srtgo, making it suitable for use as a library in high-performance applications like mediamtx. The changes maintain API compatibility while significantly reducing CPU usage and improving overall efficiency.