https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
m

mbonnin

02/01/2023, 1:12 PM
Not sure where to ask this: what's the biggest possible
String
literal in Kotlin? Given multiplatform resources are not really a thing, how bad of an idea is it to store large texts/images in
String
literals?
👀 1
j

jw

02/01/2023, 1:32 PM
On the JVM it'll be 65536
m

mbonnin

02/01/2023, 1:33 PM
Doesn't Kotlin have some compiler magic to split large strings though?
I think I saw this somewhere but can't find the reference anymore so maybe I just smoked it
j

jw

02/01/2023, 1:34 PM
Not that I'm aware of. That would break
const
usages such as passing as annotation values
I would be surprised to see a maximum length for native or JS
Beyond the normal maximum of like 2^53-1 or something
m

mbonnin

02/01/2023, 1:44 PM
I can compile a 100k String so that seems to work
I guess I cannot make it a
@JvmField
I can actually, the
StringBuilder
magic is done in the initializer for something like this:
Copy code
val hugeString = """
  // very long string
"""
Back to my initial point about storing resources in there, it looks like I'll pay a small performance price but the convenience of it might be worth it
If anyone's curious about large string splitting for the JVM (and also for my future self), this is where it's done in the compiler
m

Michael Paus

02/01/2023, 3:51 PM
What is your primary intention to consider that? Wouldn’t the second option mentioned here https://kotlinlang.slack.com/archives/C01D6HTPATV/p1674811279172749?thread_ts=1674787761.307139&cid=C01D6HTPATV not be the better option if it works?
m

mbonnin

02/01/2023, 3:55 PM
Thanks! Didn't know about
compose-jb
resources! My needs is that I need to ship these resources as part as a library so I need a multiplatform way to embed them in .jars, .klibs, etc... I wasn't sure the formats allowed this but I'll investigate 🕵️
d

darkmoon_uk

02/02/2023, 3:37 AM
Given multiplatform resources are not really a thing
MokoResources is my go-to for this; it's rather useful, covering mobiles, Desktop and JS
m

mbonnin

02/02/2023, 8:43 AM
@darkmoon_uk MokoResources is not really what I'm looking for. From what I understand, it's mostly for app developers, I'd like something for lib developers. I don't think MR can embedded strings in a
.klib
, right? Only frameworks?
m

MJegorovas

04/11/2023, 11:46 AM
@mbonnin have you found a solution? Currently having the same problem while building a library and trying to somehow add needed resources into
klib
.
m

mbonnin

04/11/2023, 1:34 PM
@MJegorovas not really, I gave up on this idea for now. Might come back to it at some point
m

Matt Nelson

11/10/2023, 12:43 PM
Sorry to necro-bump this thread, but did you ever find a solution for packaging resources in a
.klib
@mbonnin? I have a prebuilt executable that I wish to ship with my library for darwin/linux targets, so it can be extracted and then executed at runtime.
m

mbonnin

11/10/2023, 12:46 PM
No solution, sorry. "Base64 encode your executable and add it as a string" is the only thing that comes to mind right now. This will put your whole executable in RAM but given that any binary allocates gigabytes now, it's probably ok
m

Matt Nelson

11/10/2023, 12:51 PM
I considered doing that, but each file is 5-8mb sad panda
m

mbonnin

11/10/2023, 12:51 PM
Slack allocates gigabytes. It's probably OK for your app to allocate 8MB. It's not even the size of a picture these days
m

Matt Nelson

11/10/2023, 12:52 PM
Well, it'll be 1 file for every target (they're executables compiled for each platform/arch)
m

mbonnin

11/10/2023, 12:53 PM
But you're not going to decompress them in parallel, do you? It's only going to allocate when you extract and copy/execute
I would expect the mach-O/elf binary to be mmaped so the .data section is on storage, not RAM (no idea if it's actually working like that under the hood but that'd be my assumption)
I'm curious if you try it, let us know how that goes
m

Matt Nelson

11/10/2023, 1:02 PM
So, • compile the platform specific executables • codesign/notarize darwin targets • gradle task reads and writes base64 chunks to a
.kt
file • At runtime that gets written to specified directory. Insanity, but doable
m

mbonnin

11/10/2023, 1:02 PM
Yep, something like so. Write a blog post if you do 🙂
m

Matt Nelson

11/10/2023, 1:13 PM
Yeah I saw that. Doesn't seem like a high priority at the moment.
nod 1
Finally able to start in on tackling this. Any suggestions or ideas would be much appreciated! https://github.com/05nelsonm/kmp-tor-binary/issues/120#issue-2001297235
There she is @mbonnin https://github.com/05nelsonm/kmp-tor-binary/pull/136 Command line tool for converting any file into a
NativeResource
. Still have work to do on extraction of that resource to the application filesystem, but it's pretty slick (i think at least)
🎉 1
Lmao, finally wrapped up that work (had to build another library for something else). Native resources decompression and writing to the filesystem. https://github.com/05nelsonm/kmp-tor-binary/pull/155
👀 1
So, the static
.a
binaries include the shared libs which can now be extracted to the filesystem and be executed in a child process. Jesus what a headache.
m

mbonnin

12/11/2023, 4:16 PM
mind blown
😄 1
OMG, this is both scary and genius 🙂
Might be worth not checking in as source files in source control (ie keep the binary checked in, generate the
.kt
at build time?)
m

Matt Nelson

12/11/2023, 4:22 PM
Yeah, need to think of something. Primary thing I want to do is maintain build reproducability (so others running
tor
will be able to easily verify with like, 3 shell commands + docker).
👍 1
It's a pretty multi-faceted coordination problem
Hey how do I purge this from the git history, lol? Anyone cloning this repo is going to hate life.
just force push to master 😆 ?
m

mbonnin

12/11/2023, 5:17 PM
WE have a couple of .jar files from 2015 that account for half the size of the repo 😅
Since you just pushed, I assume force push now is better than in 5 years from now
💯 1
m

Matt Nelson

12/11/2023, 6:20 PM
😅
🎉 1
7 Views