Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Programming and Hacking > Development
Register FAQ Calendar Today's Posts Search

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 27th December 2019, 21:43   #1  |  Link
stax76
Registered User
 
stax76's Avatar
 
Join Date: Jun 2002
Location: On thin ice
Posts: 6,837
C++ performance problem

Why is func1 100 times faster than the func2?

Code:
extern "C" {
	__declspec(dllexport) LPVOID __stdcall
	func1()
	{
		CmyInterface* pTest = new CmyInterface();
		LPVOID pMyInterface;
		pTest->QueryInterface(__uuidof(ImyInterface), &pMyInterface);
		return pMyInterface;
	}

        __declspec(dllexport) ImyInterface* __stdcall
        func2()
        {
            return (ImyInterface*) new CmyInterface();
        }
}
It has nothing to do with the server implementation because I tested two independent implementations. One found here:

https://www.codeproject.com/Articles...out-ATL-or-MFC

and other is that:

Code:
#include "pch.h"

#include <ComDef.h>

#include <atomic>


// {864C3D72-B6FD-4014-8AB4-55B664FAEF2C}
static const GUID CLSID_BasicServer =
    { 0x864c3d72, 0xb6fd, 0x4014, { 0x8a, 0xb4, 0x55, 0xb6, 0x64, 0xfa, 0xef, 0x2c } };

// {177D3798-3D6D-4217-B355-D2355884CFDF}
static const GUID IID_IBasicServer =
    { 0x177d3798, 0x3d6d, 0x4217, { 0xb3, 0x55, 0xd2, 0x35, 0x58, 0x84, 0xcf, 0xdf } };


struct IBasicServer : IUnknown
{
    virtual int __stdcall GetFive() = 0;
};


class BasicServer : IBasicServer
{

private:

    std::atomic<int> m_refs = 0;

public:

    // IUnknown

    HRESULT __stdcall QueryInterface(const IID& iid, void** ppv);
    ULONG   __stdcall AddRef();
    ULONG   __stdcall Release();

    // IBasicServer

    int     __stdcall GetFive();
};


// IUnknown

STDMETHODIMP BasicServer::QueryInterface(const IID& iid, void** ppv)
{
    if (!ppv)
        return E_POINTER;

    if (iid == IID_IUnknown)
    {
        *ppv = (IUnknown*)this;
    }
    else if (iid == IID_IBasicServer)
    {
        *ppv = (IBasicServer*)this;
    }
    else
    {
        *ppv = nullptr;
        return E_NOINTERFACE;
    }

    AddRef();
    return S_OK;
}


STDMETHODIMP_(ULONG) BasicServer::AddRef()
{
    return ++m_refs;
}


STDMETHODIMP_(ULONG) BasicServer::Release() {
    int refs = --m_refs;

    if (!refs)
        delete this;

    return refs;
}


// IBasicServer

int __stdcall BasicServer::GetFive()
{
    return 5;
}


// Extern

extern "C" {
    __declspec(dllexport) LPVOID __stdcall
    CreateBasicServer()
    {
        BasicServer* pServer = new BasicServer();
        LPVOID pIBasicServer;
        pServer->QueryInterface(IID_IBasicServer, &pIBasicServer);
        return pIBasicServer;
    }
}
Might there be an easier way to write a simple server, with fewer lines code or is the code fine? It's derived from code found in VapourSynth.

Last edited by stax76; 27th December 2019 at 21:47.
stax76 is offline   Reply With Quote
 


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:55.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.