Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dune-common
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Core Modules
dune-common
Commits
fd7a42b3
Commit
fd7a42b3
authored
14 years ago
by
Martin Nolte
Browse files
Options
Downloads
Patches
Plain Diff
add a SmallObjectAllocator (for transitional purposes, see FS #766)
[[Imported from SVN: r5953]]
parent
2df9e3fd
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
dune/common/smallobject.hh
+89
-7
89 additions, 7 deletions
dune/common/smallobject.hh
dune/common/test/smallobject.cc
+22
-9
22 additions, 9 deletions
dune/common/test/smallobject.cc
with
111 additions
and
16 deletions
dune/common/smallobject.hh
+
89
−
7
View file @
fd7a42b3
...
...
@@ -75,11 +75,11 @@ namespace Dune
return
current
+
1
;
}
static
void
free
(
void
*
p
tr
)
static
void
free
(
void
*
p
)
{
if
(
p
tr
!=
0
)
if
(
p
!=
0
)
{
Block
*
current
=
reinterpret_cast
<
Block
*
>
(
p
tr
)
-
1
;
Block
*
current
=
reinterpret_cast
<
Block
*
>
(
p
)
-
1
;
const
unsigned
int
blocks
=
current
->
blocks
;
Block
*&
next
=
list
(
blocks
);
current
->
next
=
next
;
...
...
@@ -97,15 +97,97 @@ namespace Dune
{
void
*
operator
new
(
size_t
size
)
{
return
SmallObjectPool
::
allocate
(
size
);
return
SmallObjectPool
::
allocate
(
size
);
}
void
operator
delete
(
void
*
p
tr
)
void
operator
delete
(
void
*
p
)
{
SmallObjectPool
::
free
(
p
tr
);
SmallObjectPool
::
free
(
p
);
}
};
// SmallObjectAllocator
// --------------------
template
<
class
T
>
struct
SmallObjectAllocator
;
template
<
>
struct
SmallObjectAllocator
<
void
>
{
typedef
void
value_type
;
typedef
void
*
pointer
;
typedef
const
void
*
const_pointer
;
template
<
class
U
>
struct
rebind
{
typedef
SmallObjectAllocator
<
U
>
other
;
};
};
template
<
class
T
>
struct
SmallObjectAllocator
{
typedef
T
value_type
;
typedef
size_t
size_type
;
typedef
ptrdiff_t
difference_type
;
typedef
T
*
pointer
;
typedef
const
T
*
const_pointer
;
typedef
T
&
reference
;
typedef
const
T
&
const_reference
;
template
<
class
U
>
struct
rebind
{
typedef
SmallObjectAllocator
<
U
>
other
;
};
SmallObjectAllocator
()
throw
()
{}
template
<
class
U
>
SmallObjectAllocator
(
const
SmallObjectAllocator
<
U
>
&
)
throw
()
{}
~
SmallObjectAllocator
()
throw
()
{}
pointer
address
(
reference
r
)
const
{
return
&
r
;
}
const_pointer
address
(
const_reference
r
)
const
{
return
&
r
;
}
pointer
allocate
(
size_type
n
,
SmallObjectAllocator
<
void
>::
const_pointer
hint
=
0
);
void
deallocate
(
pointer
p
,
size_type
n
);
void
construct
(
pointer
p
,
const
T
&
value
)
{
new
(
p
)
T
(
value
);
}
void
destroy
(
pointer
p
)
{
p
->~
T
();
}
};
// Implementation of SmallObjectAllocator
// --------------------------------------
template
<
class
T
>
inline
typename
SmallObjectAllocator
<
T
>::
pointer
SmallObjectAllocator
<
T
>::
allocate
(
size_type
n
,
SmallObjectAllocator
<
void
>::
const_pointer
hint
)
{
return
reinterpret_cast
<
pointer
>
(
SmallObjectPool
::
allocate
(
n
*
sizeof
(
T
)
)
);
}
template
<
class
T
>
inline
void
SmallObjectAllocator
<
T
>::
deallocate
(
pointer
p
,
size_type
n
)
{
SmallObjectPool
::
free
(
p
);
}
template
<
class
T
>
bool
operator
==
(
const
SmallObjectAllocator
<
T
>
&
,
const
SmallObjectAllocator
<
T
>
&
)
throw
()
{
return
true
;
}
template
<
class
T
>
bool
operator
!=
(
const
SmallObjectAllocator
<
T
>
&
,
const
SmallObjectAllocator
<
T
>
&
)
throw
()
{
return
false
;
}
}
#endif
#endif
// #ifndef DUNE_SMALLOBJECT_HH
This diff is collapsed.
Click to expand it.
dune/common/test/smallobject.cc
+
22
−
9
View file @
fd7a42b3
...
...
@@ -63,18 +63,31 @@ int main ( int argc, char **argv )
std
::
cout
<<
"Result: SmallObject is "
<<
(
timeA
/
timeB
)
<<
" times faster."
<<
std
::
endl
;
timer
.
reset
();
PoolAllocator
<
B
,
100
>
pool
;
SmallObjectAllocator
<
A
>
alloc
;
for
(
unsigned
long
i
=
0
;
i
<
factor
*
iterations
;
++
i
)
{
B
*
b
=
pool
.
allocate
(
1
);
pool
.
construct
(
b
,
B
((
int
)
i
));
pool
.
destroy
(
b
);
pool
.
deallocate
(
b
,
1
);
A
*
a
=
alloc
.
allocate
(
1
);
alloc
.
construct
(
a
,
A
(
int
(
i
)
)
);
alloc
.
destroy
(
a
);
alloc
.
deallocate
(
a
,
1
);
}
double
timeB2
=
timer
.
elapsed
();
std
::
cout
<<
"Time with pool allocator: "
<<
timeB2
<<
std
::
endl
;
std
::
cout
<<
"Result: pool allocator is "
<<
(
timeA
/
timeB2
)
<<
" times faster."
<<
std
::
endl
;
std
::
cout
<<
"Result: pool allocator is "
<<
(
timeB
/
timeB2
)
<<
" times faster than SmallObject."
<<
std
::
endl
;
double
timeC
=
timer
.
elapsed
();
std
::
cout
<<
"Time with SmallObjectAllocator: "
<<
timeC
<<
std
::
endl
;
std
::
cout
<<
"Result: SmallObject is "
<<
(
timeA
/
timeC
)
<<
" times faster."
<<
std
::
endl
;
timer
.
reset
();
PoolAllocator
<
A
,
100
>
pool
;
for
(
unsigned
long
i
=
0
;
i
<
factor
*
iterations
;
++
i
)
{
A
*
a
=
pool
.
allocate
(
1
);
pool
.
construct
(
a
,
A
(
int
(
i
)
)
);
pool
.
destroy
(
a
);
pool
.
deallocate
(
a
,
1
);
}
double
timeD
=
timer
.
elapsed
();
std
::
cout
<<
"Time with pool allocator: "
<<
timeD
<<
std
::
endl
;
std
::
cout
<<
"Result: pool allocator is "
<<
(
timeA
/
timeD
)
<<
" times faster."
<<
std
::
endl
;
std
::
cout
<<
"Result: pool allocator is "
<<
(
timeB
/
timeD
)
<<
" times faster than SmallObject."
<<
std
::
endl
;
// we require a speedup due to SmallObject
//assert((timeA / timeB) > 1.0);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment