polyphony 0.93 → 0.94

Sign up to get free protection for your applications and to get access to all the features.
@@ -359,42 +359,16 @@ static inline int fd_from_io(VALUE io, rb_io_t **fptr, int write_mode, int recti
359
359
  }
360
360
  }
361
361
 
362
- VALUE Backend_read(VALUE self, VALUE io, VALUE str, VALUE length, VALUE to_eof, VALUE pos) {
362
+ VALUE Backend_read(VALUE self, VALUE io, VALUE buffer, VALUE length, VALUE to_eof, VALUE pos) {
363
363
  Backend_t *backend;
364
364
  int fd;
365
365
  rb_io_t *fptr;
366
- struct io_buffer buffer = get_io_buffer(str, 0);
367
- long buf_pos = FIX2INT(pos);
368
- int shrinkable_string = 0;
369
- int expandable_buffer = 0;
366
+ struct backend_buffer_spec buffer_spec = backend_get_buffer_spec(buffer, 0);
370
367
  long total = 0;
371
368
  int read_to_eof = RTEST(to_eof);
372
369
 
373
- if (buffer.raw) {
374
- if (buf_pos < 0 || buf_pos > buffer.len) buf_pos = buffer.len;
375
- buffer.ptr += buf_pos;
376
- buffer.len -= buf_pos;
377
- }
378
- else {
379
- expandable_buffer = length == Qnil;
380
- long expected_read_length = expandable_buffer ? 4096 : FIX2INT(length);
381
- long string_cap = rb_str_capacity(str);
382
- if (buf_pos < 0 || buf_pos > buffer.len) buf_pos = buffer.len;
383
-
384
- if (string_cap < expected_read_length + buf_pos) {
385
- shrinkable_string = io_setstrbuf(&str, expected_read_length + buf_pos);
386
- buffer.ptr = (unsigned char *)RSTRING_PTR(str) + buf_pos;
387
- buffer.len = expected_read_length;
388
- }
389
- else {
390
- buffer.ptr += buf_pos;
391
- buffer.len = string_cap - buf_pos;
392
- if (buffer.len > expected_read_length)
393
- buffer.len = expected_read_length;
394
- }
395
- }
396
-
397
370
  GetBackend(self, backend);
371
+ backend_prepare_read_buffer(buffer, length, &buffer_spec, FIX2INT(pos));
398
372
  fd = fd_from_io(io, &fptr, 0, 1);
399
373
 
400
374
  while (1) {
@@ -404,12 +378,12 @@ VALUE Backend_read(VALUE self, VALUE io, VALUE str, VALUE length, VALUE to_eof,
404
378
  int result;
405
379
  int completed;
406
380
 
407
- io_uring_prep_read(sqe, fd, buffer.ptr, buffer.len, -1);
381
+ io_uring_prep_read(sqe, fd, buffer_spec.ptr, buffer_spec.len, -1);
408
382
 
409
383
  result = io_uring_backend_defer_submit_and_await(backend, sqe, ctx, &resume_value);
410
384
  completed = context_store_release(&backend->store, ctx);
411
385
  if (!completed) {
412
- context_attach_buffers(ctx, 1, &str);
386
+ context_attach_buffers(ctx, 1, &buffer);
413
387
  RAISE_IF_EXCEPTION(resume_value);
414
388
  return resume_value;
415
389
  }
@@ -423,42 +397,35 @@ VALUE Backend_read(VALUE self, VALUE io, VALUE str, VALUE length, VALUE to_eof,
423
397
  total += result;
424
398
  if (!read_to_eof) break;
425
399
 
426
- if (result == buffer.len) {
427
- if (!expandable_buffer) break;
428
-
429
- // resize buffer to double its capacity
430
- rb_str_resize(str, total + buf_pos);
431
- rb_str_modify_expand(str, rb_str_capacity(str));
432
- shrinkable_string = 0;
433
- buffer.ptr = (unsigned char *)RSTRING_PTR(str) + total + buf_pos;
434
- buffer.len = rb_str_capacity(str) - total - buf_pos;
400
+ if (result == buffer_spec.len) {
401
+ if (buffer_spec.expandable)
402
+ backend_grow_string_buffer(buffer, &buffer_spec, total);
403
+ else
404
+ break;
435
405
  }
436
406
  else {
437
- buffer.ptr += result;
438
- buffer.len -= result;
439
- if (!buffer.len) break;
407
+ buffer_spec.ptr += result;
408
+ buffer_spec.len -= result;
409
+ if (!buffer_spec.len) break;
440
410
  }
441
411
  }
442
412
  }
443
413
 
444
- if (!buffer.raw) {
445
- io_set_read_length(str, buf_pos + total, shrinkable_string);
446
- if (fptr) io_enc_str(str, fptr);
447
- }
448
414
  if (!total) return Qnil;
449
-
450
- return buffer.raw ? INT2FIX(total) : str;
415
+
416
+ if (!buffer_spec.raw) backend_finalize_string_buffer(buffer, &buffer_spec, total, fptr);
417
+ return buffer_spec.raw ? INT2FIX(total) : buffer;
451
418
  }
452
419
 
453
420
  VALUE Backend_read_loop(VALUE self, VALUE io, VALUE maxlen) {
454
421
  Backend_t *backend;
455
422
  int fd;
456
423
  rb_io_t *fptr;
457
- VALUE str;
424
+ VALUE buffer;
458
425
  long total;
426
+ char *ptr;
459
427
  long len = FIX2INT(maxlen);
460
428
  int shrinkable;
461
- char *buf;
462
429
 
463
430
  READ_LOOP_PREPARE_STR();
464
431
 
@@ -472,12 +439,12 @@ VALUE Backend_read_loop(VALUE self, VALUE io, VALUE maxlen) {
472
439
  ssize_t result;
473
440
  int completed;
474
441
 
475
- io_uring_prep_read(sqe, fd, buf, len, -1);
442
+ io_uring_prep_read(sqe, fd, ptr, len, -1);
476
443
 
477
444
  result = io_uring_backend_defer_submit_and_await(backend, sqe, ctx, &resume_value);
478
445
  completed = context_store_release(&backend->store, ctx);
479
446
  if (!completed) {
480
- context_attach_buffers(ctx, 1, &str);
447
+ context_attach_buffers(ctx, 1, &buffer);
481
448
  RAISE_IF_EXCEPTION(resume_value);
482
449
  return resume_value;
483
450
  }
@@ -493,7 +460,7 @@ VALUE Backend_read_loop(VALUE self, VALUE io, VALUE maxlen) {
493
460
  }
494
461
  }
495
462
 
496
- RB_GC_GUARD(str);
463
+ RB_GC_GUARD(buffer);
497
464
 
498
465
  return io;
499
466
  }
@@ -502,11 +469,11 @@ VALUE Backend_feed_loop(VALUE self, VALUE io, VALUE receiver, VALUE method) {
502
469
  Backend_t *backend;
503
470
  int fd;
504
471
  rb_io_t *fptr;
505
- VALUE str;
472
+ VALUE buffer;
506
473
  long total;
474
+ char *ptr;
507
475
  long len = 8192;
508
476
  int shrinkable;
509
- char *buf;
510
477
  ID method_id = SYM2ID(method);
511
478
 
512
479
  READ_LOOP_PREPARE_STR();
@@ -521,12 +488,12 @@ VALUE Backend_feed_loop(VALUE self, VALUE io, VALUE receiver, VALUE method) {
521
488
  ssize_t result;
522
489
  int completed;
523
490
 
524
- io_uring_prep_read(sqe, fd, buf, len, -1);
491
+ io_uring_prep_read(sqe, fd, ptr, len, -1);
525
492
 
526
493
  result = io_uring_backend_defer_submit_and_await(backend, sqe, ctx, &resume_value);
527
494
  completed = context_store_release(&backend->store, ctx);
528
495
  if (!completed) {
529
- context_attach_buffers(ctx, 1, &str);
496
+ context_attach_buffers(ctx, 1, &buffer);
530
497
  RAISE_IF_EXCEPTION(resume_value);
531
498
  return resume_value;
532
499
  }
@@ -542,18 +509,18 @@ VALUE Backend_feed_loop(VALUE self, VALUE io, VALUE receiver, VALUE method) {
542
509
  }
543
510
  }
544
511
 
545
- RB_GC_GUARD(str);
512
+ RB_GC_GUARD(buffer);
546
513
 
547
514
  return io;
548
515
  }
549
516
 
550
- VALUE Backend_write(VALUE self, VALUE io, VALUE str) {
517
+ VALUE Backend_write(VALUE self, VALUE io, VALUE buffer) {
551
518
  Backend_t *backend;
552
519
  int fd;
553
520
  rb_io_t *fptr;
554
521
 
555
- struct io_buffer buffer = get_io_buffer(str, 1);
556
- long left = buffer.len;
522
+ struct backend_buffer_spec buffer_spec = backend_get_buffer_spec(buffer, 1);
523
+ long left = buffer_spec.len;
557
524
 
558
525
  GetBackend(self, backend);
559
526
  fd = fd_from_io(io, &fptr, 1, 0);
@@ -565,12 +532,12 @@ VALUE Backend_write(VALUE self, VALUE io, VALUE str) {
565
532
  int result;
566
533
  int completed;
567
534
 
568
- io_uring_prep_write(sqe, fd, buffer.ptr, left, -1);
535
+ io_uring_prep_write(sqe, fd, buffer_spec.ptr, left, -1);
569
536
 
570
537
  result = io_uring_backend_defer_submit_and_await(backend, sqe, ctx, &resume_value);
571
538
  completed = context_store_release(&backend->store, ctx);
572
539
  if (!completed) {
573
- context_attach_buffers(ctx, 1, &str);
540
+ context_attach_buffers(ctx, 1, &buffer);
574
541
  RAISE_IF_EXCEPTION(resume_value);
575
542
  return resume_value;
576
543
  }
@@ -579,12 +546,12 @@ VALUE Backend_write(VALUE self, VALUE io, VALUE str) {
579
546
  if (result < 0)
580
547
  rb_syserr_fail(-result, strerror(-result));
581
548
  else {
582
- buffer.ptr += result;
549
+ buffer_spec.ptr += result;
583
550
  left -= result;
584
551
  }
585
552
  }
586
553
 
587
- return INT2FIX(buffer.len);
554
+ return INT2FIX(buffer_spec.len);
588
555
  }
589
556
 
590
557
  VALUE Backend_writev(VALUE self, VALUE io, int argc, VALUE *argv) {
@@ -602,9 +569,9 @@ VALUE Backend_writev(VALUE self, VALUE io, int argc, VALUE *argv) {
602
569
 
603
570
  iov = malloc(iov_count * sizeof(struct iovec));
604
571
  for (int i = 0; i < argc; i++) {
605
- VALUE str = argv[i];
606
- iov[i].iov_base = StringValuePtr(str);
607
- iov[i].iov_len = RSTRING_LEN(str);
572
+ VALUE buffer = argv[i];
573
+ iov[i].iov_base = StringValuePtr(buffer);
574
+ iov[i].iov_len = RSTRING_LEN(buffer);
608
575
  total_length += iov[i].iov_len;
609
576
  }
610
577
  iov_ptr = iov;
@@ -664,41 +631,15 @@ VALUE Backend_write_m(int argc, VALUE *argv, VALUE self) {
664
631
  Backend_writev(self, argv[0], argc - 1, argv + 1);
665
632
  }
666
633
 
667
- VALUE Backend_recv(VALUE self, VALUE io, VALUE str, VALUE length, VALUE pos) {
634
+ VALUE Backend_recv(VALUE self, VALUE io, VALUE buffer, VALUE length, VALUE pos) {
668
635
  Backend_t *backend;
669
636
  int fd;
670
637
  rb_io_t *fptr;
671
- struct io_buffer buffer = get_io_buffer(str, 0);
672
- long buf_pos = FIX2INT(pos);
673
- int shrinkable_string = 0;
674
- int expandable_buffer = 0;
638
+ struct backend_buffer_spec buffer_spec = backend_get_buffer_spec(buffer, 0);
675
639
  long total = 0;
676
640
 
677
- if (buffer.raw) {
678
- if (buf_pos < 0 || buf_pos > buffer.len) buf_pos = buffer.len;
679
- buffer.ptr += buf_pos;
680
- buffer.len -= buf_pos;
681
- }
682
- else {
683
- expandable_buffer = length == Qnil;
684
- long expected_read_length = expandable_buffer ? 4096 : FIX2INT(length);
685
- long string_cap = rb_str_capacity(str);
686
- if (buf_pos < 0 || buf_pos > buffer.len) buf_pos = buffer.len;
687
-
688
- if (string_cap < expected_read_length + buf_pos) {
689
- shrinkable_string = io_setstrbuf(&str, expected_read_length + buf_pos);
690
- buffer.ptr = (unsigned char *)RSTRING_PTR(str) + buf_pos;
691
- buffer.len = expected_read_length;
692
- }
693
- else {
694
- buffer.ptr += buf_pos;
695
- buffer.len = string_cap - buf_pos;
696
- if (buffer.len > expected_read_length)
697
- buffer.len = expected_read_length;
698
- }
699
- }
700
-
701
641
  GetBackend(self, backend);
642
+ backend_prepare_read_buffer(buffer, length, &buffer_spec, FIX2INT(pos));
702
643
  fd = fd_from_io(io, &fptr, 0, 0);
703
644
 
704
645
  while (1) {
@@ -708,12 +649,12 @@ VALUE Backend_recv(VALUE self, VALUE io, VALUE str, VALUE length, VALUE pos) {
708
649
  int result;
709
650
  int completed;
710
651
 
711
- io_uring_prep_recv(sqe, fd, buffer.ptr, buffer.len, 0);
652
+ io_uring_prep_recv(sqe, fd, buffer_spec.ptr, buffer_spec.len, 0);
712
653
 
713
654
  result = io_uring_backend_defer_submit_and_await(backend, sqe, ctx, &resume_value);
714
655
  completed = context_store_release(&backend->store, ctx);
715
656
  if (!completed) {
716
- context_attach_buffers(ctx, 1, &str);
657
+ context_attach_buffers(ctx, 1, &buffer);
717
658
  RAISE_IF_EXCEPTION(resume_value);
718
659
  return resume_value;
719
660
  }
@@ -727,24 +668,21 @@ VALUE Backend_recv(VALUE self, VALUE io, VALUE str, VALUE length, VALUE pos) {
727
668
  }
728
669
  }
729
670
 
730
- if (!buffer.raw) {
731
- io_set_read_length(str, buf_pos + total, shrinkable_string);
732
- if (fptr) io_enc_str(str, fptr);
733
- }
734
671
  if (!total) return Qnil;
735
672
 
736
- return buffer.raw ? INT2FIX(total) : str;
673
+ if (!buffer_spec.raw) backend_finalize_string_buffer(buffer, &buffer_spec, total, fptr);
674
+ return buffer_spec.raw ? INT2FIX(total) : buffer;
737
675
  }
738
676
 
739
677
  VALUE Backend_recv_loop(VALUE self, VALUE io, VALUE maxlen) {
740
678
  Backend_t *backend;
741
679
  int fd;
742
680
  rb_io_t *fptr;
743
- VALUE str;
681
+ VALUE buffer;
744
682
  long total;
683
+ char *ptr;
745
684
  long len = FIX2INT(maxlen);
746
685
  int shrinkable;
747
- char *buf;
748
686
 
749
687
  READ_LOOP_PREPARE_STR();
750
688
 
@@ -758,12 +696,12 @@ VALUE Backend_recv_loop(VALUE self, VALUE io, VALUE maxlen) {
758
696
  int result;
759
697
  int completed;
760
698
 
761
- io_uring_prep_recv(sqe, fd, buf, len, 0);
699
+ io_uring_prep_recv(sqe, fd, ptr, len, 0);
762
700
 
763
701
  result = io_uring_backend_defer_submit_and_await(backend, sqe, ctx, &resume_value);
764
702
  completed = context_store_release(&backend->store, ctx);
765
703
  if (!completed) {
766
- context_attach_buffers(ctx, 1, &str);
704
+ context_attach_buffers(ctx, 1, &buffer);
767
705
  RAISE_IF_EXCEPTION(resume_value);
768
706
  return resume_value;
769
707
  }
@@ -779,7 +717,7 @@ VALUE Backend_recv_loop(VALUE self, VALUE io, VALUE maxlen) {
779
717
  }
780
718
  }
781
719
 
782
- RB_GC_GUARD(str);
720
+ RB_GC_GUARD(buffer);
783
721
  return io;
784
722
  }
785
723
 
@@ -787,11 +725,11 @@ VALUE Backend_recv_feed_loop(VALUE self, VALUE io, VALUE receiver, VALUE method)
787
725
  Backend_t *backend;
788
726
  int fd;
789
727
  rb_io_t *fptr;
790
- VALUE str;
728
+ VALUE buffer;
791
729
  long total;
730
+ char *ptr;
792
731
  long len = 8192;
793
732
  int shrinkable;
794
- char *buf;
795
733
  ID method_id = SYM2ID(method);
796
734
 
797
735
  READ_LOOP_PREPARE_STR();
@@ -806,12 +744,12 @@ VALUE Backend_recv_feed_loop(VALUE self, VALUE io, VALUE receiver, VALUE method)
806
744
  int result;
807
745
  int completed;
808
746
 
809
- io_uring_prep_recv(sqe, fd, buf, len, 0);
747
+ io_uring_prep_recv(sqe, fd, ptr, len, 0);
810
748
 
811
749
  result = io_uring_backend_defer_submit_and_await(backend, sqe, ctx, &resume_value);
812
750
  completed = context_store_release(&backend->store, ctx);
813
751
  if (!completed) {
814
- context_attach_buffers(ctx, 1, &str);
752
+ context_attach_buffers(ctx, 1, &buffer);
815
753
  RAISE_IF_EXCEPTION(resume_value);
816
754
  return resume_value;
817
755
  }
@@ -827,17 +765,17 @@ VALUE Backend_recv_feed_loop(VALUE self, VALUE io, VALUE receiver, VALUE method)
827
765
  }
828
766
  }
829
767
 
830
- RB_GC_GUARD(str);
768
+ RB_GC_GUARD(buffer);
831
769
  return io;
832
770
  }
833
771
 
834
- VALUE Backend_send(VALUE self, VALUE io, VALUE str, VALUE flags) {
772
+ VALUE Backend_send(VALUE self, VALUE io, VALUE buffer, VALUE flags) {
835
773
  Backend_t *backend;
836
774
  int fd;
837
775
  rb_io_t *fptr;
838
776
 
839
- struct io_buffer buffer = get_io_buffer(str, 1);
840
- long left = buffer.len;
777
+ struct backend_buffer_spec buffer_spec = backend_get_buffer_spec(buffer, 1);
778
+ long left = buffer_spec.len;
841
779
  int flags_int = FIX2INT(flags);
842
780
 
843
781
  GetBackend(self, backend);
@@ -850,12 +788,12 @@ VALUE Backend_send(VALUE self, VALUE io, VALUE str, VALUE flags) {
850
788
  int result;
851
789
  int completed;
852
790
 
853
- io_uring_prep_send(sqe, fd, buffer.ptr, left, flags_int);
791
+ io_uring_prep_send(sqe, fd, buffer_spec.ptr, left, flags_int);
854
792
 
855
793
  result = io_uring_backend_defer_submit_and_await(backend, sqe, ctx, &resume_value);
856
794
  completed = context_store_release(&backend->store, ctx);
857
795
  if (!completed) {
858
- context_attach_buffers(ctx, 1, &str);
796
+ context_attach_buffers(ctx, 1, &buffer);
859
797
  RAISE_IF_EXCEPTION(resume_value);
860
798
  return resume_value;
861
799
  }
@@ -864,12 +802,12 @@ VALUE Backend_send(VALUE self, VALUE io, VALUE str, VALUE flags) {
864
802
  if (result < 0)
865
803
  rb_syserr_fail(-result, strerror(-result));
866
804
  else {
867
- buffer.ptr += result;
805
+ buffer_spec.ptr += result;
868
806
  left -= result;
869
807
  }
870
808
  }
871
809
 
872
- return INT2FIX(buffer.len);
810
+ return INT2FIX(buffer_spec.len);
873
811
  }
874
812
 
875
813
  VALUE io_uring_backend_accept(Backend_t *backend, VALUE server_socket, VALUE socket_class, int loop) {
@@ -1406,18 +1344,18 @@ VALUE Backend_kind(VALUE self) {
1406
1344
  return SYM_io_uring;
1407
1345
  }
1408
1346
 
1409
- struct io_uring_sqe *Backend_chain_prepare_write(Backend_t *backend, VALUE io, VALUE str) {
1347
+ struct io_uring_sqe *Backend_chain_prepare_write(Backend_t *backend, VALUE io, VALUE buffer) {
1410
1348
  int fd;
1411
1349
  rb_io_t *fptr;
1412
1350
  struct io_uring_sqe *sqe;
1413
1351
 
1414
1352
  fd = fd_from_io(io, &fptr, 1, 0);
1415
1353
  sqe = io_uring_backend_get_sqe(backend);
1416
- io_uring_prep_write(sqe, fd, StringValuePtr(str), RSTRING_LEN(str), 0);
1354
+ io_uring_prep_write(sqe, fd, StringValuePtr(buffer), RSTRING_LEN(buffer), 0);
1417
1355
  return sqe;
1418
1356
  }
1419
1357
 
1420
- struct io_uring_sqe *Backend_chain_prepare_send(Backend_t *backend, VALUE io, VALUE str, VALUE flags) {
1358
+ struct io_uring_sqe *Backend_chain_prepare_send(Backend_t *backend, VALUE io, VALUE buffer, VALUE flags) {
1421
1359
  int fd;
1422
1360
  rb_io_t *fptr;
1423
1361
  struct io_uring_sqe *sqe;
@@ -1425,7 +1363,7 @@ struct io_uring_sqe *Backend_chain_prepare_send(Backend_t *backend, VALUE io, VA
1425
1363
  fd = fd_from_io(io, &fptr, 1, 0);
1426
1364
 
1427
1365
  sqe = io_uring_backend_get_sqe(backend);
1428
- io_uring_prep_send(sqe, fd, StringValuePtr(str), RSTRING_LEN(str), FIX2INT(flags));
1366
+ io_uring_prep_send(sqe, fd, StringValuePtr(buffer), RSTRING_LEN(buffer), FIX2INT(flags));
1429
1367
  return sqe;
1430
1368
  }
1431
1369
 
@@ -1560,9 +1498,9 @@ inline VALUE Backend_run_idle_tasks(VALUE self) {
1560
1498
  return self;
1561
1499
  }
1562
1500
 
1563
- static inline void splice_chunks_prep_write(op_context_t *ctx, struct io_uring_sqe *sqe, int fd, VALUE str) {
1564
- char *buf = RSTRING_PTR(str);
1565
- int len = RSTRING_LEN(str);
1501
+ static inline void splice_chunks_prep_write(op_context_t *ctx, struct io_uring_sqe *sqe, int fd, VALUE buffer) {
1502
+ char *buf = RSTRING_PTR(buffer);
1503
+ int len = RSTRING_LEN(buffer);
1566
1504
  io_uring_prep_write(sqe, fd, buf, len, 0);
1567
1505
  // io_uring_prep_send(sqe, fd, buf, len, 0);
1568
1506
  io_uring_sqe_set_data(sqe, ctx);
@@ -1629,7 +1567,6 @@ VALUE Backend_splice_chunks(VALUE self, VALUE src, VALUE dest, VALUE prefix, VAL
1629
1567
  op_context_t *ctx = 0;
1630
1568
  struct io_uring_sqe *sqe = 0;
1631
1569
  int maxlen;
1632
- VALUE str = Qnil;
1633
1570
  VALUE chunk_len_value = Qnil;
1634
1571
  int src_fd;
1635
1572
  int dest_fd;
@@ -1701,7 +1638,6 @@ VALUE Backend_splice_chunks(VALUE self, VALUE src, VALUE dest, VALUE prefix, VAL
1701
1638
  SPLICE_CHUNKS_AWAIT_OPS(backend, &ctx, 0, &switchpoint_result);
1702
1639
  }
1703
1640
 
1704
- RB_GC_GUARD(str);
1705
1641
  RB_GC_GUARD(chunk_len_value);
1706
1642
  RB_GC_GUARD(switchpoint_result);
1707
1643
  if (pipefd[0] != -1) close(pipefd[0]);