mwrap user+dev discussion/patches/pulls/bugs/help
 help / color / Atom feed
From: Eric Wong <e@80x24.org>
To: mwrap-public@80x24.org
Cc: Eric Wong <e@80x24.org>
Subject: [PATCH] struct acc: use 64-bit counters
Date: Sat, 11 Aug 2018 03:53:39 +0000
Message-ID: <20180811035339.2044-1-e@80x24.org> (raw)

Since there's no hope of atomicity here anyways, use 64-bit
counters.
---
 ext/mwrap/mwrap.c | 34 ++++++++++++++++++++--------------
 1 file changed, 20 insertions(+), 14 deletions(-)

diff --git a/ext/mwrap/mwrap.c b/ext/mwrap/mwrap.c
index b60c21d..5174127 100644
--- a/ext/mwrap/mwrap.c
+++ b/ext/mwrap/mwrap.c
@@ -218,14 +218,14 @@ static int has_ec_p(void)
 }
 
 struct acc {
-	size_t nr;
-	size_t min;
-	size_t max;
+	uint64_t nr;
+	int64_t min;
+	int64_t max;
 	double m2;
 	double mean;
 };
 
-#define ACC_INIT(name) { .nr=0, .min=SIZE_MAX, .max=0, .m2=0, .mean=0 }
+#define ACC_INIT(name) { .nr=0, .min=INT64_MAX, .max=-1, .m2=0, .mean=0 }
 
 /* for tracking 16K-aligned heap page bodies (protected by GVL) */
 struct {
@@ -314,29 +314,35 @@ static void
 acc_add(struct acc *acc, size_t val)
 {
 	double delta = val - acc->mean;
-	size_t nr = ++acc->nr;
+	uint64_t nr = ++acc->nr;
 
-	/* 32-bit overflow, ignore accuracy, just don't divide-by-zero */
+	/* just don't divide-by-zero if we ever hit this (unlikely :P) */
 	if (nr)
 		acc->mean += delta / nr;
 
 	acc->m2 += delta * (val - acc->mean);
-	if (val < acc->min)
-		acc->min = val;
-	if (val > acc->max)
-		acc->max = val;
+	if ((int64_t)val < acc->min)
+		acc->min = (int64_t)val;
+	if ((int64_t)val > acc->max)
+		acc->max = (int64_t)val;
 }
 
+#if SIZEOF_LONG == 8
+# define INT64toNUM(x) LONG2NUM((long)x)
+#elif defined(HAVE_LONG_LONG) && SIZEOF_LONG_LONG == 8
+# define INT64toNUM(x) LL2NUM((LONG_LONG)x)
+#endif
+
 static VALUE
 acc_max(const struct acc *acc)
 {
-	return acc->max ? SIZET2NUM(acc->max) : DBL2NUM(HUGE_VAL);
+	return INT64toNUM(acc->max);
 }
 
 static VALUE
 acc_min(const struct acc *acc)
 {
-	return acc->min == SIZE_MAX ? DBL2NUM(HUGE_VAL) : SIZET2NUM(acc->min);
+	return acc->min == INT64_MAX ? INT2FIX(-1) : INT64toNUM(acc->min);
 }
 
 static VALUE
@@ -1360,12 +1366,12 @@ static void dump_hpb(FILE *fp, unsigned flags)
 			"deathspan_stddev: %0.3f\n"
 			"gc_count: %zu\n",
 			hpb_stats.alive.max,
-			hpb_stats.alive.min == SIZE_MAX ? " -" : " ",
+			hpb_stats.alive.min == INT64_MAX ? " -" : " ",
 			hpb_stats.alive.min,
 			hpb_stats.alive.mean,
 			acc_stddev_dbl(&hpb_stats.alive),
 			hpb_stats.reborn.max,
-			hpb_stats.reborn.min == SIZE_MAX ? " -" : " ",
+			hpb_stats.reborn.min == INT64_MAX ? " -" : " ",
 			hpb_stats.reborn.min,
 			hpb_stats.reborn.mean,
 			acc_stddev_dbl(&hpb_stats.reborn),
-- 
EW


             reply index

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-11  3:53 Eric Wong [this message]
2018-08-11  4:00 ` [SQUASH] " Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://80x24.org/mwrap/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180811035339.2044-1-e@80x24.org \
    --to=e@80x24.org \
    --cc=mwrap-public@80x24.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

mwrap user+dev discussion/patches/pulls/bugs/help

Archives are clonable:
	git clone --mirror https://80x24.org/mwrap-public/0 mwrap-public/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 mwrap-public mwrap-public/ https://80x24.org/mwrap-public \
		mwrap-public@80x24.org
	public-inbox-index mwrap-public

Example config snippet for mirrors

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.lang.ruby.mwrap
	nntp://ou63pmih66umazou.onion/inbox.comp.lang.ruby.mwrap

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git