Skip to content

Commit

Permalink
Merge branch 'master' into unreviewed/phil/fix-7988-b
Browse files Browse the repository at this point in the history
  • Loading branch information
philsc authored Dec 8, 2023
2 parents 7cd6ad6 + 5ba80c2 commit 9f61e0e
Show file tree
Hide file tree
Showing 56 changed files with 642 additions and 291 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ if(FLATBUFFERS_BUILD_TESTS)

# Add a library so there is a single target that the generated samples can
# link too.
if(MSVC)
if(MSVC OR ${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.20.0")
add_library(flatsample INTERFACE)
else()
add_library(flatsample STATIC)
Expand Down Expand Up @@ -598,7 +598,7 @@ if(FLATBUFFERS_BUILD_GRPCTEST)
find_package(protobuf CONFIG REQUIRED)
find_package(gRPC CONFIG REQUIRED)
add_executable(grpctest ${FlatBuffers_GRPCTest_SRCS})
target_link_libraries(grpctext
target_link_libraries(grpctest
PRIVATE
$<BUILD_INTERFACE:ProjectConfig>
gRPC::grpc++_unsecure
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/*
* Copyright 2023 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import Benchmark
import CoreFoundation
import FlatBuffers

@usableFromInline
struct AA: NativeStruct {
public init(a: Double, b: Double) {
self.a = a
self.b = b
}
var a: Double
var b: Double
}

let benchmarks = {
let ints: [Int] = Array(repeating: 42, count: 100)
let bytes: [UInt8] = Array(repeating: 42, count: 100)
let str10 = (0...9).map { _ -> String in "x" }.joined()
let str100 = (0...99).map { _ -> String in "x" }.joined()
let array: [AA] = [
AA(a: 2.4, b: 2.4),
AA(a: 2.4, b: 2.4),
AA(a: 2.4, b: 2.4),
AA(a: 2.4, b: 2.4),
AA(a: 2.4, b: 2.4),
]

let metrics: [BenchmarkMetric] = [
.cpuTotal,
.wallClock,
.mallocCountTotal,
.releaseCount,
.peakMemoryResident,
]
let maxIterations = 1_000_000
let maxDuration: Duration = .seconds(3)
let singleConfiguration: Benchmark.Configuration = .init(
metrics: metrics,
warmupIterations: 1,
scalingFactor: .one,
maxDuration: maxDuration,
maxIterations: maxIterations)
let kiloConfiguration: Benchmark.Configuration = .init(
metrics: metrics,
warmupIterations: 1,
scalingFactor: .kilo,
maxDuration: maxDuration,
maxIterations: maxIterations)
let megaConfiguration: Benchmark.Configuration = .init(
metrics: metrics,
warmupIterations: 1,
scalingFactor: .mega,
maxDuration: maxDuration,
maxIterations: maxIterations)

Benchmark.defaultConfiguration = megaConfiguration

Benchmark("Allocating 1GB", configuration: singleConfiguration) { benchmark in
for _ in benchmark.scaledIterations {
blackHole(FlatBufferBuilder(initialSize: 1_024_000_000))
}
}

Benchmark("Clearing 1GB", configuration: singleConfiguration) { benchmark in
var fb = FlatBufferBuilder(initialSize: 1_024_000_000)
benchmark.startMeasurement()
for _ in benchmark.scaledIterations {
blackHole(fb.clear())
}
}

Benchmark("Strings 10") { benchmark in
var fb = FlatBufferBuilder(initialSize: 1<<20)
benchmark.startMeasurement()
for _ in benchmark.scaledIterations {
blackHole(fb.create(string: str10))
}
}

Benchmark("Strings 100") { benchmark in
var fb = FlatBufferBuilder(initialSize: 1<<20)
benchmark.startMeasurement()
for _ in benchmark.scaledIterations {
blackHole(fb.create(string: str100))
}
}

Benchmark("Vector 1 Bytes") { benchmark in
var fb = FlatBufferBuilder(initialSize: 1<<20)
benchmark.startMeasurement()
for _ in benchmark.scaledIterations {
blackHole(fb.createVector(bytes: bytes))
}
}

Benchmark("Vector 1 Ints") { benchmark in
var fb = FlatBufferBuilder(initialSize: 1<<20)
benchmark.startMeasurement()
for _ in benchmark.scaledIterations {
blackHole(fb.createVector(ints))
}
}

Benchmark("Vector 100 Ints") { benchmark in
var fb = FlatBufferBuilder(initialSize: 1<<20)
benchmark.startMeasurement()
for i in benchmark.scaledIterations {
blackHole(fb.createVector(ints))
}
}

Benchmark("Vector 100 Bytes") { benchmark in
var fb = FlatBufferBuilder(initialSize: 1<<20)
benchmark.startMeasurement()
for i in benchmark.scaledIterations {
blackHole(fb.createVector(bytes))
}
}

Benchmark("Vector 100 ContiguousBytes") { benchmark in
var fb = FlatBufferBuilder(initialSize: 1<<20)
benchmark.startMeasurement()
for i in benchmark.scaledIterations {
blackHole(fb.createVector(bytes: bytes))
}
}

Benchmark(
"FlatBufferBuilder Add",
configuration: kiloConfiguration)
{ benchmark in
var fb = FlatBufferBuilder(initialSize: 1024 * 1024 * 32)
benchmark.startMeasurement()
for _ in benchmark.scaledIterations {
let off = fb.create(string: "T")
let s = fb.startTable(with: 4)
fb.add(element: 3.2, def: 0, at: 2)
fb.add(element: 4.2, def: 0, at: 4)
fb.add(element: 5.2, def: 0, at: 6)
fb.add(offset: off, at: 8)
blackHole(fb.endTable(at: s))
}
}

Benchmark("Structs") { benchmark in
let rawSize = ((16 * 5) * benchmark.scaledIterations.count) / 1024
var fb = FlatBufferBuilder(initialSize: Int32(rawSize * 1600))
var offsets: [Offset] = []

benchmark.startMeasurement()
for _ in benchmark.scaledIterations {
let vector = fb.createVector(
ofStructs: array)
let start = fb.startTable(with: 1)
fb.add(offset: vector, at: 4)
offsets.append(Offset(offset: fb.endTable(at: start)))
}

let vector = fb.createVector(ofOffsets: offsets)
let start = fb.startTable(with: 1)
fb.add(offset: vector, at: 4)
let root = Offset(offset: fb.endTable(at: start))
fb.finish(offset: root)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.1
// swift-tools-version:5.8
/*
* Copyright 2020 Google Inc. All rights reserved.
*
Expand All @@ -20,15 +20,23 @@ import PackageDescription
let package = Package(
name: "benchmarks",
platforms: [
.macOS(.v10_14),
.macOS(.v13),
],
dependencies: [
.package(path: "../../.."),
.package(url: "https://github.com/google/swift-benchmark", from: "0.1.0"),
.package(path: "../.."),
.package(
url: "https://github.com/ordo-one/package-benchmark",
from: "1.12.0"),
],
targets: [
.target(
name: "benchmarks",
dependencies: ["FlatBuffers",
.product(name: "Benchmark", package: "swift-benchmark")]),
.executableTarget(
name: "FlatbuffersBenchmarks",
dependencies: [
.product(name: "FlatBuffers", package: "flatbuffers"),
.product(name: "Benchmark", package: "package-benchmark"),
],
path: "Benchmarks/FlatbuffersBenchmarks",
plugins: [
.plugin(name: "BenchmarkPlugin", package: "package-benchmark"),
]),
])
9 changes: 9 additions & 0 deletions benchmarks/swift/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Benchmarks

To open the benchmarks in xcode use:

`open --env BENCHMARK_DISABLE_JEMALLOC=true Package.swift`

or running them directly within terminal using:

`swift package benchmark`
44 changes: 25 additions & 19 deletions goldens/cpp/basic_generated.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
// automatically generated by the FlatBuffers compiler, do not modify


#ifndef FLATBUFFERS_GENERATED_BASIC_H_
#define FLATBUFFERS_GENERATED_BASIC_H_
#ifndef FLATBUFFERS_GENERATED_BASIC_FLATBUFFERS_GOLDENS_H_
#define FLATBUFFERS_GENERATED_BASIC_FLATBUFFERS_GOLDENS_H_

#include "flatbuffers/flatbuffers.h"

// Ensure the included flatbuffers.h is the same version as when this file was
// generated, otherwise it may not be compatible.
static_assert(FLATBUFFERS_VERSION_MAJOR == 23 &&
FLATBUFFERS_VERSION_MINOR == 5 &&
FLATBUFFERS_VERSION_REVISION == 9,
FLATBUFFERS_VERSION_REVISION == 26,
"Non-compatible flatbuffers version included");

namespace flatbuffers {
namespace goldens {

struct Galaxy;
struct GalaxyBuilder;

Expand Down Expand Up @@ -69,8 +72,8 @@ struct Universe FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
double age() const {
return GetField<double>(VT_AGE, 0.0);
}
const ::flatbuffers::Vector<::flatbuffers::Offset<Galaxy>> *galaxies() const {
return GetPointer<const ::flatbuffers::Vector<::flatbuffers::Offset<Galaxy>> *>(VT_GALAXIES);
const ::flatbuffers::Vector<::flatbuffers::Offset<flatbuffers::goldens::Galaxy>> *galaxies() const {
return GetPointer<const ::flatbuffers::Vector<::flatbuffers::Offset<flatbuffers::goldens::Galaxy>> *>(VT_GALAXIES);
}
bool Verify(::flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
Expand All @@ -89,7 +92,7 @@ struct UniverseBuilder {
void add_age(double age) {
fbb_.AddElement<double>(Universe::VT_AGE, age, 0.0);
}
void add_galaxies(::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset<Galaxy>>> galaxies) {
void add_galaxies(::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset<flatbuffers::goldens::Galaxy>>> galaxies) {
fbb_.AddOffset(Universe::VT_GALAXIES, galaxies);
}
explicit UniverseBuilder(::flatbuffers::FlatBufferBuilder &_fbb)
Expand All @@ -106,7 +109,7 @@ struct UniverseBuilder {
inline ::flatbuffers::Offset<Universe> CreateUniverse(
::flatbuffers::FlatBufferBuilder &_fbb,
double age = 0.0,
::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset<Galaxy>>> galaxies = 0) {
::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset<flatbuffers::goldens::Galaxy>>> galaxies = 0) {
UniverseBuilder builder_(_fbb);
builder_.add_age(age);
builder_.add_galaxies(galaxies);
Expand All @@ -116,42 +119,45 @@ inline ::flatbuffers::Offset<Universe> CreateUniverse(
inline ::flatbuffers::Offset<Universe> CreateUniverseDirect(
::flatbuffers::FlatBufferBuilder &_fbb,
double age = 0.0,
const std::vector<::flatbuffers::Offset<Galaxy>> *galaxies = nullptr) {
auto galaxies__ = galaxies ? _fbb.CreateVector<::flatbuffers::Offset<Galaxy>>(*galaxies) : 0;
return CreateUniverse(
const std::vector<::flatbuffers::Offset<flatbuffers::goldens::Galaxy>> *galaxies = nullptr) {
auto galaxies__ = galaxies ? _fbb.CreateVector<::flatbuffers::Offset<flatbuffers::goldens::Galaxy>>(*galaxies) : 0;
return flatbuffers::goldens::CreateUniverse(
_fbb,
age,
galaxies__);
}

inline const Universe *GetUniverse(const void *buf) {
return ::flatbuffers::GetRoot<Universe>(buf);
inline const flatbuffers::goldens::Universe *GetUniverse(const void *buf) {
return ::flatbuffers::GetRoot<flatbuffers::goldens::Universe>(buf);
}

inline const Universe *GetSizePrefixedUniverse(const void *buf) {
return ::flatbuffers::GetSizePrefixedRoot<Universe>(buf);
inline const flatbuffers::goldens::Universe *GetSizePrefixedUniverse(const void *buf) {
return ::flatbuffers::GetSizePrefixedRoot<flatbuffers::goldens::Universe>(buf);
}

inline bool VerifyUniverseBuffer(
::flatbuffers::Verifier &verifier) {
return verifier.VerifyBuffer<Universe>(nullptr);
return verifier.VerifyBuffer<flatbuffers::goldens::Universe>(nullptr);
}

inline bool VerifySizePrefixedUniverseBuffer(
::flatbuffers::Verifier &verifier) {
return verifier.VerifySizePrefixedBuffer<Universe>(nullptr);
return verifier.VerifySizePrefixedBuffer<flatbuffers::goldens::Universe>(nullptr);
}

inline void FinishUniverseBuffer(
::flatbuffers::FlatBufferBuilder &fbb,
::flatbuffers::Offset<Universe> root) {
::flatbuffers::Offset<flatbuffers::goldens::Universe> root) {
fbb.Finish(root);
}

inline void FinishSizePrefixedUniverseBuffer(
::flatbuffers::FlatBufferBuilder &fbb,
::flatbuffers::Offset<Universe> root) {
::flatbuffers::Offset<flatbuffers::goldens::Universe> root) {
fbb.FinishSizePrefixed(root);
}

#endif // FLATBUFFERS_GENERATED_BASIC_H_
} // namespace goldens
} // namespace flatbuffers

#endif // FLATBUFFERS_GENERATED_BASIC_FLATBUFFERS_GOLDENS_H_
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// automatically generated by the FlatBuffers compiler, do not modify
// </auto-generated>

namespace flatbuffers.goldens
{

using global::System;
using global::System.Collections.Generic;
using global::Google.FlatBuffers;
Expand All @@ -18,7 +21,7 @@ public struct Galaxy : IFlatbufferObject

public long NumStars { get { int o = __p.__offset(4); return o != 0 ? __p.bb.GetLong(o + __p.bb_pos) : (long)0; } }

public static Offset<Galaxy> CreateGalaxy(FlatBufferBuilder builder,
public static Offset<flatbuffers.goldens.Galaxy> CreateGalaxy(FlatBufferBuilder builder,
long num_stars = 0) {
builder.StartTable(1);
Galaxy.AddNumStars(builder, num_stars);
Expand All @@ -27,9 +30,9 @@ public static Offset<Galaxy> CreateGalaxy(FlatBufferBuilder builder,

public static void StartGalaxy(FlatBufferBuilder builder) { builder.StartTable(1); }
public static void AddNumStars(FlatBufferBuilder builder, long numStars) { builder.AddLong(0, numStars, 0); }
public static Offset<Galaxy> EndGalaxy(FlatBufferBuilder builder) {
public static Offset<flatbuffers.goldens.Galaxy> EndGalaxy(FlatBufferBuilder builder) {
int o = builder.EndTable();
return new Offset<Galaxy>(o);
return new Offset<flatbuffers.goldens.Galaxy>(o);
}
}

Expand All @@ -43,3 +46,5 @@ static public bool Verify(Google.FlatBuffers.Verifier verifier, uint tablePos)
&& verifier.VerifyTableEnd(tablePos);
}
}

}
Loading

0 comments on commit 9f61e0e

Please sign in to comment.