protocol-zmtp 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a8774efa1927a863efbec0173b00b4642fe99f809ee0df07e3ab6a164829909e
4
- data.tar.gz: a77071e52f4461323f7d839738ad56d0ed83af312ae1071b4f88ffb365ee2514
3
+ metadata.gz: f6f3ddb84f7c69bf776e615da15797cb815eaf663f357cf16cff60c744153bc8
4
+ data.tar.gz: fd410e2dc417e623254be530230e88fc3e428f62413d6465ee88465c82444b10
5
5
  SHA512:
6
- metadata.gz: c0cc5712a1c5aabd5f360ea5fe052bb45e963f645551f0bea2a46f62d3d43d4f9f8a0a0cd3a2de63e563641d2463586b0574016db6ab394a66b78a0c7bd93924
7
- data.tar.gz: 87ae97e2a6bc9448b16a9053d959e7b7707639bb15ad168bc020f83eae0d95309b82be6a0772a38d6e56307038de013d7544e836094c5d52b4dfb114175b5a28
6
+ metadata.gz: 0c84020c35c81c9b9609541019cb7433956bfed7a2bf8fd4ea615ad7560b9aecec5086efbab733576309ff7cb2febb4f5c98bef2388f55a056bcf38c78a0633d
7
+ data.tar.gz: 7251f30d057e56f97e6ffa3eb95af5727d7763a04829b3c93580e36998b1aa2591cdc0152ff4d53e7764ea80f75113585513d537147c3de238b498d50f25d15c
@@ -52,6 +52,11 @@ module Protocol
52
52
  @mutex = Mutex.new
53
53
  @max_message_size = max_message_size
54
54
  @last_received_at = nil
55
+
56
+ # Reusable scratch buffer for frame headers. Array#pack(buffer:)
57
+ # writes in place so the per-message 2-or-9 byte String allocation
58
+ # in write_frames disappears on the hot send path.
59
+ @header_buf = String.new(capacity: 9, encoding: Encoding::BINARY)
55
60
  end
56
61
 
57
62
 
@@ -109,6 +114,27 @@ module Protocol
109
114
  end
110
115
 
111
116
 
117
+ # Writes a batch of multi-frame messages to the buffer under a
118
+ # single mutex acquisition. Used by work-stealing send pumps that
119
+ # dequeue up to N messages at once — avoids the N lock/unlock
120
+ # pairs per batch that a plain `batch.each { write_message }`
121
+ # would incur.
122
+ #
123
+ # @param messages [Array<Array<String>>] each element is one
124
+ # multi-frame message
125
+ # @return [void]
126
+ def write_messages(messages)
127
+ @mutex.synchronize do
128
+ i = 0
129
+ n = messages.size
130
+ while i < n
131
+ write_frames(messages[i])
132
+ i += 1
133
+ end
134
+ end
135
+ end
136
+
137
+
112
138
  # Writes pre-encoded wire bytes to the buffer without flushing.
113
139
  # Used for fan-out: encode once, write to many connections.
114
140
  #
@@ -241,13 +267,32 @@ module Protocol
241
267
  private
242
268
 
243
269
  # Writes message parts as ZMTP frames, encrypting if needed.
270
+ #
271
+ # For the unencrypted path, writes the frame header and body
272
+ # separately to the IO instead of allocating a wire String. This
273
+ # avoids copying the body just to glue a 1- or 9-byte header onto
274
+ # it -- significant for large messages where the body copy was
275
+ # the dominant allocation per send.
244
276
  def write_frames(parts)
277
+ encrypted = @mechanism.encrypted?
278
+ buf = @header_buf
279
+
245
280
  parts.each_with_index do |part, i|
246
281
  more = i < parts.size - 1
247
- if @mechanism.encrypted?
282
+ if encrypted
248
283
  @io.write(@mechanism.encrypt(part.b, more: more))
249
284
  else
250
- @io.write(Codec::Frame.new(part, more: more).to_wire)
285
+ body = part.b
286
+ size = body.bytesize
287
+ flags = more ? Codec::Frame::FLAGS_MORE : 0
288
+ buf.clear
289
+ if size > Codec::Frame::SHORT_MAX
290
+ [flags | Codec::Frame::FLAGS_LONG, size].pack("CQ>", buffer: buf)
291
+ else
292
+ [flags, size].pack("CC", buffer: buf)
293
+ end
294
+ @io.write(buf)
295
+ @io.write(body)
251
296
  end
252
297
  end
253
298
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Protocol
4
4
  module ZMTP
5
- VERSION = "0.3.0"
5
+ VERSION = "0.4.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protocol-zmtp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrik Wenger