Skip to content

Commit

Permalink
WFLY-9544 Reduce memory footprint of wildfly-clustering-marshalling-s…
Browse files Browse the repository at this point in the history
…pi by reducing object allocations for common clustering externalizers.

Enumerate common externalizer instances, instead of loading new instances per cache container and per deployment.
Drop separate class definitions for externalizer classes with just a constructor.
  • Loading branch information
pferraro committed Nov 14, 2017
1 parent 7da3efb commit 8477d39
Show file tree
Hide file tree
Showing 55 changed files with 379 additions and 1,041 deletions.
4 changes: 4 additions & 0 deletions clustering/infinispan/extension/pom.xml
Expand Up @@ -54,6 +54,10 @@
<groupId>org.wildfly</groupId>
<artifactId>wildfly-clustering-marshalling-infinispan</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-clustering-marshalling-spi</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-clustering-spi</artifactId>
Expand Down
Expand Up @@ -30,6 +30,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import javax.management.MBeanServer;

Expand Down Expand Up @@ -59,6 +60,7 @@
import org.jboss.msc.value.Value;
import org.wildfly.clustering.marshalling.Externalizer;
import org.wildfly.clustering.marshalling.infinispan.AdvancedExternalizerAdapter;
import org.wildfly.clustering.marshalling.spi.DefaultExternalizer;
import org.wildfly.clustering.service.Builder;
import org.wildfly.clustering.service.InjectedValueDependency;
import org.wildfly.clustering.service.ValueDependency;
Expand Down Expand Up @@ -115,7 +117,11 @@ public GlobalConfiguration getValue() {
builder.serialization().classResolver(ModularClassResolver.getInstance(this.loader.getValue()));
builder.classLoader(module.getClassLoader());
int id = Ids.MAX_ID;
for (Externalizer<?> externalizer : module.loadService(Externalizer.class)) {
Stream<? extends Externalizer<?>> commonExternalizers = EnumSet.allOf(DefaultExternalizer.class).stream();
@SuppressWarnings("unchecked")
Stream<Externalizer<?>> moduleExternalizers = (Stream<Externalizer<?>>) (Stream<?>) StreamSupport.stream(module.loadService(Externalizer.class).spliterator(), false);
Iterable<Externalizer<?>> externalizers = Stream.concat(commonExternalizers, moduleExternalizers)::iterator;
for (Externalizer<?> externalizer : externalizers) {
InfinispanLogger.ROOT_LOGGER.debugf("Cache container %s will use an externalizer for %s", this.name, externalizer.getTargetClass().getName());
builder.serialization().addAdvancedExternalizer(id++, new AdvancedExternalizerAdapter<>(externalizer));
}
Expand Down
Expand Up @@ -24,6 +24,7 @@

import java.io.IOException;
import java.lang.reflect.Modifier;
import java.util.EnumSet;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.ServiceLoader;
Expand All @@ -35,6 +36,7 @@
import org.jboss.marshalling.Unmarshaller;
import org.wildfly.clustering.marshalling.Externalizer;
import org.wildfly.clustering.marshalling.spi.IndexExternalizer;
import org.wildfly.clustering.marshalling.spi.DefaultExternalizer;

/**
* {@link ObjectTable} implementation that dynamically loads {@link Externalizer} instances available from a given {@link ClassLoader}.
Expand All @@ -47,12 +49,8 @@ public class ExternalizerObjectTable implements ObjectTable {
private final IndexExternalizer indexExternalizer;

public ExternalizerObjectTable(ClassLoader loader) {
this(Stream.concat(loadExternalizers(ExternalizerObjectTable.class.getClassLoader()), loadExternalizers(loader)).toArray(Externalizer[]::new));
}

@SuppressWarnings("rawtypes")
private static Stream<Externalizer> loadExternalizers(ClassLoader loader) {
return StreamSupport.stream(ServiceLoader.load(Externalizer.class, loader).spliterator(), false);
this(Stream.concat(EnumSet.allOf(DefaultExternalizer.class).stream(), StreamSupport.stream(ServiceLoader.load(Externalizer.class, loader).spliterator(), false))
.toArray(Externalizer[]::new));
}

public ExternalizerObjectTable(Externalizer<?>... externalizers) {
Expand Down
5 changes: 0 additions & 5 deletions clustering/marshalling/spi/pom.xml
Expand Up @@ -46,11 +46,6 @@
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron</artifactId>
</dependency>
<dependency>
<groupId>org.kohsuke.metainf-services</groupId>
<artifactId>metainf-services</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
Expand Up @@ -20,36 +20,43 @@
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.wildfly.clustering.marshalling.spi.util;
package org.wildfly.clustering.marshalling.spi;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Optional;
import java.util.function.Function;

import org.kohsuke.MetaInfServices;
import org.wildfly.clustering.marshalling.Externalizer;

/**
* {@link Externalizer} for {@link Optional} values.
* Base {@link Externalizer} for boolean-based externalization.
* @author Paul Ferraro
*/
@MetaInfServices(Externalizer.class)
public class OptionalExternalizer implements Externalizer<Optional<Object>> {
public class BooleanExternalizer<T> implements Externalizer<T> {

private final Class<T> targetClass;
private final Function<Boolean, T> reader;
private final Function<T, Boolean> writer;

public BooleanExternalizer(Class<T> targetClass, Function<Boolean, T> reader, Function<T, Boolean> writer) {
this.targetClass = targetClass;
this.reader = reader;
this.writer = writer;
}

@Override
public void writeObject(ObjectOutput output, Optional<Object> optional) throws IOException {
output.writeObject(optional.orElse(null));
public void writeObject(ObjectOutput output, T object) throws IOException {
output.writeBoolean(this.writer.apply(object).booleanValue());
}

@Override
public Optional<Object> readObject(ObjectInput input) throws IOException, ClassNotFoundException {
return Optional.ofNullable(input.readObject());
public T readObject(ObjectInput input) throws IOException, ClassNotFoundException {
return this.reader.apply(Boolean.valueOf(input.readBoolean()));
}

@SuppressWarnings("unchecked")
@Override
public Class<Optional<Object>> getTargetClass() {
return (Class<Optional<Object>>) (Class<?>) Optional.class;
public Class<T> getTargetClass() {
return this.targetClass;
}
}
@@ -0,0 +1,189 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2017, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.wildfly.clustering.marshalling.spi;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Month;
import java.time.Year;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.AbstractMap;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Currency;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Optional;
import java.util.TimeZone;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

import org.wildfly.clustering.marshalling.Externalizer;
import org.wildfly.clustering.marshalling.spi.net.URLExternalizer;
import org.wildfly.clustering.marshalling.spi.time.DurationExternalizer;
import org.wildfly.clustering.marshalling.spi.time.InstantExternalizer;
import org.wildfly.clustering.marshalling.spi.time.LocalDateTimeExternalizer;
import org.wildfly.clustering.marshalling.spi.time.MonthDayExternalizer;
import org.wildfly.clustering.marshalling.spi.time.PeriodExternalizer;
import org.wildfly.clustering.marshalling.spi.time.YearMonthExternalizer;
import org.wildfly.clustering.marshalling.spi.util.CalendarExternalizer;
import org.wildfly.clustering.marshalling.spi.util.CollectionExternalizer;
import org.wildfly.clustering.marshalling.spi.util.CopyOnWriteCollectionExternalizer;
import org.wildfly.clustering.marshalling.spi.util.DateExternalizer;
import org.wildfly.clustering.marshalling.spi.util.MapEntryExternalizer;
import org.wildfly.clustering.marshalling.spi.util.MapExternalizer;
import org.wildfly.clustering.marshalling.spi.util.SingletonCollectionExternalizer;
import org.wildfly.clustering.marshalling.spi.util.SingletonMapExternalizer;
import org.wildfly.clustering.marshalling.spi.util.SortedMapExternalizer;
import org.wildfly.clustering.marshalling.spi.util.SortedSetExternalizer;
import org.wildfly.clustering.marshalling.spi.util.UUIDExternalizer;

/**
* Default externalizers for common JDK types
* @author Paul Ferraro
*/
public enum DefaultExternalizer implements Externalizer<Object> {
// java.net
URI(new StringExternalizer<>(java.net.URI.class, java.net.URI::create, java.net.URI::toString)),
URL(new URLExternalizer()),
// java.time
DAY_OF_WEEK(new EnumExternalizer<>(DayOfWeek.class)),
DURATION(new DurationExternalizer()),
INSTANT(new InstantExternalizer()),
LOCAL_DATE(new LongExternalizer<>(LocalDate.class, LocalDate::ofEpochDay, LocalDate::toEpochDay)),
LOCAL_DATE_TIME(new LocalDateTimeExternalizer()),
LOCAL_TIME(new LongExternalizer<>(LocalTime.class, LocalTime::ofNanoOfDay, LocalTime::toNanoOfDay)),
MONTH(new EnumExternalizer<>(Month.class)),
MONTH_DAY(new MonthDayExternalizer()),
PERIOD(new PeriodExternalizer()),
YEAR(new IntExternalizer<>(Year.class, Year::of, Year::getValue)),
YEAR_MONTH(new YearMonthExternalizer()),
ZONE_ID(new StringExternalizer<>(ZoneId.class, ZoneId::of, ZoneId::getId)),
ZONE_OFFSET(new StringExternalizer<>(ZoneOffset.class, ZoneOffset::of, ZoneOffset::getId)),
// java.util
ARRAY_DEQUE(new CollectionExternalizer<>(ArrayDeque.class, ArrayDeque::new)),
ARRAY_LIST(new CollectionExternalizer<>(ArrayList.class, ArrayList::new)),
ATOMIC_BOOLEAN(new BooleanExternalizer<>(AtomicBoolean.class, AtomicBoolean::new, AtomicBoolean::get)),
ATOMIC_INTEGER(new IntExternalizer<>(AtomicInteger.class, AtomicInteger::new, AtomicInteger::get)),
ATOMIC_LONG(new LongExternalizer<>(AtomicLong.class, AtomicLong::new, AtomicLong::get)),
ATOMIC_REFERENCE(new ObjectExternalizer<>(AtomicReference.class, AtomicReference::new, AtomicReference::get)),
CALENDAR(new CalendarExternalizer()),
CONCURRENT_HASH_MAP(new MapExternalizer<>(ConcurrentHashMap.class, ConcurrentHashMap::new)),
CONCURRENT_HASH_SET(new CollectionExternalizer<>(ConcurrentHashMap.KeySetView.class, ConcurrentHashMap::newKeySet)),
CONCURRENT_LINKED_DEQUE(new CollectionExternalizer<>(ConcurrentLinkedDeque.class, size -> new ConcurrentLinkedDeque<>())),
CONCURRENT_LINKED_QUEUE(new CollectionExternalizer<>(ConcurrentLinkedQueue.class, size -> new ConcurrentLinkedQueue<>())),
CONCURRENT_SKIP_LIST_MAP(new SortedMapExternalizer<>(ConcurrentSkipListMap.class, ConcurrentSkipListMap::new)),
CONCURRENT_SKIP_LIST_SET(new SortedSetExternalizer<>(ConcurrentSkipListSet.class, ConcurrentSkipListSet::new)),
COPY_ON_WRITE_ARRAY_LIST(new CopyOnWriteCollectionExternalizer<>(CopyOnWriteArrayList.class, CopyOnWriteArrayList::new)),
COPY_ON_WRITE_ARRAY_SET(new CopyOnWriteCollectionExternalizer<>(CopyOnWriteArraySet.class, CopyOnWriteArraySet::new)),
CURRENCY(new StringExternalizer<>(Currency.class, Currency::getInstance, Currency::getCurrencyCode)),
DATE(new DateExternalizer<>(Date.class, Date::new)),
EMPTY_ENUMERATION(new ValueExternalizer<>(Collections.emptyEnumeration())),
EMPTY_ITERATOR(new ValueExternalizer<>(Collections.emptyIterator())),
EMPTY_LIST(new ValueExternalizer<>(Collections.emptyList())),
EMPTY_LIST_ITERATOR(new ValueExternalizer<>(Collections.emptyListIterator())),
EMPTY_MAP(new ValueExternalizer<>(Collections.emptyMap())),
EMPTY_NAVIGABLE_MAP(new ValueExternalizer<>(Collections.emptyNavigableMap())),
EMPTY_NAVIGABLE_SET(new ValueExternalizer<>(Collections.emptyNavigableSet())),
EMPTY_SET(new ValueExternalizer<>(Collections.emptySet())),
EMPTY_SORTED_MAP(new ValueExternalizer<>(Collections.emptySortedMap())),
EMPTY_SORTED_SET(new ValueExternalizer<>(Collections.emptySortedSet())),
HASH_MAP(new MapExternalizer<>(HashMap.class, HashMap::new)),
HASH_SET(new CollectionExternalizer<>(HashSet.class, HashSet::new)),
LINKED_HASH_MAP(new MapExternalizer<>(LinkedHashMap.class, LinkedHashMap::new)),
LINKED_HASH_SET(new CollectionExternalizer<>(LinkedHashSet.class, LinkedHashSet::new)),
LINKED_LIST(new CollectionExternalizer<>(LinkedList.class, size -> new LinkedList<>())),
LOCALE(new StringExternalizer<>(Locale.class, Locale::forLanguageTag, Locale::toLanguageTag)),
@SuppressWarnings("unchecked")
OPTIONAL(new ObjectExternalizer<>(Optional.class, Optional::ofNullable, optional -> optional.orElse(null))),
SIMPLE_ENTRY(new MapEntryExternalizer<>(AbstractMap.SimpleEntry.class, AbstractMap.SimpleEntry::new)),
SIMPLE_IMMUTABLE_ENTRY(new MapEntryExternalizer<>(AbstractMap.SimpleImmutableEntry.class, AbstractMap.SimpleImmutableEntry::new)),
SINGLETON_LIST(new SingletonCollectionExternalizer<>(Collections::singletonList)),
SINGLETON_MAP(new SingletonMapExternalizer()),
SINGLETON_SET(new SingletonCollectionExternalizer<>(Collections::singleton)),
SQL_DATE(new DateExternalizer<>(java.sql.Date.class, java.sql.Date::new)),
SQL_TIME(new DateExternalizer<>(java.sql.Time.class, java.sql.Time::new)),
SQL_TIMESTAMP(new DateExternalizer.SqlTimestampExternalizer()),
TIME_ZONE(new StringExternalizer<>(TimeZone.class, TimeZone::getTimeZone, TimeZone::getID)),
TREE_MAP(new SortedMapExternalizer<>(TreeMap.class, TreeMap::new)),
TREE_SET(new SortedSetExternalizer<>(TreeSet.class, TreeSet::new)),
UUID(new UUIDExternalizer()),
;

private final Externalizer<Object> externalizer;

@SuppressWarnings("unchecked")
DefaultExternalizer(Externalizer<?> externalizer) {
this.externalizer = (Externalizer<Object>) externalizer;
}

@Override
public void writeObject(ObjectOutput output, Object object) throws IOException {
this.externalizer.writeObject(output, object);
}

@Override
public Object readObject(ObjectInput input) throws IOException, ClassNotFoundException {
return this.externalizer.readObject(input);
}

@Override
public Class<Object> getTargetClass() {
return this.externalizer.getTargetClass();
}

/**
* Cast this externalizer to its target type.
* @param type the externalizer type
* @return the externalizer
*/
@SuppressWarnings("unchecked")
public <T> Externalizer<T> cast(Class<T> type) {
if (!type.isAssignableFrom(this.externalizer.getTargetClass())) {
throw new IllegalArgumentException(type.getName());
}
return (Externalizer<T>) this.externalizer;
}
}
Expand Up @@ -20,36 +20,42 @@
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.wildfly.clustering.marshalling.spi.util;
package org.wildfly.clustering.marshalling.spi;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;

import org.kohsuke.MetaInfServices;
import org.wildfly.clustering.marshalling.Externalizer;

/**
* {@link Externalizer} for an {@link AtomicReference>
* Base {@link Externalizer} for object wrapper externalization.
* @author Paul Ferraro
*/
@MetaInfServices(Externalizer.class)
public class AtomicReferenceExternalizer implements Externalizer<AtomicReference<Object>> {
public class ObjectExternalizer<T> implements Externalizer<T> {
private final Function<Object, T> reader;
private final Function<T, Object> writer;
private final Class<T> targetClass;

public ObjectExternalizer(Class<T> targetClass, Function<Object, T> reader, Function<T, Object> writer) {
this.targetClass = targetClass;
this.reader = reader;
this.writer = writer;
}

@Override
public void writeObject(ObjectOutput output, AtomicReference<Object> reference) throws IOException {
output.writeObject(reference.get());
public void writeObject(ObjectOutput output, T object) throws IOException {
output.writeObject(this.writer.apply(object));
}

@Override
public AtomicReference<Object> readObject(ObjectInput input) throws IOException, ClassNotFoundException {
return new AtomicReference<>(input.readObject());
public T readObject(ObjectInput input) throws IOException, ClassNotFoundException {
return this.reader.apply(input.readObject());
}

@SuppressWarnings("unchecked")
@Override
public Class<AtomicReference<Object>> getTargetClass() {
return (Class<AtomicReference<Object>>) (Class<?>) AtomicReference.class;
public Class<T> getTargetClass() {
return this.targetClass;
}
}
Expand Up @@ -38,6 +38,10 @@ public class StringExternalizer<T> implements Externalizer<T> {
private final Function<T, String> writer;
private final Class<T> targetClass;

public StringExternalizer(Class<T> targetClass, Function<String, T> reader) {
this(targetClass, reader, Object::toString);
}

public StringExternalizer(Class<T> targetClass, Function<String, T> reader, Function<T, String> writer) {
this.reader = reader;
this.writer = writer;
Expand Down

0 comments on commit 8477d39

Please sign in to comment.