[ 
https://issues.apache.org/jira/browse/MESOS-1919?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14184139#comment-14184139
 ] 

Joris Van Remoortere commented on MESOS-1919:
---------------------------------------------

[~evelinad] My comment about using an stl container was not about allocating 
too much memory. It was about:
1. Using dynamic allocation rather than local storage. Dynamic allocation has a 
higher run-time cost and can trigger locking of the heap (depending on the 
allocator). Since we know these types are fixed size and small, we can allocate 
them directly in the managing object like you did in the union example.
2. STL containers are primarily designed to deal with collections of objects 
that are dynamic (change in size over time, or the size it not known till 
runtime). When we are trying to represent a very specific behavior that 
requires a specific amount of storage then we should use a type (such as your 
example struct) to more accurately represent the idea.

If you run valgrind and look at the allocation count of the following 2 
examples it will be more clear:

{code:title=a.cc|borderStyle=solid}
#include <stdint.h>
#include <vector>

struct IP {
  IP() : data{1,2,3,4,5,6,7,8} {}
  std::vector<uint8_t> data;
};

int main() {
  IP a;
  return 0;
}
{code}
{code:title=valgrind ./a.out|borderStyle=solid}
total heap usage: 1 allocs, 1 frees, 8 bytes allocated
{code}
{code:title=b.cc|borderStyle=solid}
#include <stdint.h>
#include <vector>

struct IP2 {
  IP2(int64_t val) : dataBig(val) {}
  IP2(int8_t val) : dataSmall(val) {}
  union {
    int64_t dataBig;
    int8_t dataSmall;
  };
};

int main() {
  int8_t i8 = 3;
  int64_t i64 = -1;
  IP2 a(i8);
  IP2 b(i8);
  return 0;
}
{code}
{code:title=valgrind ./b.out|borderStyle=solid}
total heap usage: 0 allocs, 0 frees, 0 bytes allocated
{code}

> Create IP address abstraction
> -----------------------------
>
>                 Key: MESOS-1919
>                 URL: https://issues.apache.org/jira/browse/MESOS-1919
>             Project: Mesos
>          Issue Type: Task
>          Components: libprocess
>            Reporter: Dominic Hamon
>            Assignee: Evelina Dumitrescu
>            Priority: Minor
>
> in the code many functions need only the ip address to be passed as a 
> parameter. I don't think it would be desirable to use a struct 
> SockaddrStorage (MESOS-1916).
> Consider using a {{std::vector<unsigned char>}} (see {{typedef 
> std::vector<unsigned char> IPAddressNumber;}} in the Chromium project)



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to