Skip to content

Commit

Permalink
Reduces the core jar from 272 to 188k by tuning codec and shade
Browse files Browse the repository at this point in the history
Before, we used more reflection than necessary in codec. This implied
use of larger and more classes than we actually need. By slightly
changing codec, and manually controlling shade, we dropped the jar size
by 45%.
  • Loading branch information
Adrian Cole committed Aug 14, 2016
1 parent 0307137 commit b1ecb40
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Check out the [`zipkin-server`](/zipkin-server) documentation for configuration
## Core Library
The [core library](https://github.com/openzipkin/zipkin/tree/master/zipkin/src/main/java/io/zipkin) requires minimum language level 7. While currently only used by the server, we expect this library to be used in native instrumentation as well.

This includes built-in codec for both thrift and json structs. Direct dependencies on thrift or moshi (json library) are avoided by minifying and repackaging classes used. The result is a 256k jar which won't conflict with any library you use.
This includes built-in codec for both thrift and json structs. Direct dependencies on thrift or moshi (json library) are avoided by minifying and repackaging classes used. The result is a 190k jar which won't conflict with any library you use.

Ex.
```java
Expand Down
23 changes: 23 additions & 0 deletions zipkin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,29 @@
<configuration>
<shadeTestJar>false</shadeTestJar>
<minimizeJar>true</minimizeJar>
<filters>
<filter>
<artifact>com.squareup.moshi:moshi</artifact>
<includes>
<include>com/squareup/moshi/BufferedSinkJsonWriter*</include>
<include>com/squareup/moshi/BufferedSourceJsonReader*</include>
<!-- don't get zipkin/internal/moshi/JsonAdapter$Factory.class -->
<include>com/squareup/moshi/JsonAdapter.class</include>
<include>com/squareup/moshi/JsonAdapter$?.class</include>
<include>com/squareup/moshi/JsonDataException*</include>
<include>com/squareup/moshi/JsonReader*</include>
<include>com/squareup/moshi/JsonScope*</include>
<include>com/squareup/moshi/JsonWriter*</include>
</includes>
</filter>
<filter>
<!-- don't include pom files, some are kilobytes -->
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/**</exclude>
</excludes>
</filter>
</filters>
<relocations>
<relocation>
<pattern>okio</pattern>
Expand Down
74 changes: 67 additions & 7 deletions zipkin/src/main/java/zipkin/internal/JsonCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.squareup.moshi.JsonDataException;
import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonWriter;
import com.squareup.moshi.Moshi;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Collections;
Expand Down Expand Up @@ -122,14 +121,75 @@ public void toJson(JsonWriter writer, Endpoint value) throws IOException {
}
}.nullSafe();

static final Moshi MOSHI = new Moshi.Builder()
.add(Endpoint.class, ENDPOINT_ADAPTER)
.build();
static final JsonAdapter<Long> NULLABLE_LONG_ADAPTER = new JsonAdapter<Long>() {
@Override public Long fromJson(JsonReader reader) throws IOException {
return reader.nextLong();
}

@Override public void toJson(JsonWriter writer, Long value) throws IOException {
writer.value(value.longValue());
}

@Override public String toString() {
return "Long";
}
}.nullSafe();

static final JsonAdapter<Boolean> NULLABLE_BOOLEAN_ADAPTER = new JsonAdapter<Boolean>() {
@Override public Boolean fromJson(JsonReader reader) throws IOException {
return reader.nextBoolean();
}

@Override public void toJson(JsonWriter writer, Boolean value) throws IOException {
writer.value(value.booleanValue());
}

@Override public String toString() {
return "Boolean";
}
}.nullSafe();

public static final JsonAdapter<Annotation> ANNOTATION_ADAPTER = new JsonAdapter<Annotation>() {
@Override
public Annotation fromJson(JsonReader reader) throws IOException {
Annotation.Builder result = Annotation.builder();
reader.beginObject();
while (reader.hasNext()) {
switch (reader.nextName()) {
case "timestamp":
result.timestamp(reader.nextLong());
break;
case "value":
result.value(reader.nextString());
break;
case "endpoint":
result.endpoint(ENDPOINT_ADAPTER.fromJson(reader));
break;
default:
reader.skipValue();
}
}
reader.endObject();
return result.build();
}

static final JsonAdapter<Long> NULLABLE_LONG_ADAPTER = MOSHI.adapter(Long.class);
static final JsonAdapter<Boolean> NULLABLE_BOOLEAN_ADAPTER = MOSHI.adapter(Boolean.class);
@Override
public void toJson(JsonWriter writer, Annotation value) throws IOException {
writer.beginObject();
writer.name("timestamp").value(value.timestamp);
writer.name("value").value(value.value);
if (value.endpoint != null) {
writer.name("endpoint");
ENDPOINT_ADAPTER.toJson(writer, value.endpoint);
}
writer.endObject();
}

public static final JsonAdapter<Annotation> ANNOTATION_ADAPTER = MOSHI.adapter(Annotation.class);
@Override
public String toString() {
return "Annotation";
}
};

public static final JsonAdapter<BinaryAnnotation> BINARY_ANNOTATION_ADAPTER = new JsonAdapter<BinaryAnnotation>() {

Expand Down

0 comments on commit b1ecb40

Please sign in to comment.