Skip to content

Commit

Permalink
应用wasm target (#11)
Browse files Browse the repository at this point in the history
* 性能优化

* 应用wasm target
  • Loading branch information
ssttkkl authored Dec 13, 2023
1 parent 33a3f13 commit 7a4dfaa
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ class KmpLibConventionPlugin : Plugin<Project> {
browser()
nodejs()
}
wasmJs {
browser()
nodejs()
}

iosArm64()
iosX64()
iosSimulatorArm64()

val hostOs = System.getProperty("os.name")
val isMingwX64 = hostOs.startsWith("Windows")
Expand Down
13 changes: 12 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin

plugins {
alias(libs.plugins.kotlin.multiplatform) apply false
alias(libs.plugins.kotlin.jvm) apply false
Expand All @@ -16,7 +19,15 @@ tasks.wrapper {
distributionType = Wrapper.DistributionType.ALL
}


rootProject.plugins.withType<NodeJsRootPlugin> {
rootProject.the<NodeJsRootExtension>().apply {
nodeVersion = "21.0.0-v8-canary20231024d0ddc81258"
nodeDownloadBaseUrl = "https://nodejs.org/download/v8-canary"
}
}

dependencies {
kover(project(":mahjong-utils"))
kover(project(":mahjong-utils-entry"))
}
}
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[versions]
kotlin = "1.9.20"
kotlinx-coroutines = "1.7.3"
kotlinx-serialization = "1.6.0"
kotlinx-coroutines = "1.8.0-RC"
kotlinx-serialization = "1.6.2"
ktor = "2.3.6"
buildlogic = "0.0.1"

Expand Down
29 changes: 29 additions & 0 deletions mahjong-utils-entry/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,29 @@ kotlin {
)
}
}
wasmJs {
browser {
binaries.library()
useCommonJs()
}
nodejs {
binaries.library()
useCommonJs()
}
applyBinaryen()
compilations["main"].packageJson {
name = "mahjong-utils-entry-wasm"
customField(
"author", mapOf(
"name" to "ssttkkl",
"email" to "[email protected]"
)
)
customField(
"license", "MIT"
)
}
}

val hostOs = System.getProperty("os.name")
val isMingwX64 = hostOs.startsWith("Windows")
Expand Down Expand Up @@ -65,6 +88,12 @@ kotlin {
val nativeTest by getting {
dependsOn(nonJsTest)
}
val wasmJsMain by getting {
dependsOn(nonJsMain)
}
val wasmJsTest by getting {
dependsOn(nonJsTest)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package mahjongutils.common

import mahjongutils.models.hand.HandPattern

internal class BestHandPatternsSelector<T : HandPattern>(
private val calcShanten: (T) -> Int
) {
var bestShanten = 100
private set

var bestPatterns = ArrayList<T>()
private set

fun receive(pattern: T) {
val patShanten = calcShanten(pattern)
if (patShanten < bestShanten) {
bestShanten = patShanten
bestPatterns = ArrayList()
}
if (patShanten == bestShanten) {
bestPatterns.add(pattern)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,6 @@ private class RegularHandPatternSearcher(
}
}

internal fun regularHandPatternSearch(tiles: List<Tile>, furo: List<Furo>): Collection<RegularHandPattern> {
val patterns = ArrayList<RegularHandPattern>()
val searcher = RegularHandPatternSearcher(tiles, furo) {
patterns.add(it)
}
searcher.run()
return patterns
internal fun regularHandPatternSearch(tiles: List<Tile>, furo: List<Furo>, callback: (RegularHandPattern) -> Unit) {
RegularHandPatternSearcher(tiles, furo, callback).run()
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import mahjongutils.hanhu.getParentPointByHanHu
import mahjongutils.models.Furo
import mahjongutils.models.Tile
import mahjongutils.models.Wind
import mahjongutils.shanten.*
import mahjongutils.shanten.CommonShantenResult
import mahjongutils.shanten.ShantenWithGot
import mahjongutils.shanten.UnionShantenResult
import mahjongutils.shanten.shanten
import mahjongutils.yaku.Yaku
import mahjongutils.yaku.Yakus

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,44 @@ import mahjongutils.models.hand.Hand
import mahjongutils.models.hand.KokushiHandPattern
import mahjongutils.models.isYaochu

private fun buildKokushiPattern(tiles: List<Tile>): Collection<KokushiHandPattern> {
val yaochu = HashSet<Tile>()
val repeated = HashSet<Tile>()
val remaining = ArrayList<Tile>()
private fun buildKokushiPattern(tiles: List<Tile>): Sequence<KokushiHandPattern> {
return sequence {
val yaochu = HashSet<Tile>()
val repeated = HashSet<Tile>()
val remaining = ArrayList<Tile>()

for (t in tiles) {
if (t.isYaochu) {
if (t in yaochu) {
if (t in repeated) {
remaining.add(t)
for (t in tiles) {
if (t.isYaochu) {
if (t in yaochu) {
if (t in repeated) {
remaining.add(t)
} else {
repeated.add(t)
}
} else {
repeated.add(t)
yaochu.add(t)
}
} else {
yaochu.add(t)
remaining.add(t)
}
} else {
remaining.add(t)
}
}

return buildList {
if (repeated.isNotEmpty()) {
// 非十三面
for (t in repeated) {
val pat = KokushiHandPattern(yaochu, t, remaining + (repeated - t))
add(pat)
yield(pat)
}
} else {
// 十三面
val pat = KokushiHandPattern(yaochu, null, remaining)
add(pat)
yield(pat)
}
}
}

private fun handleKokushiShantenWithoutGot(tiles: List<Tile>): Pair<ShantenWithoutGot, Collection<KokushiHandPattern>> {
val patterns = buildKokushiPattern(tiles)
val (shantenNum, bestPatterns) = selectBestPatterns(patterns, KokushiHandPattern::calcShanten)
val (shantenNum, bestPatterns) = selectBestPatterns(buildKokushiPattern(tiles), KokushiHandPattern::calcShanten)

val pat = bestPatterns.first()
if (pat.repeated != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package mahjongutils.shanten

import mahjongutils.common.TILE_CLING
import mahjongutils.common.calcAdvance
import mahjongutils.common.calcShanten
import mahjongutils.common.regularHandPatternSearch
import mahjongutils.common.*
import mahjongutils.models.Furo
import mahjongutils.models.Kan
import mahjongutils.models.Tile
import mahjongutils.models.TileType
import mahjongutils.models.hand.Hand
import mahjongutils.models.hand.RegularHandPattern

private fun bestRegularHandPatternSearch(tiles: List<Tile>, furo: List<Furo>): Pair<Int, List<RegularHandPattern>> {
val selector = BestHandPatternsSelector(RegularHandPattern::calcShanten)
regularHandPatternSearch(tiles, furo) {
selector.receive(it)
}
return Pair(selector.bestShanten, selector.bestPatterns)
}

private fun getGoodShapeAdvance(
tiles: List<Tile>, furo: List<Furo>,
remaining: IntArray,
Expand Down Expand Up @@ -110,8 +115,7 @@ private fun handleRegularShantenWithoutGot(
calcGoodShapeAdvance: Boolean = true,
calcImprovement: Boolean = true
): Pair<ShantenWithoutGot, Collection<RegularHandPattern>> {
val patterns = regularHandPatternSearch(tiles, furo)
val (bestShanten, bestPatterns) = selectBestPatterns(patterns, RegularHandPattern::calcShanten)
val (bestShanten, bestPatterns) = bestRegularHandPatternSearch(tiles, furo)

val tilesCount = getTileCount(tiles, furo)
val remaining = getRemainingFromTileCount(tilesCount)
Expand Down Expand Up @@ -158,8 +162,7 @@ private fun handleRegularShantenWithGot(
allowAnkan: Boolean = true,
calcImprovement: Boolean = true,
): Pair<ShantenWithGot, Collection<RegularHandPattern>> {
val patterns = regularHandPatternSearch(tiles, furo)
val (bestShanten, bestPatterns) = selectBestPatterns(patterns, RegularHandPattern::calcShanten)
val (bestShanten, bestPatterns) = bestRegularHandPatternSearch(tiles, furo)

val tilesCount = getTileCount(tiles, furo)
val remaining = getRemainingFromTileCount(tilesCount)
Expand Down
21 changes: 6 additions & 15 deletions mahjong-utils/src/commonMain/kotlin/mahjongutils/shanten/Utils.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package mahjongutils.shanten

import mahjongutils.common.BestHandPatternsSelector
import mahjongutils.models.Furo
import mahjongutils.models.Tile
import mahjongutils.models.countAsCodeArray
Expand Down Expand Up @@ -35,24 +36,14 @@ internal fun ensureLegalTiles(
}

internal fun <T : HandPattern> selectBestPatterns(
patterns: Collection<T>,
patterns: Sequence<T>,
calcShanten: (T) -> Int
): Pair<Int, Collection<T>> {
var bestShanten = 100
var bestPattern = ArrayList<T>()

for (pat in patterns) {
val patShanten = calcShanten(pat)
if (patShanten < bestShanten) {
bestShanten = patShanten
bestPattern = ArrayList()
}
if (patShanten == bestShanten) {
bestPattern.add(pat)
}
val selector = BestHandPatternsSelector(calcShanten)
patterns.forEach {
selector.receive(it)
}

return Pair(bestShanten, bestPattern)
return Pair(selector.bestShanten, selector.bestPatterns)
}

internal fun getTileCount(tiles: Collection<Tile>, furo: Collection<Furo> = emptyList()): IntArray {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package mahjongutils.yaku

import mahjongutils.models.*
import mahjongutils.hora.ChitoiHoraHandPattern
import mahjongutils.hora.RegularHoraHandPattern
import mahjongutils.models.*

/**
* 役牌系列checker工厂
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package mahjongutils.yaku

import mahjongutils.models.*
import mahjongutils.hora.ChitoiHoraHandPattern
import mahjongutils.hora.KokushiHoraHandPattern
import mahjongutils.hora.RegularHoraHandPattern
import mahjongutils.models.*

/**
* 包含所有役种
Expand Down
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ pluginManagement {
google()
mavenCentral()
gradlePluginPortal()
maven("https://maven.pkg.jetbrains.space/kotlin/p/wasm/experimental")
}
}

dependencyResolutionManagement {
repositories {
google()
mavenCentral()
maven("https://maven.pkg.jetbrains.space/kotlin/p/wasm/experimental")
}
}

0 comments on commit 7a4dfaa

Please sign in to comment.