Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8254883: ZGC: Make the ZArrayIterator reusable for ZRelocationSetIter…
…ators

Reviewed-by: eosterlund
  • Loading branch information
pliden committed Oct 20, 2020
1 parent cb6167b commit 3267b09
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 121 deletions.
22 changes: 11 additions & 11 deletions src/hotspot/share/gc/z/zArray.hpp
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -29,23 +29,23 @@

template <typename T> using ZArray = GrowableArrayCHeap<T, mtGC>;

template <typename T, bool parallel>
template <typename T, bool Parallel>
class ZArrayIteratorImpl : public StackObj {
private:
ZArray<T>* const _array;
int _next;
const T* _next;
const T* const _end;

bool next_serial(T* elem);
bool next_parallel(T* elem);

public:
ZArrayIteratorImpl(ZArray<T>* array);
ZArrayIteratorImpl(const T* array, size_t length);
ZArrayIteratorImpl(const ZArray<T>* array);

bool next(T* elem);
};

// Iterator types
#define ZARRAY_SERIAL false
#define ZARRAY_PARALLEL true

template <typename T> using ZArrayIterator = ZArrayIteratorImpl<T, ZARRAY_SERIAL>;
template <typename T> using ZArrayParallelIterator = ZArrayIteratorImpl<T, ZARRAY_PARALLEL>;
template <typename T> using ZArrayIterator = ZArrayIteratorImpl<T, false /* Parallel */>;
template <typename T> using ZArrayParallelIterator = ZArrayIteratorImpl<T, true /* Parallel */>;

#endif // SHARE_GC_Z_ZARRAY_HPP
62 changes: 44 additions & 18 deletions src/hotspot/share/gc/z/zArray.inline.hpp
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -27,28 +27,54 @@
#include "gc/z/zArray.hpp"
#include "runtime/atomic.hpp"

template <typename T, bool parallel>
inline ZArrayIteratorImpl<T, parallel>::ZArrayIteratorImpl(ZArray<T>* array) :
_array(array),
_next(0) {}

template <typename T, bool parallel>
inline bool ZArrayIteratorImpl<T, parallel>::next(T* elem) {
if (parallel) {
const int next = Atomic::fetch_and_add(&_next, 1);
if (next < _array->length()) {
*elem = _array->at(next);
return true;
template <typename T, bool Parallel>
inline bool ZArrayIteratorImpl<T, Parallel>::next_serial(T* elem) {
if (_next == _end) {
return false;
}

*elem = *_next;
_next++;

return true;
}

template <typename T, bool Parallel>
inline bool ZArrayIteratorImpl<T, Parallel>::next_parallel(T* elem) {
const T* old_next = Atomic::load(&_next);

for (;;) {
if (old_next == _end) {
return false;
}
} else {
if (_next < _array->length()) {
*elem = _array->at(_next++);

const T* const new_next = old_next + 1;
const T* const prev_next = Atomic::cmpxchg(&_next, old_next, new_next);
if (prev_next == old_next) {
*elem = *old_next;
return true;
}

old_next = prev_next;
}
}

template <typename T, bool Parallel>
inline ZArrayIteratorImpl<T, Parallel>::ZArrayIteratorImpl(const T* array, size_t length) :
_next(array),
_end(array + length) {}

// No more elements
return false;
template <typename T, bool Parallel>
inline ZArrayIteratorImpl<T, Parallel>::ZArrayIteratorImpl(const ZArray<T>* array) :
ZArrayIteratorImpl<T, Parallel>(array->is_empty() ? NULL : array->adr_at(0), array->length()) {}

template <typename T, bool Parallel>
inline bool ZArrayIteratorImpl<T, Parallel>::next(T* elem) {
if (Parallel) {
return next_parallel(elem);
} else {
return next_serial(elem);
}
}

#endif // SHARE_GC_Z_ZARRAY_INLINE_HPP
17 changes: 4 additions & 13 deletions src/hotspot/share/gc/z/zGranuleMap.hpp
Expand Up @@ -24,15 +24,13 @@
#ifndef SHARE_GC_Z_ZGRANULEMAP_HPP
#define SHARE_GC_Z_ZGRANULEMAP_HPP

#include "gc/z/zArray.hpp"
#include "memory/allocation.hpp"

template<typename T>
class ZGranuleMapIterator;

template <typename T>
class ZGranuleMap {
friend class VMStructs;
friend class ZGranuleMapIterator<T>;
template <typename> friend class ZGranuleMapIterator;

private:
const size_t _size;
Expand All @@ -53,16 +51,9 @@ class ZGranuleMap {
};

template <typename T>
class ZGranuleMapIterator : public StackObj {
public:
const ZGranuleMap<T>* const _map;
size_t _next;

class ZGranuleMapIterator : public ZArrayIteratorImpl<T, false /* Parallel */> {
public:
ZGranuleMapIterator(const ZGranuleMap<T>* map);

bool next(T* value);
bool next(T** value);
ZGranuleMapIterator(const ZGranuleMap<T>* granule_map);
};

#endif // SHARE_GC_Z_ZGRANULEMAP_HPP
28 changes: 3 additions & 25 deletions src/hotspot/share/gc/z/zGranuleMap.inline.hpp
Expand Up @@ -24,6 +24,7 @@
#ifndef SHARE_GC_Z_ZGRANULEMAP_INLINE_HPP
#define SHARE_GC_Z_ZGRANULEMAP_INLINE_HPP

#include "gc/z/zArray.inline.hpp"
#include "gc/z/zGlobals.hpp"
#include "gc/z/zGranuleMap.hpp"
#include "memory/allocation.inline.hpp"
Expand Down Expand Up @@ -86,30 +87,7 @@ inline void ZGranuleMap<T>::release_put(uintptr_t offset, T value) {
}

template <typename T>
inline ZGranuleMapIterator<T>::ZGranuleMapIterator(const ZGranuleMap<T>* map) :
_map(map),
_next(0) {}

template <typename T>
inline bool ZGranuleMapIterator<T>::next(T* value) {
if (_next < _map->_size) {
*value = _map->_map[_next++];
return true;
}

// End of map
return false;
}

template <typename T>
inline bool ZGranuleMapIterator<T>::next(T** value) {
if (_next < _map->_size) {
*value = _map->_map + _next++;
return true;
}

// End of map
return false;
}
inline ZGranuleMapIterator<T>::ZGranuleMapIterator(const ZGranuleMap<T>* granule_map) :
ZArrayIteratorImpl<T, false /* Parallel */>(granule_map->_map, granule_map->_size) {}

#endif // SHARE_GC_Z_ZGRANULEMAP_INLINE_HPP
4 changes: 2 additions & 2 deletions src/hotspot/share/gc/z/zRelocate.hpp
Expand Up @@ -24,9 +24,9 @@
#ifndef SHARE_GC_Z_ZRELOCATE_HPP
#define SHARE_GC_Z_ZRELOCATE_HPP

#include "gc/z/zRelocationSet.hpp"

class ZForwarding;
class ZRelocationSet;
class ZRelocationSetParallelIterator;
class ZWorkers;

class ZRelocate {
Expand Down
30 changes: 6 additions & 24 deletions src/hotspot/share/gc/z/zRelocationSet.hpp
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -24,6 +24,7 @@
#ifndef SHARE_GC_Z_ZRELOCATIONSET_HPP
#define SHARE_GC_Z_ZRELOCATIONSET_HPP

#include "gc/z/zArray.hpp"
#include "memory/allocation.hpp"

class ZForwarding;
Expand All @@ -44,32 +45,13 @@ class ZRelocationSet {
void reset();
};

template <bool parallel>
class ZRelocationSetIteratorImpl : public StackObj {
private:
ZRelocationSet* const _relocation_set;
size_t _next;

template <bool Parallel>
class ZRelocationSetIteratorImpl : public ZArrayIteratorImpl<ZForwarding*, Parallel> {
public:
ZRelocationSetIteratorImpl(ZRelocationSet* relocation_set);

bool next(ZForwarding** forwarding);
};

// Iterator types
#define ZRELOCATIONSET_SERIAL false
#define ZRELOCATIONSET_PARALLEL true

class ZRelocationSetIterator : public ZRelocationSetIteratorImpl<ZRELOCATIONSET_SERIAL> {
public:
ZRelocationSetIterator(ZRelocationSet* relocation_set) :
ZRelocationSetIteratorImpl<ZRELOCATIONSET_SERIAL>(relocation_set) {}
};

class ZRelocationSetParallelIterator : public ZRelocationSetIteratorImpl<ZRELOCATIONSET_PARALLEL> {
public:
ZRelocationSetParallelIterator(ZRelocationSet* relocation_set) :
ZRelocationSetIteratorImpl<ZRELOCATIONSET_PARALLEL>(relocation_set) {}
};
using ZRelocationSetIterator = ZRelocationSetIteratorImpl<false /* Parallel */>;
using ZRelocationSetParallelIterator = ZRelocationSetIteratorImpl<true /* Parallel */>;

#endif // SHARE_GC_Z_ZRELOCATIONSET_HPP
33 changes: 5 additions & 28 deletions src/hotspot/share/gc/z/zRelocationSet.inline.hpp
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -24,34 +24,11 @@
#ifndef SHARE_GC_Z_ZRELOCATIONSET_INLINE_HPP
#define SHARE_GC_Z_ZRELOCATIONSET_INLINE_HPP

#include "gc/z/zArray.inline.hpp"
#include "gc/z/zRelocationSet.hpp"
#include "runtime/atomic.hpp"

template <bool parallel>
inline ZRelocationSetIteratorImpl<parallel>::ZRelocationSetIteratorImpl(ZRelocationSet* relocation_set) :
_relocation_set(relocation_set),
_next(0) {}

template <bool parallel>
inline bool ZRelocationSetIteratorImpl<parallel>::next(ZForwarding** forwarding) {
const size_t nforwardings = _relocation_set->_nforwardings;

if (parallel) {
if (_next < nforwardings) {
const size_t next = Atomic::fetch_and_add(&_next, 1u);
if (next < nforwardings) {
*forwarding = _relocation_set->_forwardings[next];
return true;
}
}
} else {
if (_next < nforwardings) {
*forwarding = _relocation_set->_forwardings[_next++];
return true;
}
}

return false;
}
template <bool Parallel>
inline ZRelocationSetIteratorImpl<Parallel>::ZRelocationSetIteratorImpl(ZRelocationSet* relocation_set) :
ZArrayIteratorImpl<ZForwarding*, Parallel>(relocation_set->_forwardings, relocation_set->_nforwardings) {}

#endif // SHARE_GC_Z_ZRELOCATIONSET_INLINE_HPP

1 comment on commit 3267b09

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented on 3267b09 Oct 20, 2020

Choose a reason for hiding this comment

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

Please sign in to comment.