0

After I read (long) articles about custom allocators :-

and did some simple testing, I realized that they can vastly improve performance of my software.

I feel that they are moderately-simple to be adopted in the most cases,
except in situation that a function want to return non-POD type.

Example

I create 2 systems SystemA and SystemB.
For simplicity, they are singleton and can be accessed globally.

SystemA* systemA; 
SystemB* systemB; 

Both system has it owns allocators for each type:-

class SystemA/SystemB{
    Allo oneFrameAllo=oneFrameAllocator();  
    Allo heapAllo=heapAllocator();  
};

By design, systemB want to request likeReport* report=systemA->generateReport().
Example: SystemB (AI-System) query all enemy information from SystemA (NPC-Manager).

In SystemA::generateReport() prefer to use its own allocator SystemA::oneFrameAllo to do internal thing, but systemB expect a result that use SystemB::heapAllo.

enter image description here

How to achieve that?
Note: In real case, Report is a non-POD, so it is not convenient to return Report by value.

Edit

Here is a compilable demo.
It uses "pass allocator as parameter" approach. (suggested by John Zwinck, thank!)
Only the essential part is shown here :-

class SystemA{
    Allo oneFrameAllo;  
    Allo heapAllo;  
    public: Strong_Pointer<Report> generateReport(Allo* allo){
        /*  use the one-frame-allocator to do internal computation */ 
        Strong_Pointer<int> some_temporary_variable=oneFrameAllo.allocate<int>();
        *(some_temporary_variable.ptr)=5; //result of #1 above
        Strong_Pointer<Report> r=allo->allocate<Report>();
        r.ptr->someInfo = allo->allocate<int>();
        *((r.ptr->someInfo).ptr)=*(some_temporary_variable.ptr);//#2
        return r;
    }
};
SystemA* systemA=new SystemA();
class SystemB{
    Allo oneFrameAllo;  
    Allo heapAllo;  
    Strong_Pointer<Report> report;
    public: void update(){
        report=systemA->generateReport(&heapAllo);
    }
};
SystemB* systemB=new SystemB();

I am still not sure if it is the right way.

Disadvantage:-

  • It is dirty : additional parameter.
  • (minor) The function interface will become very allocator-aware :-

    • If allocator is the first parameter : I can't make it default parameter in most case.
    • Otherwise, it interrupts existing function with default parameter e.g.

      f(X x=X()){}  -> call f() is OK
      become
      f(X x=X(),Allo* allo){}  -> f( must insert X() manually, &allo)
      
  • Really, I have to add that single parameter to many function in existing code base.
    Is it really ok?

  • (subjective) I have never found any code like this.

Another alternative is to let SystemB creates a report that has field Allo*,
and pass the &report as a parameter of systemA->generateReport().
SystemA will fill the report. (Thank vu1p3n0x)

Please provide either a better way OR confirm that this approach is already good in general.
Then, provides some references to show that it is really a professional approach.

javaLover
  • 6,347
  • 2
  • 22
  • 67
  • 2
    It might be a lot more clear if you posted a single, compilable, valid, minimal program that showed some specific problem. It is hard to believe that you actually need custom allocators here at all. What exactly are you allocating in your real use case? Your question is so lengthy it's hard to follow. – John Zwinck May 27 '17 at 04:15
  • @John Zwinck I agree about mcve, I will try to do it. About example, I edited my queston: "SystemB (AI-System) query all enemy information from SystemA (NPC-Manager)". About the "very long", I also agree, I will draw some helper diagram first to depict it, then try to create mcve (may take a long time). – javaLover May 27 '17 at 04:21
  • The standard way to do this is to pass the allocator you want used explicitly, either as an argument to `generateReport()`, or via a separate stateful function like `setAllocator()` (to be used by any `generateReport()` from then on). – John Zwinck May 27 '17 at 04:31
  • @John Zwinck Thank! I also perceived that from the both links, but I am still very hesitate and scared of it. May you help me alleviate my fear by providing more information / link / book / reference please? I have searched various source, but it is very hard for me to get more of such precious information. You can post it as answer if you desire ; I feel that you totally understand my question. – javaLover May 27 '17 at 04:34
  • @John Zwinck I have just added the compilable demo (+ steal your idea). Hopefully, it will improve quality of the question. – javaLover May 27 '17 at 04:54
  • Following your edit, you could potentially rework it to create a Report in SystemB and pass it to SystemA to fill. You can supply Report with your allocator if it needs to aggregate data in dynamic storage while being filled by SystemA. I don't know if this is a good idea in general though. – kmdreko May 27 '17 at 05:50
  • @vu1p3n0x Thank for digging. I would re-add that part. – javaLover May 27 '17 at 05:55

0 Answers0