1818#ifndef UWS_ASYNCSOCKETDATA_H
1919#define UWS_ASYNCSOCKETDATA_H
2020
21- #include < cstring>
2221#include < string>
2322
2423namespace uWS {
2524
2625struct BackPressure {
27- static constexpr size_t TRIM_THRESHOLD = 64 * 1024 ;
28- static constexpr size_t RETAIN_CAPACITY = 32 * 1024 ;
29-
3026 std::string buffer;
3127 unsigned int pendingRemoval = 0 ;
32-
33- void normalize () {
34- size_t length = buffer.length () - pendingRemoval;
35- if (length) {
36- memmove (buffer.data (), buffer.data () + pendingRemoval, length);
37- }
38- buffer.resize (length);
39- pendingRemoval = 0 ;
40- }
41-
4228 BackPressure (BackPressure &&other) {
4329 buffer = std::move (other.buffer );
4430 pendingRemoval = other.pendingRemoval ;
@@ -48,43 +34,29 @@ struct BackPressure {
4834 buffer.append (data, length);
4935 }
5036 void erase (unsigned int length) {
51- size_t logicalLength = this ->length ();
52- if (length >= logicalLength) {
53- clear ();
54- return ;
55- }
56-
5737 pendingRemoval += length;
5838 /* Always erase a minimum of 1/32th the current backpressure */
5939 if (pendingRemoval > (buffer.length () >> 5 )) {
60- normalize ();
40+ std::string (buffer.begin () + pendingRemoval, buffer.end ()).swap (buffer);
41+ pendingRemoval = 0 ;
6142 }
6243 }
6344 size_t length () {
6445 return buffer.length () - pendingRemoval;
6546 }
66- /* Only used in AsyncSocket::write when buffered backpressure fully drains */
47+ /* Only used in AsyncSocket::write - what about replacing it with the other functions like erase(length())? */
6748 void clear () {
6849 pendingRemoval = 0 ;
69- if (buffer.capacity () > TRIM_THRESHOLD ) {
70- /* Trim pathological spikes but keep a warm buffer for normal bursts */
71- std::string retained;
72- retained.reserve (RETAIN_CAPACITY );
73- buffer.swap (retained);
74- } else {
75- buffer.clear ();
76- }
50+ buffer.clear ();
51+ buffer.shrink_to_fit ();
7752 }
7853 /* Only used by AsyncSocket::write (optionally) before append */
7954 void reserve (size_t length) {
8055 buffer.reserve (length + pendingRemoval);
8156 }
8257 /* Only used by getSendBuffer as last resort */
8358 void resize (size_t length) {
84- if (pendingRemoval) {
85- normalize ();
86- }
87- buffer.resize (length);
59+ buffer.resize (length + pendingRemoval);
8860 }
8961 const char *data () {
9062 return buffer.data () + pendingRemoval;
0 commit comments