Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[GH #828] Optimize print_n/i and say_n/i

print_n/nc/i/ic and the say variants go through a full Parrot_io_printf with
3 va_start/va_end scenarios and a custom format string parser,
and through char* -> STRING->char* conversion until the fixed
FLOATVAL_FMT or INTVAL_FMT printf is done.
Just to print a single int or number.

This also lost precision for non-double floatval's - if cast to (double).
Now avoided.
  • Loading branch information...
commit 29582199aef9ea94622b4202aee129521bc649a1 1 parent 6f0e533
@rurban rurban authored
Showing with 141 additions and 38 deletions.
  1. +102 −20 src/ops/core_ops.c
  2. +39 −18 src/ops/io.ops
View
122 src/ops/core_ops.c
@@ -16844,27 +16844,57 @@ Parrot_xor_p_p_p(opcode_t *cur_opcode, PARROT_INTERP) {
opcode_t *
Parrot_print_i(opcode_t *cur_opcode, PARROT_INTERP) {
- Parrot_io_printf(interp, INTVAL_FMT, (INTVAL)IREG(1));
+ char buf[128];
+
+ #if defined(PARROT_HAS_SNPRINTF)
+ snprintf(buf, 128, INTVAL_FMT, (INTVAL)IREG(1));
+
+#else
+ sprintf(buf, INTVAL_FMT, (INTVAL)IREG(1));
+
+#endif
+;
+ Parrot_io_write_b(interp, _PIO_STDOUT(interp), buf, strlen(buf));
return cur_opcode + 2;
}
opcode_t *
Parrot_print_ic(opcode_t *cur_opcode, PARROT_INTERP) {
- Parrot_io_printf(interp, INTVAL_FMT, (INTVAL)ICONST(1));
+ char buf[128];
+
+ #if defined(PARROT_HAS_SNPRINTF)
+ snprintf(buf, 128, INTVAL_FMT, (INTVAL)ICONST(1));
+
+#else
+ sprintf(buf, INTVAL_FMT, (INTVAL)ICONST(1));
+
+#endif
+;
+ Parrot_io_write_b(interp, _PIO_STDOUT(interp), buf, strlen(buf));
return cur_opcode + 2;
}
opcode_t *
Parrot_print_n(opcode_t *cur_opcode, PARROT_INTERP) {
+ char buf[128];
+
+ #if defined(PARROT_HAS_SNPRINTF)
+ snprintf(buf, 128, FLOATVAL_FMT, NREG(1));
+
+#else
+ sprintf(buf, FLOATVAL_FMT, NREG(1));
+
+#endif
+;
#if defined(PARROT_HAS_NEGATIVE_ZERO)
- Parrot_io_printf(interp, FLOATVAL_FMT, NREG(1));
+ Parrot_io_write_b(interp, _PIO_STDOUT(interp), buf, strlen(buf));
#else
- if (Parrot_is_nzero(NREG(1))) {
- Parrot_io_printf(interp, "-0");
+ if ((!Parrot_is_nzero(NREG(1)))) {
+ Parrot_io_write_b(interp, _PIO_STDOUT(interp), buf, strlen(buf));
}
else {
- Parrot_io_printf(interp, FLOATVAL_FMT, NREG(1));
+ Parrot_io_write_b(interp, _PIO_STDOUT(interp), "-0", 2);
}
#endif
@@ -16874,15 +16904,25 @@ Parrot_print_n(opcode_t *cur_opcode, PARROT_INTERP) {
opcode_t *
Parrot_print_nc(opcode_t *cur_opcode, PARROT_INTERP) {
+ char buf[128];
+
+ #if defined(PARROT_HAS_SNPRINTF)
+ snprintf(buf, 128, FLOATVAL_FMT, NCONST(1));
+
+#else
+ sprintf(buf, FLOATVAL_FMT, NCONST(1));
+
+#endif
+;
#if defined(PARROT_HAS_NEGATIVE_ZERO)
- Parrot_io_printf(interp, FLOATVAL_FMT, NCONST(1));
+ Parrot_io_write_b(interp, _PIO_STDOUT(interp), buf, strlen(buf));
#else
- if (Parrot_is_nzero(NCONST(1))) {
- Parrot_io_printf(interp, "-0");
+ if ((!Parrot_is_nzero(NREG(1)))) {
+ Parrot_io_write_b(interp, _PIO_STDOUT(interp), buf, strlen(buf));
}
else {
- Parrot_io_printf(interp, FLOATVAL_FMT, NCONST(1));
+ Parrot_io_write_b(interp, _PIO_STDOUT(interp), "-0", 2);
}
#endif
@@ -16926,27 +16966,58 @@ Parrot_print_p(opcode_t *cur_opcode, PARROT_INTERP) {
opcode_t *
Parrot_say_i(opcode_t *cur_opcode, PARROT_INTERP) {
- Parrot_io_printf(interp, INTVAL_FMT "\n", (INTVAL)IREG(1));
+ char buf[128];
+
+ #if defined(PARROT_HAS_SNPRINTF)
+ snprintf(buf, 128, INTVAL_FMT "\n", (INTVAL)IREG(1));
+
+#else
+ sprintf(buf, INTVAL_FMT "\n", (INTVAL)IREG(1));
+
+#endif
+;
+ Parrot_io_write_b(interp, _PIO_STDOUT(interp), buf, strlen(buf));
return cur_opcode + 2;
}
opcode_t *
Parrot_say_ic(opcode_t *cur_opcode, PARROT_INTERP) {
- Parrot_io_printf(interp, INTVAL_FMT "\n", (INTVAL)ICONST(1));
+ char buf[128];
+
+ #if defined(PARROT_HAS_SNPRINTF)
+ snprintf(buf, 128, INTVAL_FMT "\n", (INTVAL)ICONST(1));
+
+#else
+ sprintf(buf, INTVAL_FMT "\n", (INTVAL)ICONST(1));
+
+#endif
+;
+ Parrot_io_write_b(interp, _PIO_STDOUT(interp), buf, strlen(buf));
return cur_opcode + 2;
}
opcode_t *
Parrot_say_n(opcode_t *cur_opcode, PARROT_INTERP) {
+ char buf[128];
+ size_t ignore;
+
+ #if defined(PARROT_HAS_SNPRINTF)
+ snprintf(buf, 128, FLOATVAL_FMT "\n", NREG(1));
+
+#else
+ sprintf(buf, 128, FLOATVAL_FMT, NREG(1));
+
+#endif
+;
#if defined(PARROT_HAS_NEGATIVE_ZERO)
- Parrot_io_printf(interp, FLOATVAL_FMT "\n", NREG(1));
+ Parrot_io_write_b(interp, _PIO_STDOUT(interp), buf, strlen(buf));
#else
- if (Parrot_is_nzero(NREG(1))) {
- Parrot_io_printf(interp, "-0\n");
+ if ((!Parrot_is_nzero(NREG(1)))) {
+ Parrot_io_write_b(interp, _PIO_STDOUT(interp), buf, strlen(buf));
}
else {
- Parrot_io_printf(interp, FLOATVAL_FMT "\n", NREG(1));
+ Parrot_io_write_b(interp, _PIO_STDOUT(interp), "-0\n", 2);
}
#endif
@@ -16956,15 +17027,26 @@ Parrot_say_n(opcode_t *cur_opcode, PARROT_INTERP) {
opcode_t *
Parrot_say_nc(opcode_t *cur_opcode, PARROT_INTERP) {
+ char buf[128];
+ size_t ignore;
+
+ #if defined(PARROT_HAS_SNPRINTF)
+ snprintf(buf, 128, FLOATVAL_FMT "\n", NCONST(1));
+
+#else
+ sprintf(buf, 128, FLOATVAL_FMT, NCONST(1));
+
+#endif
+;
#if defined(PARROT_HAS_NEGATIVE_ZERO)
- Parrot_io_printf(interp, FLOATVAL_FMT "\n", NCONST(1));
+ Parrot_io_write_b(interp, _PIO_STDOUT(interp), buf, strlen(buf));
#else
- if (Parrot_is_nzero(NCONST(1))) {
- Parrot_io_printf(interp, "-0\n");
+ if ((!Parrot_is_nzero(NREG(1)))) {
+ Parrot_io_write_b(interp, _PIO_STDOUT(interp), buf, strlen(buf));
}
else {
- Parrot_io_printf(interp, FLOATVAL_FMT "\n", NCONST(1));
+ Parrot_io_write_b(interp, _PIO_STDOUT(interp), "-0\n", 2);
}
#endif
View
57 src/ops/io.ops
@@ -46,20 +46,30 @@ Print $1 to standard output.
=cut
inline op print(in INT) :base_io {
- Parrot_io_printf(interp, INTVAL_FMT, (INTVAL)$1);
+ char buf[128];
+#ifdef PARROT_HAS_SNPRINTF
+ snprintf(buf, 128, INTVAL_FMT, (INTVAL)$1);
+#else
+ sprintf(buf, INTVAL_FMT, (INTVAL)$1);
+#endif
+ Parrot_io_write_b(interp, _PIO_STDOUT(interp), buf, strlen(buf));
}
inline op print(in NUM) :base_io {
+ char buf[128];
+#ifdef PARROT_HAS_SNPRINTF
+ snprintf(buf, 128, FLOATVAL_FMT, $1);
+#else
+ sprintf(buf, FLOATVAL_FMT, $1);
+#endif
#ifdef PARROT_HAS_NEGATIVE_ZERO
- Parrot_io_printf(interp, FLOATVAL_FMT, $1);
+ Parrot_io_write_b(interp, _PIO_STDOUT(interp), buf, strlen(buf));
#else
- /* Workaround for older msvcrt and openbsd. GH #366 */
- if (Parrot_is_nzero($1)) {
- Parrot_io_printf(interp, "-0");
- }
- else {
- Parrot_io_printf(interp, FLOATVAL_FMT, $1);
- }
+ if (!Parrot_is_nzero(NREG(1)))
+ Parrot_io_write_b(interp, _PIO_STDOUT(interp), buf, strlen(buf));
+ else
+ /* Workaround for older msvcrt and openbsd. GH #366 */
+ Parrot_io_write_b(interp, _PIO_STDOUT(interp), "-0", 2);
#endif
}
@@ -89,20 +99,31 @@ Print $1 to standard output with a trailing newline.
=cut
inline op say(in INT) :base_io {
- Parrot_io_printf(interp, INTVAL_FMT "\n", (INTVAL)$1);
+ char buf[128];
+#ifdef PARROT_HAS_SNPRINTF
+ snprintf(buf, 128, INTVAL_FMT "\n", (INTVAL)$1);
+#else
+ sprintf(buf, INTVAL_FMT "\n", (INTVAL)$1);
+#endif
+ Parrot_io_write_b(interp, _PIO_STDOUT(interp), buf, strlen(buf));
}
inline op say(in NUM) :base_io {
+ char buf[128];
+ size_t ignore;
+#ifdef PARROT_HAS_SNPRINTF
+ snprintf(buf, 128, FLOATVAL_FMT "\n", $1);
+#else
+ sprintf(buf, 128, FLOATVAL_FMT, $1);
+#endif
#ifdef PARROT_HAS_NEGATIVE_ZERO
- Parrot_io_printf(interp, FLOATVAL_FMT "\n", $1);
+ Parrot_io_write_b(interp, _PIO_STDOUT(interp), buf, strlen(buf));
#else
- /* Workaround for older msvcrt and openbsd. GH #366 */
- if (Parrot_is_nzero($1)) {
- Parrot_io_printf(interp, "-0\n");
- }
- else {
- Parrot_io_printf(interp, FLOATVAL_FMT "\n", $1);
- }
+ if (!Parrot_is_nzero(NREG(1)))
+ Parrot_io_write_b(interp, _PIO_STDOUT(interp), buf, strlen(buf));
+ else
+ /* Workaround for older msvcrt and openbsd. GH #366 */
+ Parrot_io_write_b(interp, _PIO_STDOUT(interp), "-0\n", 2);
#endif
}
Please sign in to comment.
Something went wrong with that request. Please try again.