this post was submitted on 20 Jul 2024
3 points (100.0% liked)

Perchance - Create a Random Text Generator

448 readers
19 users here now

โš„๏ธŽ Perchance

This is a Lemmy Community for perchance.org, a platform for sharing and creating random text generators.

Feel free to ask for help, share your generators, and start friendly discussions at your leisure :)

This community is mainly for discussions between those who are building generators. For discussions about using generators, especially the popular AI ones, the community-led Casual Perchance forum is likely a more appropriate venue.

See this post for the Complete Guide to Posting Here on the Community!

Rules

1. Please follow the Lemmy.World instance rules.

2. Be kind and friendly.

  • Please be kind to others on this community (and also in general), and remember that for many people Perchance is their first experience with coding. We have members for whom English is not their first language, so please be take that into account too :)

3. Be thankful to those who try to help you.

  • If you ask a question and someone has made a effort to help you out, please remember to be thankful! Even if they don't manage to help you solve your problem - remember that they're spending time out of their day to try to help a stranger :)

4. Only post about stuff related to perchance.

  • Please only post about perchance related stuff like generators on it, bugs, and the site.

5. Refrain from requesting Prompts for the AI Tools.

  • We would like to ask to refrain from posting here needing help specifically with prompting/achieving certain results with the AI plugins (text-to-image-plugin and ai-text-plugin) e.g. "What is the good prompt for X?", "How to achieve X with Y generator?"
  • See Perchance AI FAQ for FAQ about the AI tools.
  • You can ask for help with prompting at the 'sister' community Casual Perchance, which is for more casual discussions.
  • We will still be helping/answering questions about the plugins as long as it is related to building generators with them.

6. Search through the Community Before Posting.

  • Please Search through the Community Posts here (and on Reddit) before posting to see if what you will post has similar post/already been posted.

founded 1 year ago
MODERATORS
 

I've just been through the whole engine, all the code, and learned a lot about how this stuff works! I'm planning on writing my own engine that works the same way but hopefully with a number of efficiencies. Just for fun, mind you. I enjoy a challenge...

I took a load of notes along the way for my own learning, but also have many suggestions and ideas for how things can be optimised, improved, potential bugs quashed, etc. Partly for when I come to code my version and explore that side of things. But I thought @perchance may find it useful.

This is just part 1 of what I learned and my "feedback" about various aspects of the engine code.

Easy Wins

Prototypes

Currently it seems all nodes have all those special properties added to them. Instead, (at least most of) those special properties could be part of some perchance_prototype somewhere. And then use Object.setPrototypeOf() to set the node's prototype to it, or make it in the first place with Object.create() to use the prototype.

That way you don't have definitions and functions on all those objects, just once on the prototype. But they'll all work the same. For a lot less memory, and processing time setting it all up.

String-to-number conversion

+string will convert it to a number, just like that.

Currently try-catch blocks and eval are used, but this doesn't seem necessary in particular. And, at least in theory, is less safe than simply telling JS you want it to be a number primitive.

Also means you don't need to do Number(string), which creates a new full Number object. (Different to a number primitive.) And so uses more memory, which will have to be garbage-collected as a lot of these are only temporary values anyway.

Checking if a string can convert to a number

Speaking of which, you can use isNaN(string) will be true if it is a string of a number, and false if it is not.

The code String(Number(string)) === string creates 2 new temporary objects in memory to be collected, and goes through a lot more processing than using the built-in function for this.

Converting to string

And on the other side, ""+val or val+"" will convert anything to a string, and automatically call its .toString() when available. (I think I saw this used a couple of times in the engine.)

Commonly String(string) is used, which again uses more memory than a simple string primitive.

Function nodes getter

(I'm talking about a fn_name() => for an inline or multiline function.)

Seems that a getter is used to build a new function every time it is accessed. Could be that I misunderstood the code, but it's how it appeared to me. If that's how it's working, it uses more memory for each call and wastes processing time making the function again.

Caching it somehow, even just making the function at load-time would save on all of that.

.replaceText()

Currently it splits and joins, creating a new array each time, so that it replaces all instances found instead of just the first one.

There is a built-in method on strings that does this already: string.replaceAll(find, replace), which doesn't create an array, so should be cheaper on memory. And (likely) quicker on processing too. And accepts a regex as well.

//- keep regexes/simple functions out of functions where they are called, // otherwise it creates a new one every time the function is called, // using more memory every time

Storing reusable objects out of functions

The kind of thing I'm talking about are regexes, for example /^\/\/.*$/.test(line).

And functions, for example array.map((value) => value.toLowerCase()).

In both those cases, the regex or function is recreated every time their parent function is called, using more memory. And when we're talking about some of the complex functions that are used everywhere this can really mount up.

Simply keeping them outside the function they are used in means that object (regex/function) is reused every time instead. And may potentially be reused by multiple different functions too.

//- reuse overriden selectMany (etc.) array.join function override, // to not create a new function every time it is processed.

Reusing overriding methods

A good example of the previous heading is, when an array is created and then has its own .join() or .toString() method added as a simple property. Currently that method is created each time such an array is made--using memory.

When these could all use the same exact function as their methods instead.

...

These are all just thoughts on easy low-hanging fruit that could be plucked for the benefit of how the engine runs. I'm not saying "do as I say" or anything like that, just putting these ideas out there.

Thanks for creating the platform in the first place, it's really fun ๐Ÿ‘

no comments (yet)
sorted by: hot top controversial new old
there doesn't seem to be anything here