Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public class #E#InlineDependencyCache extends #E#InlineCacheImpl implements Depe
*/
private Set<Reference<DependencyNode>> dependentNodes;

/**
* Number of elements in dependentNodes after which all the set should be checked for the presence of
* references to GC'ed objects.
*
* This threshold is required in order to evict such references as they pollute memory and never GC'ed otherwise.
*/
private int cleanupThreshold = 10;

private Reference<DependencyNode> selfReference;

public #E#InlineDependencyCache(Object owner, #E#Calculatable calculable, MutableStatistics statistics) {
Expand Down Expand Up @@ -68,6 +76,26 @@ public class #E#InlineDependencyCache extends #E#InlineCacheImpl implements Depe
dependentNodes = new THashSet<Reference<DependencyNode>>();
}
dependentNodes.add(node.getSelfReference());
cleanupIfNeeded();
}

private void cleanupIfNeeded() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why it is not in AbstractCache?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two different concepts:

  • Cache - takes care of storing the data and accessing it;
  • DependencyNode - takes care about dependencies.

There are few implementations XXXInlineDependencyCache that implement both Cache and DependencyNode. This reduces the memory overhead of caching in some simple cases.

But cleanupIfNeeded stuff is not part of Cache contract, so AbstractCache has nothing to do with that.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be create helper class? Too much code duplicating.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class is a template, others are autogenerated from this template.

It doesn't seem reasonable to create some kind of helper for a single method or add one more class to the hierarchy.

Code generation is bad, I know. But there's no other way to have high performance with primitives in Java.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

if (dependentNodes.size() >= cleanupThreshold) {
for (Iterator<Reference<DependencyNode>> it = dependentNodes.iterator(); it.hasNext(); ) {
if (it.next().get() == null) {
it.remove();
}
}
// It's important to increase cleanup threshold according to the number of elements in a set
// in order to maintain the balance between CPU-overhead and memory-overhead

// The cleanup has O(N) complexity, so doing this on addition of N new elements would lead to constant
// small overhead and thus would not affect the asymptotic behaviour of operations.

// The memory overhead could be significant but it's guaranteed that memory usage would not be more than
// 2 * peak memory usage for alive elements.
cleanupThreshold = dependentNodes.size() * 2;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange, very strange. After cleaning dependentNodes can be empty. And must be immediately cleaned after it. Very strange approach. Cleanup occurs while reading cache. So if somebody add 1M dependencies (they all alive while adding) and read cache after, than next cleanup occurs almost never.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After cleaning dependentNodes can be empty.

Why do you think so? Dependencies are never reset ;-)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check by time?

  1. in cleanupIfNeeded check current time and last check time
  2. in separate thread call all cleanupIfNeeded after XXX seconds
  3. check on cache clean (may be after timeout)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That might help, however:

  • there are thousands of hundreds of dependency nodes, so this might hurt performance.
  • it's hard to obtain the list of all dependency nodes.
  • the memory footprint will still be high if allocation seed is high (e.g. if 1M of objects is allocated and then GC'e within the interval of cleanups).

}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public class BooleanInlineDependencyCache extends BooleanInlineCacheImpl impleme
*/
private Set<Reference<DependencyNode>> dependentNodes;

/**
* Number of elements in dependentNodes after which all the set should be checked for the presence of
* references to GC'ed objects.
*
* This threshold is required in order to evict such references as they pollute memory and never GC'ed otherwise.
*/
private int cleanupThreshold = 10;

private Reference<DependencyNode> selfReference;

public BooleanInlineDependencyCache(Object owner, BooleanCalculatable calculable, MutableStatistics statistics) {
Expand Down Expand Up @@ -68,6 +76,26 @@ public synchronized void trackDependency(DependencyNode node) {
dependentNodes = new THashSet<Reference<DependencyNode>>();
}
dependentNodes.add(node.getSelfReference());
cleanupIfNeeded();
}

private void cleanupIfNeeded() {
if (dependentNodes.size() >= cleanupThreshold) {
for (Iterator<Reference<DependencyNode>> it = dependentNodes.iterator(); it.hasNext(); ) {
if (it.next().get() == null) {
it.remove();
}
}
// It's important to increase cleanup threshold according to the number of elements in a set
// in order to maintain the balance between CPU-overhead and memory-overhead

// The cleanup has O(N) complexity, so doing this on addition of N new elements would lead to constant
// small overhead and thus would not affect the asymptotic behaviour of operations.

// The memory overhead could be significant but it's guaranteed that memory usage would not be more than
// 2 * peak memory usage for alive elements.
cleanupThreshold = dependentNodes.size() * 2;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public class ByteInlineDependencyCache extends ByteInlineCacheImpl implements De
*/
private Set<Reference<DependencyNode>> dependentNodes;

/**
* Number of elements in dependentNodes after which all the set should be checked for the presence of
* references to GC'ed objects.
*
* This threshold is required in order to evict such references as they pollute memory and never GC'ed otherwise.
*/
private int cleanupThreshold = 10;

private Reference<DependencyNode> selfReference;

public ByteInlineDependencyCache(Object owner, ByteCalculatable calculable, MutableStatistics statistics) {
Expand Down Expand Up @@ -68,6 +76,26 @@ public synchronized void trackDependency(DependencyNode node) {
dependentNodes = new THashSet<Reference<DependencyNode>>();
}
dependentNodes.add(node.getSelfReference());
cleanupIfNeeded();
}

private void cleanupIfNeeded() {
if (dependentNodes.size() >= cleanupThreshold) {
for (Iterator<Reference<DependencyNode>> it = dependentNodes.iterator(); it.hasNext(); ) {
if (it.next().get() == null) {
it.remove();
}
}
// It's important to increase cleanup threshold according to the number of elements in a set
// in order to maintain the balance between CPU-overhead and memory-overhead

// The cleanup has O(N) complexity, so doing this on addition of N new elements would lead to constant
// small overhead and thus would not affect the asymptotic behaviour of operations.

// The memory overhead could be significant but it's guaranteed that memory usage would not be more than
// 2 * peak memory usage for alive elements.
cleanupThreshold = dependentNodes.size() * 2;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public class CharacterInlineDependencyCache extends CharacterInlineCacheImpl imp
*/
private Set<Reference<DependencyNode>> dependentNodes;

/**
* Number of elements in dependentNodes after which all the set should be checked for the presence of
* references to GC'ed objects.
*
* This threshold is required in order to evict such references as they pollute memory and never GC'ed otherwise.
*/
private int cleanupThreshold = 10;

private Reference<DependencyNode> selfReference;

public CharacterInlineDependencyCache(Object owner, CharacterCalculatable calculable, MutableStatistics statistics) {
Expand Down Expand Up @@ -68,6 +76,26 @@ public synchronized void trackDependency(DependencyNode node) {
dependentNodes = new THashSet<Reference<DependencyNode>>();
}
dependentNodes.add(node.getSelfReference());
cleanupIfNeeded();
}

private void cleanupIfNeeded() {
if (dependentNodes.size() >= cleanupThreshold) {
for (Iterator<Reference<DependencyNode>> it = dependentNodes.iterator(); it.hasNext(); ) {
if (it.next().get() == null) {
it.remove();
}
}
// It's important to increase cleanup threshold according to the number of elements in a set
// in order to maintain the balance between CPU-overhead and memory-overhead

// The cleanup has O(N) complexity, so doing this on addition of N new elements would lead to constant
// small overhead and thus would not affect the asymptotic behaviour of operations.

// The memory overhead could be significant but it's guaranteed that memory usage would not be more than
// 2 * peak memory usage for alive elements.
cleanupThreshold = dependentNodes.size() * 2;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public class DoubleInlineDependencyCache extends DoubleInlineCacheImpl implement
*/
private Set<Reference<DependencyNode>> dependentNodes;

/**
* Number of elements in dependentNodes after which all the set should be checked for the presence of
* references to GC'ed objects.
*
* This threshold is required in order to evict such references as they pollute memory and never GC'ed otherwise.
*/
private int cleanupThreshold = 10;

private Reference<DependencyNode> selfReference;

public DoubleInlineDependencyCache(Object owner, DoubleCalculatable calculable, MutableStatistics statistics) {
Expand Down Expand Up @@ -68,6 +76,26 @@ public synchronized void trackDependency(DependencyNode node) {
dependentNodes = new THashSet<Reference<DependencyNode>>();
}
dependentNodes.add(node.getSelfReference());
cleanupIfNeeded();
}

private void cleanupIfNeeded() {
if (dependentNodes.size() >= cleanupThreshold) {
for (Iterator<Reference<DependencyNode>> it = dependentNodes.iterator(); it.hasNext(); ) {
if (it.next().get() == null) {
it.remove();
}
}
// It's important to increase cleanup threshold according to the number of elements in a set
// in order to maintain the balance between CPU-overhead and memory-overhead

// The cleanup has O(N) complexity, so doing this on addition of N new elements would lead to constant
// small overhead and thus would not affect the asymptotic behaviour of operations.

// The memory overhead could be significant but it's guaranteed that memory usage would not be more than
// 2 * peak memory usage for alive elements.
cleanupThreshold = dependentNodes.size() * 2;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public class FloatInlineDependencyCache extends FloatInlineCacheImpl implements
*/
private Set<Reference<DependencyNode>> dependentNodes;

/**
* Number of elements in dependentNodes after which all the set should be checked for the presence of
* references to GC'ed objects.
*
* This threshold is required in order to evict such references as they pollute memory and never GC'ed otherwise.
*/
private int cleanupThreshold = 10;

private Reference<DependencyNode> selfReference;

public FloatInlineDependencyCache(Object owner, FloatCalculatable calculable, MutableStatistics statistics) {
Expand Down Expand Up @@ -68,6 +76,26 @@ public synchronized void trackDependency(DependencyNode node) {
dependentNodes = new THashSet<Reference<DependencyNode>>();
}
dependentNodes.add(node.getSelfReference());
cleanupIfNeeded();
}

private void cleanupIfNeeded() {
if (dependentNodes.size() >= cleanupThreshold) {
for (Iterator<Reference<DependencyNode>> it = dependentNodes.iterator(); it.hasNext(); ) {
if (it.next().get() == null) {
it.remove();
}
}
// It's important to increase cleanup threshold according to the number of elements in a set
// in order to maintain the balance between CPU-overhead and memory-overhead

// The cleanup has O(N) complexity, so doing this on addition of N new elements would lead to constant
// small overhead and thus would not affect the asymptotic behaviour of operations.

// The memory overhead could be significant but it's guaranteed that memory usage would not be more than
// 2 * peak memory usage for alive elements.
cleanupThreshold = dependentNodes.size() * 2;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public class IntInlineDependencyCache extends IntInlineCacheImpl implements Depe
*/
private Set<Reference<DependencyNode>> dependentNodes;

/**
* Number of elements in dependentNodes after which all the set should be checked for the presence of
* references to GC'ed objects.
*
* This threshold is required in order to evict such references as they pollute memory and never GC'ed otherwise.
*/
private int cleanupThreshold = 10;

private Reference<DependencyNode> selfReference;

public IntInlineDependencyCache(Object owner, IntCalculatable calculable, MutableStatistics statistics) {
Expand Down Expand Up @@ -68,6 +76,26 @@ public synchronized void trackDependency(DependencyNode node) {
dependentNodes = new THashSet<Reference<DependencyNode>>();
}
dependentNodes.add(node.getSelfReference());
cleanupIfNeeded();
}

private void cleanupIfNeeded() {
if (dependentNodes.size() >= cleanupThreshold) {
for (Iterator<Reference<DependencyNode>> it = dependentNodes.iterator(); it.hasNext(); ) {
if (it.next().get() == null) {
it.remove();
}
}
// It's important to increase cleanup threshold according to the number of elements in a set
// in order to maintain the balance between CPU-overhead and memory-overhead

// The cleanup has O(N) complexity, so doing this on addition of N new elements would lead to constant
// small overhead and thus would not affect the asymptotic behaviour of operations.

// The memory overhead could be significant but it's guaranteed that memory usage would not be more than
// 2 * peak memory usage for alive elements.
cleanupThreshold = dependentNodes.size() * 2;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public class LongInlineDependencyCache extends LongInlineCacheImpl implements De
*/
private Set<Reference<DependencyNode>> dependentNodes;

/**
* Number of elements in dependentNodes after which all the set should be checked for the presence of
* references to GC'ed objects.
*
* This threshold is required in order to evict such references as they pollute memory and never GC'ed otherwise.
*/
private int cleanupThreshold = 10;

private Reference<DependencyNode> selfReference;

public LongInlineDependencyCache(Object owner, LongCalculatable calculable, MutableStatistics statistics) {
Expand Down Expand Up @@ -68,6 +76,26 @@ public synchronized void trackDependency(DependencyNode node) {
dependentNodes = new THashSet<Reference<DependencyNode>>();
}
dependentNodes.add(node.getSelfReference());
cleanupIfNeeded();
}

private void cleanupIfNeeded() {
if (dependentNodes.size() >= cleanupThreshold) {
for (Iterator<Reference<DependencyNode>> it = dependentNodes.iterator(); it.hasNext(); ) {
if (it.next().get() == null) {
it.remove();
}
}
// It's important to increase cleanup threshold according to the number of elements in a set
// in order to maintain the balance between CPU-overhead and memory-overhead

// The cleanup has O(N) complexity, so doing this on addition of N new elements would lead to constant
// small overhead and thus would not affect the asymptotic behaviour of operations.

// The memory overhead could be significant but it's guaranteed that memory usage would not be more than
// 2 * peak memory usage for alive elements.
cleanupThreshold = dependentNodes.size() * 2;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public class ObjectInlineDependencyCache extends ObjectInlineCacheImpl implement
*/
private Set<Reference<DependencyNode>> dependentNodes;

/**
* Number of elements in dependentNodes after which all the set should be checked for the presence of
* references to GC'ed objects.
*
* This threshold is required in order to evict such references as they pollute memory and never GC'ed otherwise.
*/
private int cleanupThreshold = 10;

private Reference<DependencyNode> selfReference;

public ObjectInlineDependencyCache(Object owner, ObjectCalculatable calculable, MutableStatistics statistics) {
Expand Down Expand Up @@ -68,6 +76,26 @@ public synchronized void trackDependency(DependencyNode node) {
dependentNodes = new THashSet<Reference<DependencyNode>>();
}
dependentNodes.add(node.getSelfReference());
cleanupIfNeeded();
}

private void cleanupIfNeeded() {
if (dependentNodes.size() >= cleanupThreshold) {
for (Iterator<Reference<DependencyNode>> it = dependentNodes.iterator(); it.hasNext(); ) {
if (it.next().get() == null) {
it.remove();
}
}
// It's important to increase cleanup threshold according to the number of elements in a set
// in order to maintain the balance between CPU-overhead and memory-overhead

// The cleanup has O(N) complexity, so doing this on addition of N new elements would lead to constant
// small overhead and thus would not affect the asymptotic behaviour of operations.

// The memory overhead could be significant but it's guaranteed that memory usage would not be more than
// 2 * peak memory usage for alive elements.
cleanupThreshold = dependentNodes.size() * 2;
}
}

@Override
Expand Down
Loading