diff mbox

[v3,8/8] target-i386/kvm: cache the return value of kvm_enable_x2apic()

Message ID 20160930161013.9832-9-rkrcmar@redhat.com
State New
Headers show

Commit Message

Radim Krčmář Sept. 30, 2016, 4:10 p.m. UTC
Assume that KVM would have returned the same on subsequent runs.
Abstract the memoizaiton pattern into macros.

Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
---
 target-i386/kvm.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

Comments

Igor Mammedov Oct. 4, 2016, 11:33 a.m. UTC | #1
On Fri, 30 Sep 2016 18:10:13 +0200
Radim Krčmář <rkrcmar@redhat.com> wrote:

> Assume that KVM would have returned the same on subsequent runs.
> Abstract the memoizaiton pattern into macros.
s/memoi/memori/i
Throughout whole patch

> 
> Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
> ---
>  target-i386/kvm.c | 21 +++++++++++++++++++--
>  1 file changed, 19 insertions(+), 2 deletions(-)
> 
> diff --git a/target-i386/kvm.c b/target-i386/kvm.c
> index 0fd664648665..113c5bf058ba 100644
> --- a/target-i386/kvm.c
> +++ b/target-i386/kvm.c
> @@ -129,10 +129,27 @@ static bool kvm_x2apic_api_set_flags(uint64_t flags)
>      return !kvm_vm_enable_cap(s, KVM_CAP_X2APIC_API, 0, flags);
>  }
>  
> +#define MEMOIZE_RESULT(_result, _fn) \
> +    ({ \
> +        static bool _memoized; \
> +        if (_memoized) { \
> +            return _result; \
> +        } \
> +        _memoized = true; \
> +        _result = _fn; \
> +    })
> +
> +#define MEMOIZE(_fn) \
> +    ({ \
> +        static typeof(_fn) _result; \
> +        MEMOIZE_RESULT(_result, _fn); \
> +    })
> +
>  bool kvm_enable_x2apic(void)
>  {
> -    return kvm_x2apic_api_set_flags(KVM_X2APIC_API_USE_32BIT_IDS |
> -                                    KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK);
> +    return MEMOIZE(
> +             kvm_x2apic_api_set_flags(KVM_X2APIC_API_USE_32BIT_IDS |
> +                                      KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK));
>  }
>  
>  static int kvm_get_tsc(CPUState *cs)
Radim Krčmář Oct. 4, 2016, 1:45 p.m. UTC | #2
2016-10-04 13:33+0200, Igor Mammedov:
> On Fri, 30 Sep 2016 18:10:13 +0200
> Radim Krčmář <rkrcmar@redhat.com> wrote:
>> Assume that KVM would have returned the same on subsequent runs.
>> Abstract the memoizaiton pattern into macros.
> s/memoi/memori/i
> Throughout whole patch

The pattern I wanted to describe is known as "memoization" without r,
due to reasons, https://en.wikipedia.org/wiki/Memoization, but I was
expecting the whole idea to be nacked, so the naming didn't get much
thought. :)  I'll change it memorize, to create less confusion.
(Memoizing a non-pure "function" is hacky anyway.)

Thanks.
diff mbox

Patch

diff --git a/target-i386/kvm.c b/target-i386/kvm.c
index 0fd664648665..113c5bf058ba 100644
--- a/target-i386/kvm.c
+++ b/target-i386/kvm.c
@@ -129,10 +129,27 @@  static bool kvm_x2apic_api_set_flags(uint64_t flags)
     return !kvm_vm_enable_cap(s, KVM_CAP_X2APIC_API, 0, flags);
 }
 
+#define MEMOIZE_RESULT(_result, _fn) \
+    ({ \
+        static bool _memoized; \
+        if (_memoized) { \
+            return _result; \
+        } \
+        _memoized = true; \
+        _result = _fn; \
+    })
+
+#define MEMOIZE(_fn) \
+    ({ \
+        static typeof(_fn) _result; \
+        MEMOIZE_RESULT(_result, _fn); \
+    })
+
 bool kvm_enable_x2apic(void)
 {
-    return kvm_x2apic_api_set_flags(KVM_X2APIC_API_USE_32BIT_IDS |
-                                    KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK);
+    return MEMOIZE(
+             kvm_x2apic_api_set_flags(KVM_X2APIC_API_USE_32BIT_IDS |
+                                      KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK));
 }
 
 static int kvm_get_tsc(CPUState *cs)