| import kotlin.coroutines.* | |
| import kotlin.coroutines.intrinsics.* | |
| /** | |
| * Defines deep recursive function that keeps its stack on the heap, | |
| * which allows very deep recursive computations that do not use the actual call stack. | |
| * To initiate a call to this deep recursive function use its [invoke] function. | |
| * As a rule of thumb, it should be used if recursion goes deeper than a thousand calls. | |
| * | |
| * The [DeepRecursiveFunction] takes one parameter of type [T] and returns a result of type [R]. |
| /** | |
| * Copyright 2019 Coinbase, Inc. | |
| * | |
| * 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, |
| /* | |
| * Copyright (C) 2018 Square, Inc. | |
| * | |
| * 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 |
| import com.sun.net.httpserver.Filter; | |
| import com.sun.net.httpserver.HttpExchange; | |
| import com.sun.net.httpserver.HttpHandler; | |
| import com.sun.net.httpserver.HttpServer; | |
| import java.io.IOException; | |
| import java.net.HttpURLConnection; | |
| import java.net.InetSocketAddress; | |
| import java.util.Arrays; | |
| import java.util.List; |
I was talking to a coworker recently about general techniques that almost always form the core of any effort to write very fast, down-to-the-metal hot path code on the JVM, and they pointed out that there really isn't a particularly good place to go for this information. It occurred to me that, really, I had more or less picked up all of it by word of mouth and experience, and there just aren't any good reference sources on the topic. So⦠here's my word of mouth.
This is by no means a comprehensive gist. It's also important to understand that the techniques that I outline in here are not 100% absolute either. Performance on the JVM is an incredibly complicated subject, and while there are rules that almost always hold true, the "almost" remains very salient. Also, for many or even most applications, there will be other techniques that I'm not mentioning which will have a greater impact. JMH, Java Flight Recorder, and a good profiler are your very best friend! Mea
| Function | Function type | Target passed as | Returns |
|---|---|---|---|
also |
Extension | it |
Target |
apply |
Extension | this |
Target |
let |
Extension | it |
Block return value |
run |
Extension | this |
Block return value |
with |
Regular | this |
Block return value |
| class Result<T> private constructor(private val result: Any?) { | |
| // discovery | |
| val isFailure: Boolean get() = result is Failure | |
| val isSuccess: Boolean get() = result !is Failure | |
| // value retrieval | |
| fun get(): T = | |
| if (result is Failure) throw result.exception |
| // Bring in Google Guava as a dependency | |
| // compile 'com.google.guava:guava:22.0' | |
| import com.google.common.base.CaseFormat | |
| import java.sql.ResultSet | |
| fun ResultSet.toDataClass(): String { |
| import org.gradle.api.file.SourceDirectorySet | |
| import org.gradle.api.internal.HasConvention | |
| import org.gradle.api.tasks.SourceSet | |
| import org.gradle.api.tasks.SourceSetContainer | |
| import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet | |
| import org.jetbrains.kotlin.gradle.tasks.KotlinCompile | |
| //βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| // BUILD SCRIPT. | |
| //βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |