From 5167e3da69df90afb667c2d09216b620a55fc646 Mon Sep 17 00:00:00 2001 From: "Mrozek, Michal" Date: Fri, 2 Feb 2018 13:46:30 +0100 Subject: [PATCH] [1/n] optimize CPU code. Limit the amount of atomic operations while decrementing ref count from 2 to 1 Change-Id: I0e9e9f07abd1aa62a3967ce4f83ffe2cc288765a --- runtime/utilities/reference_tracked_object.h | 11 ++++++++--- .../utilities/reference_tracked_object_tests.cpp | 7 ++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/runtime/utilities/reference_tracked_object.h b/runtime/utilities/reference_tracked_object.h index 165744f7b4..4dbf84f239 100644 --- a/runtime/utilities/reference_tracked_object.h +++ b/runtime/utilities/reference_tracked_object.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, Intel Corporation + * Copyright (c) 2017 - 2018, Intel Corporation * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -54,6 +54,10 @@ class RefCounter { return (curr == 0); } + CT decAndReturnCurrent() { + return --val; + } + bool peekIsZero() const { return (val == 0); } @@ -144,8 +148,9 @@ class ReferenceTrackedObject { unique_ptr_if_unused decRefInternal() { auto customDeleter = tryGetCustomDeleter(); - bool unused = refInternal.dec(); - UNRECOVERABLE_IF(refInternal.peek() < 0); + auto current = refInternal.decAndReturnCurrent(); + bool unused = (current == 0); + UNRECOVERABLE_IF(current < 0); return unique_ptr_if_unused(static_cast(this), unused, customDeleter); } diff --git a/unit_tests/utilities/reference_tracked_object_tests.cpp b/unit_tests/utilities/reference_tracked_object_tests.cpp index f192a1a9a7..76d413d247 100644 --- a/unit_tests/utilities/reference_tracked_object_tests.cpp +++ b/unit_tests/utilities/reference_tracked_object_tests.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, Intel Corporation + * Copyright (c) 2017 - 2018, Intel Corporation * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -45,6 +45,11 @@ TEST(RefCounter, referenceCount) { ASSERT_TRUE(rc.peekIsZero()); } +TEST(RefCounter, givenReferenceTrackedObjectWhenDecAndReturnCurrentIsCalledThenMinusOneIsReturned) { + RefCounter<> rc; + EXPECT_EQ(-1, rc.decAndReturnCurrent()); +} + TEST(unique_ptr_if_unused, InitializedWithDefaultConstructorAtQueryReturnsNullptr) { unique_ptr_if_unused uptr; ASSERT_EQ(nullptr, uptr.get());