Skip to content

Commit 6f83cc5

Browse files
committed
perf: Move to protobuf-lite.
7.3M -> 4.8M for the JNI library.
1 parent 591216d commit 6f83cc5

File tree

7 files changed

+58
-57
lines changed

7 files changed

+58
-57
lines changed

docker/jvm.Dockerfile

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,27 @@ RUN apt-get update \
1717
&& rm -rf /var/lib/apt/lists/*
1818

1919
WORKDIR /work/jvm-toxcore-c
20+
# Rarely changing external dependencies.
2021
COPY scripts/ /work/jvm-toxcore-c/scripts/
2122
RUN scripts/build-host "$PWD/_install/host/protobuf.stamp" "-j$(nproc)"
2223
RUN scripts/build-host "$PWD/_install/host/toxcore.stamp" "-j$(nproc)"
24+
25+
# Native code, changes less frequently.
2326
COPY lib/src/main/cpp/ /work/jvm-toxcore-c/lib/src/main/cpp/
2427
COPY lib/src/main/proto/ /work/jvm-toxcore-c/lib/src/main/proto/
25-
RUN scripts/build-host "$PWD/_install/host/tox4j.stamp" "-j$(nproc)"
28+
RUN touch "$PWD/_install/host/.stamp" \
29+
&& touch "$PWD/_install/host/libsodium.stamp" \
30+
&& touch "$PWD/_install/host/libvpx.stamp" \
31+
&& touch "$PWD/_install/host/opus.stamp" \
32+
&& touch "$PWD/_install/host/protobuf.stamp" \
33+
&& touch "$PWD/_install/host/toxcore.stamp" \
34+
&& scripts/build-host "$PWD/_install/host/tox4j.stamp" "-j$(nproc)"
35+
RUN ["ls", "-lh", "/work/jvm-toxcore-c/_install/host/lib/libtox4j-c.so"]
36+
37+
# Java/Kotlin code changes a lot.
38+
COPY gradlew gradle.properties settings.gradle.kts /work/jvm-toxcore-c/
39+
COPY gradle /work/jvm-toxcore-c/gradle/
40+
COPY lib/ /work/jvm-toxcore-c/lib/
41+
ENV LD_LIBRARY_PATH=/work/jvm-toxcore-c/_install/host/lib
42+
ENV PATH=/work/jvm-toxcore-c/_install/host/bin:$PATH
43+
RUN ./gradlew build

lib/src/main/cpp/CMakeLists.txt

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
project(tox4j-c)
2-
cmake_minimum_required(VERSION 2.8.12)
2+
cmake_minimum_required(VERSION 3.16)
33

44
if(NOT DEFINED CMAKE_MACOSX_RPATH)
55
set(CMAKE_MACOSX_RPATH true)
@@ -11,42 +11,9 @@ endif()
1111

1212
find_package(PkgConfig REQUIRED)
1313

14-
pkg_search_module(LIBTOXCORE libtoxcore)
15-
if(LIBTOXCORE_FOUND)
16-
pkg_search_module(LIBTOXAV REQUIRED libtoxav)
17-
link_directories(${LIBTOXCORE_LIBRARY_DIRS})
18-
include_directories(${LIBTOXCORE_INCLUDE_DIRS})
19-
foreach(flag ${LIBTOXCORE_CFLAGS_OTHER})
20-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
21-
endforeach()
22-
23-
link_directories(${LIBTOXAV_LIBRARY_DIRS})
24-
include_directories(${LIBTOXAV_INCLUDE_DIRS})
25-
foreach(flag ${LIBTOXAV_CFLAGS_OTHER})
26-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
27-
endforeach()
28-
else()
29-
pkg_search_module(TOXCORE REQUIRED toxcore)
30-
link_directories(${TOXCORE_LIBRARY_DIRS})
31-
include_directories(${TOXCORE_INCLUDE_DIRS})
32-
foreach(flag ${TOXCORE_CFLAGS_OTHER})
33-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
34-
endforeach()
35-
endif()
36-
37-
find_package(Protobuf REQUIRED)
38-
include_directories(${PROTOBUF_INCLUDE_DIRS})
39-
14+
pkg_search_module(TOXCORE REQUIRED toxcore)
15+
find_package(protobuf CONFIG REQUIRED)
4016
find_package(JNI)
41-
if(JAVA_INCLUDE_PATH)
42-
include_directories(${JAVA_INCLUDE_PATH})
43-
endif()
44-
if(JAVA_INCLUDE_PATH2)
45-
include_directories(${JAVA_INCLUDE_PATH2})
46-
endif()
47-
48-
include_directories(${CMAKE_SOURCE_DIR})
49-
include_directories(${CMAKE_BINARY_DIR})
5017

5118
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
5219

@@ -96,14 +63,12 @@ endif()
9663
# Build
9764
#
9865

99-
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ToxAv/Av.proto
100-
ToxCore/Core.proto util/ProtoLog.proto)
101-
10266
add_library(
10367
${PROJECT_NAME} SHARED
10468
${ANDROID_CPU_FEATURES}
105-
${PROTO_SRCS}
106-
${PROTO_HDRS}
69+
ToxAv/Av.proto
70+
ToxCore/Core.proto
71+
util/ProtoLog.proto
10772
ToxAv/generated/enums.cpp
10873
ToxAv/generated/errors.cpp
10974
ToxAv/generated/impls.h
@@ -175,15 +140,28 @@ if(ANDROID_CPU_FEATURES)
175140
target_compile_definitions(${PROJECT_NAME} PRIVATE -Dtypeof=__typeof__)
176141
endif()
177142

178-
target_link_libraries(${PROJECT_NAME} ${PROTOBUF_LIBRARIES})
143+
target_include_directories(${PROJECT_NAME} PUBLIC ${PROTOBUF_INCLUDE_DIRS})
144+
target_link_libraries(${PROJECT_NAME} protobuf::libprotobuf-lite)
179145

180-
if(LIBTOXCORE_FOUND)
181-
target_link_libraries(${PROJECT_NAME} ${LIBTOXAV_STATIC_LIBRARIES}
182-
${LIBTOXCORE_STATIC_LIBRARIES})
183-
else()
184-
target_link_libraries(${PROJECT_NAME} ${TOXCORE_STATIC_LIBRARIES})
146+
if(JAVA_INCLUDE_PATH)
147+
target_include_directories(${PROJECT_NAME} PUBLIC ${JAVA_INCLUDE_PATH})
148+
endif()
149+
if(JAVA_INCLUDE_PATH2)
150+
target_include_directories(${PROJECT_NAME} PUBLIC ${JAVA_INCLUDE_PATH2})
185151
endif()
186152

153+
target_link_directories(${PROJECT_NAME} PUBLIC ${TOXCORE_LIBRARY_DIRS})
154+
target_link_libraries(${PROJECT_NAME} ${TOXCORE_STATIC_LIBRARIES})
155+
target_include_directories(${PROJECT_NAME} PUBLIC ${TOXCORE_INCLUDE_DIRS})
156+
157+
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR})
158+
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_BINARY_DIR})
159+
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_BINARY_DIR}/ToxAv)
160+
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_BINARY_DIR}/ToxCore)
161+
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_BINARY_DIR}/util)
162+
163+
protobuf_generate(TARGET ${PROJECT_NAME})
164+
187165
# Windows and OSX don't have this linker functionality.
188166
if(NOT WIN32 AND NOT APPLE)
189167
set_target_properties(

lib/src/main/proto/Av.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ syntax = "proto3";
33
package im.tox.tox4j.av.proto;
44

55
option java_multiple_files = true;
6+
option optimize_for = LITE_RUNTIME;
67

78
message Call {
89
int32 friend_number = 1;

lib/src/main/proto/Core.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ syntax = "proto3";
33
package im.tox.tox4j.core.proto;
44

55
option java_multiple_files = true;
6+
option optimize_for = LITE_RUNTIME;
67

78
message Connection {
89
enum Type {

lib/src/main/proto/ProtoLog.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ syntax = "proto3";
22

33
package im.tox.tox4j.impl.jni.proto;
44

5+
option java_multiple_files = true;
6+
option optimize_for = LITE_RUNTIME;
57

68
// struct timeval from <sys/time.h> representing a point in time.
79
message Timestamp {

scripts/build-host

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ cpp/src/ToxCrypto/generated/im_tox_tox4j_impl_jni_ToxCryptoJni.h: build
3939
%.run: ; $*
4040
regenerate: $(foreach i,$(wildcard bin/Jni*),$i.run) $(wildcard cpp/src/*/generated/*.h)
4141

42-
protobuf_CONFIGURE := --prefix=$(PREFIX) --disable-shared
42+
protobuf_CONFIGURE := -DCMAKE_INSTALL_PREFIX:PATH=$(PREFIX) -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_DISABLE_RTTI=ON -Dprotobuf_BUILD_SHARED_LIBS=OFF
4343
libsodium_CONFIGURE := --prefix=$(PREFIX) --disable-shared
4444
opus_CONFIGURE := --prefix=$(PREFIX) --disable-shared
4545
libvpx_CONFIGURE := --prefix=$(PREFIX) --disable-examples --disable-unit-tests --enable-pic

scripts/dependencies.mk

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
PRE_RULE = (echo "=== Building $@ ==="; ls -ld $@; true) && ls -ld $+
22
POST_RULE = ls -ld $@
33

4+
GIT_CLONE = git clone --depth=1 --recurse-submodules --shallow-submodules
5+
46
$(BUILDDIR)/tox4j/Makefile: $(CURDIR)/lib/src/main/cpp/CMakeLists.txt $(TOOLCHAIN_FILE) $(foreach i,protobuf toxcore,$(PREFIX)/$i.stamp)
57
@$(PRE_RULE)
68
mkdir -p $(@D)
@@ -17,14 +19,13 @@ $(PREFIX)/tox4j.stamp: $(BUILDDIR)/tox4j/Makefile
1719
# protobuf
1820

1921
$(SRCDIR)/protobuf:
20-
git clone --depth=1 --branch=v3.11.1 https://github.com/google/protobuf $@
22+
$(GIT_CLONE) --branch=v3.24.4 https://github.com/protocolbuffers/protobuf $@
2123

2224
$(PREFIX)/protobuf.stamp: $(SRCDIR)/protobuf $(TOOLCHAIN_FILE) $(PROTOC)
2325
@$(PRE_RULE)
24-
cd $< && autoreconf -fi
2526
mkdir -p $(BUILDDIR)/$(notdir $<)
26-
cd $(BUILDDIR)/$(notdir $<) && $(SRCDIR)/$(notdir $<)/configure $($(notdir $<)_CONFIGURE)
27-
$(MAKE) -C $(BUILDDIR)/$(notdir $<) install V=0
27+
cd $(BUILDDIR)/$(notdir $<) && cmake $(SRCDIR)/$(notdir $<) $($(notdir $<)_CONFIGURE)
28+
$(MAKE) -C $(BUILDDIR)/$(notdir $<) install
2829
mkdir -p $(@D) && touch $@
2930
@$(POST_RULE)
3031

@@ -35,7 +36,7 @@ $(SRCDIR)/toxcore:
3536
if [ -e ../c-toxcore ]; then \
3637
ln -s $(realpath ../c-toxcore) $@; \
3738
else \
38-
git clone --depth=1 --recurse-submodules https://github.com/TokTok/c-toxcore $@; \
39+
$(GIT_CLONE) https://github.com/TokTok/c-toxcore $@; \
3940
fi
4041

4142
$(PREFIX)/toxcore.stamp: $(foreach f,$(shell cd $(SRCDIR)/toxcore && git ls-files),$(SRCDIR)/toxcore/$f)
@@ -51,7 +52,7 @@ $(PREFIX)/toxcore.stamp: $(SRCDIR)/toxcore $(TOOLCHAIN_FILE) $(foreach i,libsodi
5152
# libsodium
5253

5354
$(SRCDIR)/libsodium:
54-
git clone --depth=1 --branch=1.0.18 https://github.com/jedisct1/libsodium $@
55+
$(GIT_CLONE) --branch=1.0.18 https://github.com/jedisct1/libsodium $@
5556

5657
$(PREFIX)/libsodium.stamp: $(SRCDIR)/libsodium $(TOOLCHAIN_FILE)
5758
@$(PRE_RULE)
@@ -66,7 +67,7 @@ $(PREFIX)/libsodium.stamp: $(SRCDIR)/libsodium $(TOOLCHAIN_FILE)
6667
# opus
6768

6869
$(SRCDIR)/opus:
69-
git clone --depth=1 https://github.com/xiph/opus $@
70+
$(GIT_CLONE) https://github.com/xiph/opus $@
7071

7172
$(PREFIX)/opus.stamp: $(SRCDIR)/opus $(TOOLCHAIN_FILE)
7273
@$(PRE_RULE)
@@ -81,7 +82,7 @@ $(PREFIX)/opus.stamp: $(SRCDIR)/opus $(TOOLCHAIN_FILE)
8182
# libvpx
8283

8384
$(SRCDIR)/libvpx:
84-
git clone --depth=1 --branch=v1.6.0 https://github.com/webmproject/libvpx $@
85+
$(GIT_CLONE) --branch=v1.6.0 https://github.com/webmproject/libvpx $@
8586
cd $@ && patch -p1 < $(CURDIR)/scripts/patches/libvpx.patch
8687

8788
$(PREFIX)/libvpx.stamp: $(SRCDIR)/libvpx $(TOOLCHAIN_FILE)

0 commit comments

Comments
 (0)